Added admin control page
This commit is contained in:
parent
d19cfd103d
commit
034c3eed70
@ -127,3 +127,8 @@ MEDIA_URL = '/media/'
|
|||||||
|
|
||||||
LOGIN_REDIRECT_URL = '/'
|
LOGIN_REDIRECT_URL = '/'
|
||||||
LOGOUT_REDIRECT_URL = '/'
|
LOGOUT_REDIRECT_URL = '/'
|
||||||
|
|
||||||
|
ZENDESK_ROLES = {
|
||||||
|
'engineer': '360005209000',
|
||||||
|
'light_agent': '360005208980',
|
||||||
|
}
|
||||||
|
@ -20,7 +20,7 @@ from django.urls import path, include
|
|||||||
|
|
||||||
from access_controller import settings
|
from access_controller import settings
|
||||||
from access_controller.settings import DEBUG
|
from access_controller.settings import DEBUG
|
||||||
from main.views import main_page, profile_page, CustomRegistrationView
|
from main.views import main_page, profile_page, CustomRegistrationView, AdminPageView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls, name='admin'),
|
path('admin/', admin.site.urls, name='admin'),
|
||||||
@ -30,4 +30,5 @@ urlpatterns = [
|
|||||||
path('accounts/login/', LoginView.as_view(extra_context={}), name='login'), # TODO add extra context
|
path('accounts/login/', LoginView.as_view(extra_context={}), name='login'), # TODO add extra context
|
||||||
path('accounts/', include('django.contrib.auth.urls')),
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
path('accounts/', include('django_registration.backends.one_step.urls')),
|
path('accounts/', include('django_registration.backends.one_step.urls')),
|
||||||
|
path('control/', AdminPageView.as_view(), name='control')
|
||||||
]
|
]
|
||||||
|
@ -5,6 +5,8 @@ from zenpy.lib.exception import APIException
|
|||||||
|
|
||||||
from main.models import UserProfile
|
from main.models import UserProfile
|
||||||
|
|
||||||
|
from access_controller.settings import ZENDESK_ROLES as ROLES
|
||||||
|
|
||||||
|
|
||||||
class ZendeskAdmin:
|
class ZendeskAdmin:
|
||||||
"""
|
"""
|
||||||
@ -106,6 +108,24 @@ class ZendeskAdmin:
|
|||||||
except APIException:
|
except APIException:
|
||||||
raise ValueError('invalid access_controller`s login data')
|
raise ValueError('invalid access_controller`s login data')
|
||||||
|
|
||||||
|
def get_user(self, email):
|
||||||
|
user = self.admin.users.search(email).values[0]
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
def make_engineer(user_profile):
|
||||||
|
zendesk = ZendeskAdmin()
|
||||||
|
user = zendesk.get_user(user_profile.user.email)
|
||||||
|
user.custom_role_id = ROLES['engineer']
|
||||||
|
zendesk.admin.users.update(user)
|
||||||
|
|
||||||
|
|
||||||
|
def make_light_agent(user_profile):
|
||||||
|
zendesk = ZendeskAdmin()
|
||||||
|
user = zendesk.get_user(user_profile.user.email)
|
||||||
|
user.custom_role_id = ROLES['light_agent']
|
||||||
|
zendesk.admin.users.update(user)
|
||||||
|
|
||||||
|
|
||||||
def update_profile(user_profile: UserProfile):
|
def update_profile(user_profile: UserProfile):
|
||||||
"""
|
"""
|
||||||
@ -145,10 +165,10 @@ def check_user_auth(email: str, password: str) -> bool:
|
|||||||
:rtype: :class:`bool`
|
:rtype: :class:`bool`
|
||||||
"""
|
"""
|
||||||
creds = {
|
creds = {
|
||||||
'email': email,
|
'email': email,
|
||||||
'password': password,
|
'password': password,
|
||||||
'subdomain': 'ngenix1612197338',
|
'subdomain': 'ngenix1612197338',
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
user = Zenpy(**creds)
|
user = Zenpy(**creds)
|
||||||
user.search(email, type='user')
|
user.search(email, type='user')
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django_registration.forms import RegistrationFormUniqueEmail
|
from django_registration.forms import RegistrationFormUniqueEmail
|
||||||
|
|
||||||
|
from main.models import UserProfile
|
||||||
|
|
||||||
|
|
||||||
class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
||||||
"""
|
"""
|
||||||
@ -32,3 +34,15 @@ class CustomRegistrationForm(RegistrationFormUniqueEmail):
|
|||||||
class Meta(RegistrationFormUniqueEmail.Meta):
|
class Meta(RegistrationFormUniqueEmail.Meta):
|
||||||
fields = RegistrationFormUniqueEmail.Meta.fields
|
fields = RegistrationFormUniqueEmail.Meta.fields
|
||||||
fields.insert(2, 'password_zen')
|
fields.insert(2, 'password_zen')
|
||||||
|
|
||||||
|
|
||||||
|
class AdminPageUsers(forms.Form):
|
||||||
|
users = forms.ModelMultipleChoiceField(
|
||||||
|
queryset=UserProfile.objects.filter(role='agent'),
|
||||||
|
widget=forms.CheckboxSelectMultiple(
|
||||||
|
attrs={
|
||||||
|
'class': 'form-check-input'
|
||||||
|
}
|
||||||
|
),
|
||||||
|
label=''
|
||||||
|
)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from django.contrib.auth.models import User
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
|
@ -2,76 +2,101 @@
|
|||||||
|
|
||||||
{% load static %}
|
{% load static %}
|
||||||
|
|
||||||
{% block title %}{{ pagename }}{% endblock %}
|
{% block title %}
|
||||||
|
Управление
|
||||||
|
{%endblock %}
|
||||||
|
|
||||||
{% block heading %}Управление{% endblock %}
|
{% block heading %}
|
||||||
|
Управление
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
{% block extra_css %}
|
{% block extra_css %}
|
||||||
<link rel="stylesheet" href="{% static 'main/css/work.css' %}">
|
<link rel="stylesheet" href="{% static 'main/css/work.css' %}"/>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="container-md">
|
<div class="container-md">
|
||||||
|
<div class="new-section">
|
||||||
<div class="new-section">
|
<p class="row page-description">Основаная информация о странице</p>
|
||||||
<p class="row page-description">Основаная информация о странице</p>
|
</div>
|
||||||
</div>
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
<div class="row justify-content-center new-section">
|
<div class="row justify-content-center new-section">
|
||||||
<div class="col-10">
|
<div style="display: none">
|
||||||
<h6 class="table-title">Список сотрудников</h6>
|
{% for field in form.users %}
|
||||||
<table class="light-table">
|
{{ field.tag }}
|
||||||
<thead>
|
{% endfor %}
|
||||||
<th>ID</th>
|
</div>
|
||||||
<th>Email</th>
|
<div>
|
||||||
<th>Role</th>
|
<div>
|
||||||
<th>Name(link to profile)</th>
|
|
||||||
<th>Checked</th>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td>1</td>
|
|
||||||
<td>big_boss123@example.ru</td>
|
|
||||||
<td>engineer</td>
|
|
||||||
<td><a href="#">Иван Иванов</a></td>
|
|
||||||
<td><input class="form-check-input" type="checkbox" value=""></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>2</td>
|
|
||||||
<td>gachi_cool456@example.ru</td>
|
|
||||||
<td>light engineer</td>
|
|
||||||
<td><a href="#">Пётр Петров</a></td>
|
|
||||||
<td><input class="form-check-input" type="checkbox"value=""></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-10">
|
||||||
|
<h6 class="table-title">Список сотрудников</h6>
|
||||||
|
<table class="light-table">
|
||||||
|
<thead>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Email</th>
|
||||||
|
<th>Role</th>
|
||||||
|
<th>Name(link to profile)</th>
|
||||||
|
<th>Checked</th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for user in users %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ user.id }}</td>
|
||||||
|
<td>{{ user.user.email }}</td>
|
||||||
|
<td>{{ user.role }}</td>
|
||||||
|
<td><a href="#">{{ user.name }}</a></td>
|
||||||
|
<td class="checkbox_field"></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row justify-content-center new-section">
|
<div class="row justify-content-center new-section">
|
||||||
<div class="col-5">
|
<div class="col-5">
|
||||||
<div class="info">
|
<div class="info">
|
||||||
<div class="info-row">
|
<div class="info-row">
|
||||||
<div class="info-target">Инженеров:</div>
|
<div class="info-target">Инженеров:</div>
|
||||||
<div class="info-quantity">
|
<div class="info-quantity">
|
||||||
<div class="status-circle-small light-green"></div>
|
<div class="status-circle-small light-green"></div>
|
||||||
<span class="info-quantity-value">13</span>
|
<span class="info-quantity-value">13</span>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="info-row">
|
|
||||||
<div class="info-target">Легких агентов:</div>
|
|
||||||
<div class="info-quantity">
|
|
||||||
<div class="status-circle-small light-yellow"></div>
|
|
||||||
<span class="info-quantity-value">22</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<div class="info-target">Легких агентов:</div>
|
||||||
|
<div class="info-quantity">
|
||||||
|
<div class="status-circle-small light-yellow"></div>
|
||||||
|
<span class="info-quantity-value">22</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-5">
|
</div>
|
||||||
<button class="request-acess-button default-button">Назначить выбранных на роль инженера</button>
|
<div class="col-5">
|
||||||
<button class="hand-over-acess-button default-button">Назначить выбранных на роль легкого агента</button>
|
<button type="submit" name="engineer" class="request-acess-button default-button">
|
||||||
</div>
|
Назначить выбранных на роль инженера
|
||||||
|
</button>
|
||||||
|
<button type="submit" name="light_agent" class="hand-over-acess-button default-button">
|
||||||
|
Назначить выбранных на роль легкого агента
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
|
||||||
|
<script>
|
||||||
|
"use strict";
|
||||||
|
let checkboxes = document.getElementsByName("users");
|
||||||
|
let fields = document.querySelectorAll(".checkbox_field");
|
||||||
|
if (checkboxes.length == fields.length) {
|
||||||
|
for (let i = 0; i < fields.length; ++i) {
|
||||||
|
let el = checkboxes[i].cloneNode(true);
|
||||||
|
fields[i].appendChild(el);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
from django.shortcuts import render
|
from django.core.exceptions import PermissionDenied
|
||||||
|
from django.shortcuts import render, get_list_or_404
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
|
|
||||||
from main.extra_func import check_user_exist, check_user_auth, update_profile
|
from main.apiauth import api_auth
|
||||||
|
from main.extra_func import check_user_exist, check_user_auth, update_profile, make_engineer, make_light_agent
|
||||||
from main.models import UserProfile
|
from main.models import UserProfile
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from main.forms import CustomRegistrationForm
|
from main.forms import CustomRegistrationForm, AdminPageUsers
|
||||||
from django_registration.views import RegistrationView
|
from django_registration.views import RegistrationView, FormView
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
|
|
||||||
from zenpy import Zenpy
|
from zenpy import Zenpy
|
||||||
|
|
||||||
@ -70,3 +73,35 @@ def profile_page(request):
|
|||||||
|
|
||||||
def main_page(request):
|
def main_page(request):
|
||||||
return render(request, 'pages/index.html')
|
return render(request, 'pages/index.html')
|
||||||
|
|
||||||
|
|
||||||
|
class AdminPageView(FormView, LoginRequiredMixin):
|
||||||
|
template_name = 'pages/adm_ruleset.html'
|
||||||
|
form_class = AdminPageUsers
|
||||||
|
success_url = '/control/'
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
if 'engineer' in self.request.POST:
|
||||||
|
self.make_engineers(form.cleaned_data['users'])
|
||||||
|
elif 'light_agent' in self.request.POST:
|
||||||
|
self.make_light_agents(form.cleaned_data['users'])
|
||||||
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def make_engineers(users):
|
||||||
|
for user in users:
|
||||||
|
make_engineer(user)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def make_light_agents(users):
|
||||||
|
for user in users:
|
||||||
|
make_light_agent(user)
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs): # TODO: add engineers and agents count
|
||||||
|
if self.request.user.userprofile.role != 'admin':
|
||||||
|
raise PermissionDenied
|
||||||
|
context = super().get_context_data(**kwargs)
|
||||||
|
context['users'] = get_list_or_404(
|
||||||
|
UserProfile, role='agent')
|
||||||
|
context['engineers'] = get_list_or_404(UserProfile, )
|
||||||
|
return context # TODO: need to get profile page url
|
||||||
|
Loading…
x
Reference in New Issue
Block a user