From 94b5061301206898c1f964e3d7952ea948979a02 Mon Sep 17 00:00:00 2001 From: Vadim Melnikov Date: Thu, 1 Apr 2021 18:12:13 +0300 Subject: [PATCH 1/4] Added button highlights depending on current page --- access_controller/urls.py | 4 ++-- main/templates/base/menu.html | 20 ++++++++++++++++---- main/views.py | 33 ++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/access_controller/urls.py b/access_controller/urls.py index e174717..ec94163 100644 --- a/access_controller/urls.py +++ b/access_controller/urls.py @@ -19,7 +19,7 @@ from django.urls import path, include 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 + AdminPageView, statistic_page, testing_page from main.urls import router @@ -37,7 +37,7 @@ urlpatterns = [ 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') + path('statistic/', statistic_page, name='statistic'), ] urlpatterns += [ diff --git a/main/templates/base/menu.html b/main/templates/base/menu.html index 92aaec1..cdf13bd 100644 --- a/main/templates/base/menu.html +++ b/main/templates/base/menu.html @@ -10,11 +10,23 @@ {% if request.user.is_authenticated %}
- Профиль - {% if perms.main.has_control_access %} - Управление + {% if profile_lit %} + Профиль {% else %} - Запрос прав + Профиль + {% endif %} + {% if perms.main.has_control_access %} + {% if control_lit %} + Управление + {% else %} + Управление + {% endif %} + {% else %} + {% if work_lit %} + Запрос прав + {% else %} + Запрос прав + {% endif %} {% endif %} Выйти
diff --git a/main/views.py b/main/views.py index 18d7e52..9951dcf 100644 --- a/main/views.py +++ b/main/views.py @@ -121,6 +121,15 @@ class CustomRegistrationView(RegistrationView): return reverse_lazy('django_registration_disallowed') +def setup_context(profile_lit: bool = False, control_lit: bool = False, work_lit: bool = False): + context = { + 'profile_lit': profile_lit, + 'control_lit': control_lit, + 'work_lit': work_lit, + } + return context + + @login_required() def profile_page(request: WSGIRequest) -> HttpResponse: """ @@ -131,11 +140,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) @@ -171,13 +181,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") @@ -284,13 +295,17 @@ class AdminPageView(LoginRequiredMixin, PermissionRequiredMixin,SuccessMessageMi """ Функция формирования контента страницы администратора (с проверкой прав доступа) """ - context = super().get_context_data(**kwargs) + context = setup_context(control_lit=True) + context.update(super().get_context_data(**kwargs)) users = get_list_or_404( UserProfile, role='agent') - context['users'] = users - context['ZENDESK_ROLES'] = ZENDESK_ROLES - context['engineers'], context['light_agents'] = count_users(get_users_list()) - context['licences_remaining'] = max(0, ZENDESK_MAX_AGENTS - context['engineers']) + context.update({ + 'users': users, + 'ZENDESK_ROLES': ZENDESK_ROLES, + 'engineers': count_users(get_users_list()), + 'light_agents': count_users(get_users_list()), + 'licences_remaining': max(0, ZENDESK_MAX_AGENTS - context['engineers']), + }) return context # TODO: need to get profile page url From ef646a7b09fba91c4540e34e58841cbde555add7 Mon Sep 17 00:00:00 2001 From: Vadim Melnikov Date: Thu, 1 Apr 2021 18:24:49 +0300 Subject: [PATCH 2/4] Deleted the testing leftovers --- access_controller/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/access_controller/urls.py b/access_controller/urls.py index ec94163..4ace220 100644 --- a/access_controller/urls.py +++ b/access_controller/urls.py @@ -19,7 +19,7 @@ from django.urls import path, include 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, testing_page + AdminPageView, statistic_page from main.urls import router From 2e6db096d649c44348f7d02579354ed5f8bfbff8 Mon Sep 17 00:00:00 2001 From: Vadim Melnikov Date: Thu, 8 Apr 2021 16:18:25 +0300 Subject: [PATCH 3/4] Refactored menu.html so there's no copypaste --- main/templates/base/menu.html | 39 +++++++++++++++++++---------------- main/views.py | 5 +++-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/main/templates/base/menu.html b/main/templates/base/menu.html index cdf13bd..a56b4be 100644 --- a/main/templates/base/menu.html +++ b/main/templates/base/menu.html @@ -5,33 +5,36 @@ diff --git a/main/views.py b/main/views.py index 4952b60..b3efaa1 100644 --- a/main/views.py +++ b/main/views.py @@ -35,6 +35,19 @@ from main.serializers import ProfileSerializer 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): """ Отображение и логика работы страницы регистрации пользователя. @@ -48,6 +61,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' success_url = reverse_lazy('django_registration_complete') @@ -121,15 +135,6 @@ class CustomRegistrationView(RegistrationView): return reverse_lazy('django_registration_disallowed') -def setup_context(profile_lit: bool = False, control_lit: bool = False, work_lit: bool = False): - context = { - 'profile_lit': profile_lit, - 'control_lit': control_lit, - 'work_lit': work_lit, - } - return context - - @login_required() def profile_page(request: WSGIRequest) -> HttpResponse: """ @@ -313,6 +318,7 @@ class CustomLoginView(LoginView): """ Отображение страницы авторизации пользователя """ + extra_context = setup_context(login_lit=True) form_class = CustomAuthenticationForm