Merge 'develop' into 'feature/work/backend'

This commit is contained in:
Andrey Kovalev 2021-03-02 20:19:42 +03:00
commit 09a423060c
8 changed files with 196 additions and 77 deletions

View File

@ -172,3 +172,8 @@ LOGGING = {
}
}
}
ZENDESK_ROLES = {
'engineer': 360005209000,
'light_agent': 360005208980,
}

View File

@ -17,13 +17,9 @@ from django.contrib import admin
from django.contrib.auth.views import LoginView
from django.contrib.auth import views as auth_views
from django.urls import path, include
from access_controller import settings
from access_controller.settings import DEBUG
from main.views import main_page, profile_page, CustomRegistrationView, work_page, work_hand_over, work_become_engineer
from main.views import main_page, profile_page, CustomRegistrationView
from main.views import main_page, profile_page, CustomRegistrationView, work_page, work_hand_over, work_become_engineer, AdminPageView
urlpatterns = [
@ -39,6 +35,7 @@ urlpatterns = [
path('work/become_engineer/', work_become_engineer, name="work_become_engineer"),
path('accounts/', include('django_registration.backends.activation.urls')),
path('accounts/login/', include('django.contrib.auth.urls')),
path('control/', AdminPageView.as_view(), name='control')
]
urlpatterns += [

View File

@ -5,6 +5,8 @@ from zenpy.lib.exception import APIException
from main.models import UserProfile
from access_controller.settings import ZENDESK_ROLES as ROLES
class ZendeskAdmin:
"""
@ -114,12 +116,34 @@ class ZendeskAdmin:
raise ValueError('invalid access_controller`s login data')
def update_role(user_profile, role):
zendesk = ZendeskAdmin()
user = zendesk.get_user(user_profile.user.email)
user.custom_role_id = role
zendesk.admin.users.update(user)
def make_engineer(user_profile):
update_role(user_profile, ROLES['engineer'])
def make_light_agent(user_profile):
update_role(user_profile, ROLES['light_agent'])
def get_users_list():
zendesk = ZendeskAdmin()
admin = zendesk.get_user(zendesk.email)
org = next(zendesk.admin.users.organizations(user=admin))
return zendesk.admin.organizations.users(org)
def update_profile(user_profile: UserProfile):
"""
Функция обновляет профиль пользователя в соотвтетствии с текущим в Zendesk
Функция обновляет профиль пользователя в соотвтетствии с текущим в Zendesk
:param user_profile: Объект профиля пользователя
:type user_profile: :class:`main.models.UserProfile`
:param user_profile: Объект профиля пользователя
:type user_profile: :class:`main.models.UserProfile`
"""
user = ZendeskAdmin().get_user(user_profile.user.email)
user_profile.name = user.name
@ -140,7 +164,7 @@ def check_user_exist(email: str) -> bool:
return ZendeskAdmin().check_user(email)
def get_user_organization(email: str) -> bool:
def get_user_organization(email: str) -> str:
"""
Функция возвращает организацию пользователя

View File

@ -1,6 +1,8 @@
from django import forms
from django_registration.forms import RegistrationFormUniqueEmail
from main.models import UserProfile
class CustomRegistrationForm(RegistrationFormUniqueEmail):
"""
@ -24,3 +26,15 @@ class CustomRegistrationForm(RegistrationFormUniqueEmail):
class Meta(RegistrationFormUniqueEmail.Meta):
fields = RegistrationFormUniqueEmail.Meta.fields
class AdminPageUsers(forms.Form):
users = forms.ModelMultipleChoiceField(
queryset=UserProfile.objects.filter(role='agent'),
widget=forms.CheckboxSelectMultiple(
attrs={
'class': 'form-check-input'
}
),
label=''
)

View File

@ -1,5 +1,5 @@
from django.contrib.auth.models import User
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

View File

@ -2,76 +2,108 @@
{% load static %}
{% block title %}{{ pagename }}{% endblock %}
{% block title %}Управление{%endblock %}
{% block heading %}Управление{% endblock %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'main/css/work.css' %}">
<link rel="stylesheet" href="{% static 'main/css/work.css' %}"/>
{% endblock %}
{% block content %}
<div class="container-md">
<div class="new-section">
<p class="row page-description">Основная информация о странице</p>
</div>
<div class="new-section">
<p class="row page-description">Основаная информация о странице</p>
<form method="post">
{% csrf_token %}
<div class="row justify-content-center new-section">
<div style="display: none">
{% for field in form.users %}
{{ field.tag }}
{% endfor %}
</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 class="row justify-content-center new-section">
<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>
<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 class="row justify-content-center new-section">
<div class="col-5">
<div class="info">
<div class="info-row">
<div class="info-target">Инженеров:</div>
<div class="info-quantity">
<div class="status-circle-small light-green"></div>
<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 class="col-5">
<div class="info">
<div class="info-row">
<div class="info-target">Инженеров:</div>
<div class="info-quantity">
<div class="status-circle-small light-green"></div>
<span class="info-quantity-value">{{ engineers }}</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">{{ light_agents }}</span>
</div>
</div>
</div>
<div class="col-5">
<button class="request-acess-button default-button">Назначить выбранных на роль инженера</button>
<button class="hand-over-acess-button default-button">Назначить выбранных на роль легкого агента</button>
</div>
</div>
<div class="col-5">
<button type="submit" name="engineer" class="request-acess-button default-button">
Назначить выбранных на роль инженера
</button>
<button type="submit" name="light_agent" class="hand-over-acess-button default-button">
Назначить выбранных на роль легкого агента
</button>
</div>
</div>
</form>
</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 %}

View File

@ -1,10 +1,9 @@
{% extends 'base/base.html' %}
{% block title %}
Авторизация
{% endblock %}
{% block heading %}
Авторизация
{% endblock %}
{% block title %}Авторизация{% endblock %}
{% block heading %}Авторизация{% endblock %}
{% block content %}
<div class="container">
<div class="card mx-auto" style="width: 40rem">

View File

@ -5,15 +5,22 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
from django.shortcuts import render
from django.shortcuts import render, get_list_or_404
from django.urls import reverse_lazy
from django.views.generic import FormView
from django_registration.backends.one_step.views import RegistrationView
from access_controller.settings import EMAIL_HOST_USER
from main.extra_func import check_user_exist, update_profile, get_user_organization
from main.forms import CustomRegistrationForm
from main.extra_func import check_user_exist, update_profile, get_user_organization, \
make_engineer, make_light_agent, get_users_list
from django.contrib.auth.models import User
from main.models import UserProfile
from main.forms import CustomRegistrationForm, AdminPageUsers
from django_registration.views import RegistrationView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
import logging
from zenpy import Zenpy
@ -22,6 +29,7 @@ from zenpy.lib.api_objects import User as ZenpyUser
from .models import UserProfile
import os
from access_controller.settings import ZENDESK_ROLES
class CustomRegistrationView(RegistrationView):
@ -144,3 +152,43 @@ def main_page(request):
logger = logging.getLogger('main.index')
logger.info('Index page opened')
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):
[make_engineer(user) for user in users]
@staticmethod
def make_light_agents(users):
[make_light_agent(user) for user in users]
@staticmethod
def count_users(users): # TODO: this func counts users from all zendesk instead of just from a model
engineers, light_agents = 0, 0
for user in users:
if user.custom_role_id == ZENDESK_ROLES['engineer']:
engineers += 1
elif user.custom_role_id == ZENDESK_ROLES['light_agent']:
light_agents += 1
return engineers, light_agents
def get_context_data(self, **kwargs):
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'], context['light_agents'] = self.count_users(get_users_list())
return context # TODO: need to get profile page url