Merge branch 'feature/periodic' into 'develop'

Feature/periodic into develop

See merge request 2020-2021/online/s101/group-02/access_controller!33
This commit is contained in:
Кравченко Артем 2021-03-18 14:24:19 +00:00
commit 870ce45a63
10 changed files with 250 additions and 51 deletions

View File

@ -36,6 +36,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'django_registration',
'rest_framework',
'main',
]
@ -183,4 +184,12 @@ ZENDESK_ROLES = {
'light_agent': 360005208980,
}
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
ONE_DAY = 12 # Количество часов в 1 рабочем дне

View File

@ -16,23 +16,26 @@ Including another URLconf
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from main.views import work_page, work_hand_over, work_become_engineer, AdminPageView
from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView
from main.views import work_page, work_hand_over, work_become_engineer, \
AdminPageView, statistic_page
from main.urls import router
urlpatterns = [
path('admin/', admin.site.urls, name='admin'),
path('', main_page, name='index'),
path('accounts/profile/', profile_page, name='profile'),
path('accounts/register/', CustomRegistrationView.as_view(), name='registration'),
path('accounts/login/', CustomLoginView.as_view(extra_context={}), name='login',), # TODO add extra context
path('accounts/login/', CustomLoginView.as_view(), name='login'),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('django_registration.backends.one_step.urls')),
path('work/<int:id>', work_page, name="work"),
path('work/hand_over/', work_hand_over, name="work_hand_over"),
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'),
path('statistic/', statistic_page, name='statistic')
]
@ -59,3 +62,8 @@ urlpatterns += [
name='password_reset_complete'
),
]
# Django REST
urlpatterns += [
path('api/', include(router.urls))
]

View File

@ -5,8 +5,11 @@ from django.contrib.auth.models import User
from zenpy import Zenpy
from zenpy.lib.exception import APIException
from access_controller.settings import ZENDESK_ROLES as ROLES, ONE_DAY
from main.models import UserProfile, RoleChangeLogs
from django.core.exceptions import ObjectDoesNotExist
from access_controller.settings import ZENDESK_ROLES as ROLES, ONE_DAY
class ZendeskAdmin:
@ -145,9 +148,11 @@ def get_users_list() -> list:
Функция **get_users_list** возвращает список пользователей Zendesk, относящихся к организации.
"""
zendesk = ZendeskAdmin()
admin = zendesk.get_user(zendesk.email)
org = next(zendesk.admin.users.organizations(user=admin))
return zendesk.admin.organizations.users(org)
# У пользователей должна быть организация SYSTEM
org = next(zendesk.admin.search(type='organization', name='SYSTEM'))
users = zendesk.admin.organizations.users(org)
return users
def update_profile(user_profile: UserProfile) -> UserProfile:
@ -176,6 +181,63 @@ def get_user_organization(email: str) -> str:
return ZendeskAdmin().get_user_org(email)
def check_user_auth(email: str, password: str) -> bool:
"""
Функция проверяет, верны ли входные данные
:raise: :class:`APIException`: исключение, вызываемое если пользователь не аутентифицирован
"""
creds = {
'email': email,
'password': password,
'subdomain': 'ngenix1612197338',
}
try:
user = Zenpy(**creds)
user.search(email, type='user')
except APIException:
return False
return True
def update_user_in_model(profile, zendesk_user):
profile.name = zendesk_user.name
profile.role = zendesk_user.role
profile.image = zendesk_user.photo['content_url'] if zendesk_user.photo else None
profile.custom_role_id = zendesk_user.custom_role_id
profile.save()
def count_users(users) -> tuple:
"""
Функция подсчета количества сотрудников с ролями engineer и light_a
.. 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 == ROLES['engineer']:
engineers += 1
elif user.custom_role_id == ROLES['light_agent']:
light_agents += 1
return engineers, light_agents
def update_users_in_model():
"""
Обновляет пользователей в модели UserProfile по списку пользователей в организации
"""
users = get_users_list()
for user in users:
try:
profile = User.objects.get(email=user.email).userprofile
update_user_in_model(profile, user)
except ObjectDoesNotExist:
pass
return users
def daterange(start_date, end_date) -> list:
"""
Возвращает список дней с start_date по end_date исключая правую границу

17
main/serializers.py Normal file
View File

@ -0,0 +1,17 @@
from django.contrib.auth.models import User
from rest_framework import serializers
from main.models import UserProfile
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['email']
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = ['user', 'id', 'role', 'name']

View File

@ -27,6 +27,7 @@
</style>
{% block extra_css %}{% endblock %}
{% block extra_scripts %}{% endblock %}
</head>
<body class="d-flex flex-column h-100">

View File

@ -10,11 +10,15 @@
<link rel="stylesheet" href="{% static 'main/css/work.css' %}"/>
{% endblock %}
{% block extra_scripts %}
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
{% endblock%}
{% block content %}
<div class="container-md">
<div class="new-section">
<p class="row page-description">Основная информация о странице</p>
</div>
{% block form %}
<form method="post">
@ -37,25 +41,23 @@
<table class="light-table">
<thead>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Name(link to profile)</th>
<th>Checked</th>
</thead>
<tbody>
<tbody id="old_tbody">
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td><a href="#">{{ user.name }}</a></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>
<tbody id="new_tbody"></tbody>
</table>
{% endblock%}
@ -103,6 +105,6 @@
{% endblock %}
</div>
<script src="{% static 'main/js/control.js'%}"></script>
<script src="{% static 'main/js/control.js'%}" type="text/babel"></script>
{% endblock %}

6
main/urls.py Normal file
View File

@ -0,0 +1,6 @@
from rest_framework.routers import DefaultRouter
from main.views import UsersViewSet
router = DefaultRouter()
router.register(r'users', UsersViewSet)

View File

@ -2,28 +2,42 @@ import logging
import os
from datetime import datetime
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.models import User, Permission
from django.contrib.auth.tokens import default_token_generator
from django.contrib.auth.views import LoginView
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render, get_list_or_404, redirect
from django.urls import reverse_lazy, reverse
from django.views.generic import FormView
from django_registration.views import RegistrationView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from zenpy import Zenpy
from zenpy.lib.api_objects import User as ZenpyUser
from access_controller.settings import EMAIL_HOST_USER, ZENDESK_ROLES
from main.extra_func import check_user_exist, update_profile, get_user_organization, make_engineer, make_light_agent, \
get_users_list, StatisticData
from main.extra_func import check_user_exist, update_profile, get_user_organization, \
make_engineer, make_light_agent, get_users_list, update_users_in_model, count_users, \
StatisticData
from main.models import UserProfile
from main.forms import AdminPageUsers, CustomRegistrationForm, CustomAuthenticationForm, StatisticForm
from .models import UserProfile
from access_controller.settings import EMAIL_HOST_USER, ZENDESK_ROLES
# Django REST
from rest_framework import viewsets, status
from main.serializers import ProfileSerializer
from rest_framework.response import Response
content_type_temp = ContentType.objects.get_for_model(UserProfile)
permission_temp, created = Permission.objects.get_or_create(
codename='has_control_access',
content_type=content_type_temp,
)
class CustomRegistrationView(RegistrationView):
"""
@ -180,10 +194,11 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin, FormView):
"""
Функция установки ролей пользователям
"""
users = form.cleaned_data['users']
if 'engineer' in self.request.POST:
self.make_engineers(form.cleaned_data['users'])
self.make_engineers(users)
elif 'light_agent' in self.request.POST:
self.make_light_agents(form.cleaned_data['users'])
self.make_light_agents(users)
return super().form_valid(form)
@staticmethod
@ -194,32 +209,15 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin, FormView):
def make_light_agents(users):
[make_light_agent(user) for user in users]
@staticmethod
def count_users(users) -> tuple:
"""
Функция подсчета количества сотрудников с ролями engineer и light_a
.. 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) -> dict:
"""
Функция формирования контента страницы администратора (с проверкой прав доступа)
"""
if self.request.user.userprofile.role != 'admin':
raise PermissionDenied
context = super().get_context_data(**kwargs)
context['users'] = get_list_or_404(
users = get_list_or_404(
UserProfile, role='agent')
context['engineers'], context['light_agents'] = self.count_users(get_users_list())
context['users'] = users
context['engineers'], context['light_agents'] = count_users(get_users_list())
return context # TODO: need to get profile page url
@ -230,6 +228,26 @@ class CustomLoginView(LoginView):
form_class = CustomAuthenticationForm
class UsersViewSet(viewsets.ReadOnlyModelViewSet):
"""
Класс для получения пользователей с помощью api
"""
queryset = UserProfile.objects.filter(role='agent')
serializer_class = ProfileSerializer
def list(self, request, *args, **kwargs):
users = update_users_in_model().values
count = count_users(users)
profiles = UserProfile.objects.filter(role='agent')
serializer = self.get_serializer(profiles, many=True)
return Response({
'users': serializer.data,
'engineers': count[0],
'light_agents': count[1]
})
@login_required()
def statistic_page(request):
if not request.user.is_superuser:

View File

@ -3,6 +3,8 @@ Django==3.1.6
Pillow==8.1.0
zenpy~=2.0.24
django_registration==3.1.1
djangorestframework==3.12.2
# Documentation
Sphinx==3.4.3

View File

@ -1,9 +1,83 @@
"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);
function move_checkboxes() {
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);
}
} else {
alert(
"Количество пользователей агентов не соответствует количеству полей в форме AdminPageUsers"
);
}
}
move_checkboxes();
// React
class TableRow extends React.Component {
render() {
return (
<tr>
<td>
<a href="#">{this.props.user.name}</a>
</td>
<td>{this.props.user.user.email}</td>
<td>{this.props.user.role}</td>
<td>
<input
type="checkbox"
value={this.props.user.id}
className="form-check-input"
name="users"
/>
</td>
</tr>
);
}
}
class TableBody extends React.Component {
constructor(props) {
super(props);
this.state = {
users: [],
engineers: 0,
light_agents: 0,
};
}
get_users() {
axios.get("/api/users").then((response) => {
this.setState({
users: response.data.users,
engineers: response.data.engineers,
light_agents: response.data.light_agents,
});
let elements = document.querySelectorAll(".info-quantity-value");
elements[0].innerHTML = this.state.engineers;
elements[1].innerHTML = this.state.light_agents;
});
}
componentDidMount() {
this.interval = setInterval(() => {
this.get_users();
}, 10000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return this.state.users.map((user, key) => (
<TableRow user={user} key={key} />
));
}
}
ReactDOM.render(<TableBody />, document.getElementById("new_tbody"));
setTimeout(() => document.getElementById("old_tbody").remove(), 10000);