diff --git a/access_controller/urls.py b/access_controller/urls.py index 9550e6b..d8611f2 100644 --- a/access_controller/urls.py +++ b/access_controller/urls.py @@ -36,8 +36,9 @@ urlpatterns = [ path('work/become_engineer/', work_become_engineer, name="work_become_engineer"), path('accounts/login/', include('django.contrib.auth.urls')), path('control/', AdminPageView.as_view(), name='control'), - path('statistic/', statistic_page, name='statistic') -] + path('statistic/', statistic_page, name='statistic'), + ] + urlpatterns += [ path( diff --git a/main/migrations/0017_auto_20210408_1943.py b/main/migrations/0017_auto_20210408_1943.py new file mode 100644 index 0000000..4db02cf --- /dev/null +++ b/main/migrations/0017_auto_20210408_1943.py @@ -0,0 +1,26 @@ +# Generated by Django 3.1.7 on 2021-04-08 16:43 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('main', '0016_merge_20210330_0043'), + ] + + operations = [ + migrations.AlterField( + model_name='unassignedticket', + name='assignee', + field=models.ForeignKey(help_text='Пользователь, с которого снят тикет', on_delete=django.db.models.deletion.CASCADE, related_name='tickets', to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='unassignedticket', + name='status', + field=models.IntegerField(choices=[(0, 'Снят с пользователя, перенесён в буферную группу'), (1, 'Авторство восстановлено'), (2, 'Пока нас не было, тикет испарился из буферной группы. Дополнительные действия не требуются'), (3, 'Тикет уже был закрыт. Дополнительные действия не требуются'), (4, 'Тикет решён. Записан на пользователя с почтой SOLVED_TICKETS_EMAIL')], default=0, help_text='Статус тикета'), + ), + ] diff --git a/main/templates/base/menu.html b/main/templates/base/menu.html index 92aaec1..93799a5 100644 --- a/main/templates/base/menu.html +++ b/main/templates/base/menu.html @@ -5,23 +5,48 @@ diff --git a/main/views.py b/main/views.py index 0e31a5d..2202cb7 100644 --- a/main/views.py +++ b/main/views.py @@ -16,7 +16,7 @@ from django.contrib.messages.views import SuccessMessageMixin from django.core.exceptions import PermissionDenied from django.core.handlers.wsgi import WSGIRequest from django.http import HttpResponseRedirect, HttpResponse -from django.shortcuts import render, redirect +from django.shortcuts import render, redirect, get_list_or_404 from django.urls import reverse_lazy, reverse from django.views.generic import FormView from django_registration.views import RegistrationView @@ -34,6 +34,19 @@ from main.serializers import ProfileSerializer, ZendeskUserSerializer from .models import UserProfile +def setup_context(profile_lit: bool = False, control_lit: bool = False, work_lit: bool = False, + registration_lit: bool = False, login_lit: bool = False): + print(profile_lit, control_lit, work_lit, registration_lit, login_lit) + context = { + 'profile_lit': profile_lit, + 'control_lit': control_lit, + 'work_lit': work_lit, + 'registration_lit': registration_lit, + 'login_lit': login_lit, + } + return context + + class CustomRegistrationView(RegistrationView): """ Отображение и логика работы страницы регистрации пользователя. @@ -47,6 +60,7 @@ class CustomRegistrationView(RegistrationView): :param is_allowed: Определение зарегистрирован ли пользователь с введенным email на Zendesk и принадлежит ли он к организации SYSTEM :type is_allowed: :class:`bool` """ + extra_context = setup_context(registration_lit=True) form_class = CustomRegistrationForm template_name = 'django_registration/registration_form.html' urls = { @@ -138,11 +152,12 @@ def profile_page(request: WSGIRequest) -> HttpResponse: """ user_profile: UserProfile = request.user.userprofile update_profile(user_profile) - context = { + context = setup_context(profile_lit=True) + context.update({ 'profile': user_profile, 'pagename': 'Страница профиля', 'ZENDESK_ROLES': ZENDESK_ROLES, - } + }) return render(request, 'pages/profile.html', context) @@ -178,13 +193,14 @@ def work_page(request: WSGIRequest, id: int) -> HttpResponse: elif user.custom_role_id == ZENDESK_ROLES['light_agent']: light_agents.append(user) - context = { + context = setup_context(work_lit=True) + context.update({ 'engineers': engineers, 'agents': light_agents, 'messages': messages.get_messages(request), 'licences_remaining': max(0, ZENDESK_MAX_AGENTS - len(engineers)), 'pagename': 'Управление правами' - } + }) return render(request, 'pages/work.html', context) return redirect("login") @@ -299,16 +315,33 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageM """ Функция формирования контента страницы администратора (с проверкой прав доступа) """ - context = super().get_context_data(**kwargs) + + # context = super().get_context_data(**kwargs) + + # context['licences_remaining'] = max(0, ZENDESK_MAX_AGENTS - context['engineers']) + # return context + + context = setup_context(control_lit=True) + context.update(super().get_context_data(**kwargs)) + users = get_list_or_404( + UserProfile, role='agent') context['engineers'], context['light_agents'] = count_users(get_users_list()) - context['licences_remaining'] = max(0, ZENDESK_MAX_AGENTS - context['engineers']) - return context + context.update({ + 'users': users, + 'ZENDESK_ROLES': ZENDESK_ROLES, + 'engineers': context['engineers'], + 'light_agents': context['light_agents'], + 'licences_remaining': max(0, ZENDESK_MAX_AGENTS - context['engineers']), + }) + return context # TODO: need to get profile page url + class CustomLoginView(LoginView): """ Отображение страницы авторизации пользователя """ + extra_context = setup_context(login_lit=True) form_class = CustomAuthenticationForm @@ -358,12 +391,18 @@ def statistic_page(request: WSGIRequest) -> HttpResponse: :param request: данные о пользователе: email, время и интервал работы. Данные получаем через forms.StatisticForm :return: адресация на страницу статистики """ - if not request.user.has_perm('main.has_control_access'): - raise PermissionDenied - context = { + + # if not request.user.has_perm('main.has_control_access'): + # raise PermissionDenied + # context = { + + if not request.user.is_superuser: + return redirect('index') + context = setup_context() + context.update({ 'pagename': 'страница статистики', 'errors': list(), - } + }) if request.method == "POST": form = StatisticForm(request.POST) if form.is_valid():