Added button highlights depending on current page

This commit is contained in:
Vadim Melnikov 2021-04-01 18:12:13 +03:00
parent 4dd48a5e4b
commit 94b5061301
3 changed files with 42 additions and 15 deletions

View File

@ -19,7 +19,7 @@ from django.urls import path, include
from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView from main.views import main_page, profile_page, CustomRegistrationView, CustomLoginView
from main.views import work_page, work_hand_over, work_become_engineer, \ 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 from main.urls import router
@ -37,7 +37,7 @@ urlpatterns = [
path('accounts/', include('django_registration.backends.activation.urls')), path('accounts/', include('django_registration.backends.activation.urls')),
path('accounts/login/', include('django.contrib.auth.urls')), path('accounts/login/', include('django.contrib.auth.urls')),
path('control/', AdminPageView.as_view(), name='control'), path('control/', AdminPageView.as_view(), name='control'),
path('statistic/', statistic_page, name='statistic') path('statistic/', statistic_page, name='statistic'),
] ]
urlpatterns += [ urlpatterns += [

View File

@ -10,11 +10,23 @@
</a> </a>
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}
<div class="btn-group" role="group" aria-label="Basic example"> <div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-secondary" href="{% url 'profile' %}">Профиль</a> {% if profile_lit %}
{% if perms.main.has_control_access %} <a class="btn btn-primary" href="{% url 'profile' %}">Профиль</a>
<a class="btn btn-secondary" href="{% url 'control' %}">Управление</a>
{% else %} {% else %}
<a class="btn btn-secondary" href="{% url 'work' request.user.id %}">Запрос прав</a> <a class="btn btn-secondary" href="{% url 'profile' %}">Профиль</a>
{% endif %}
{% if perms.main.has_control_access %}
{% if control_lit %}
<a class="btn btn-primary" href="{% url 'control' %}">Управление</a>
{% else %}
<a class="btn btn-secondary" href="{% url 'control' %}">Управление</a>
{% endif %}
{% else %}
{% if work_lit %}
<a class="btn btn-primary" href="{% url 'work' request.user.id %}">Запрос прав</a>
{% else %}
<a class="btn btn-secondary" href="{% url 'work' request.user.id %}">Запрос прав</a>
{% endif %}
{% endif %} {% endif %}
<a class="btn btn-secondary" href="{% url 'logout' %}">Выйти</a> <a class="btn btn-secondary" href="{% url 'logout' %}">Выйти</a>
</div> </div>

View File

@ -121,6 +121,15 @@ class CustomRegistrationView(RegistrationView):
return reverse_lazy('django_registration_disallowed') 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() @login_required()
def profile_page(request: WSGIRequest) -> HttpResponse: def profile_page(request: WSGIRequest) -> HttpResponse:
""" """
@ -131,11 +140,12 @@ def profile_page(request: WSGIRequest) -> HttpResponse:
""" """
user_profile: UserProfile = request.user.userprofile user_profile: UserProfile = request.user.userprofile
update_profile(user_profile) update_profile(user_profile)
context = { context = setup_context(profile_lit=True)
context.update({
'profile': user_profile, 'profile': user_profile,
'pagename': 'Страница профиля', 'pagename': 'Страница профиля',
'ZENDESK_ROLES': ZENDESK_ROLES, 'ZENDESK_ROLES': ZENDESK_ROLES,
} })
return render(request, 'pages/profile.html', context) 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']: elif user.custom_role_id == ZENDESK_ROLES['light_agent']:
light_agents.append(user) light_agents.append(user)
context = { context = setup_context(work_lit=True)
context.update({
'engineers': engineers, 'engineers': engineers,
'agents': light_agents, 'agents': light_agents,
'messages': messages.get_messages(request), 'messages': messages.get_messages(request),
'licences_remaining': max(0, ZENDESK_MAX_AGENTS - len(engineers)), 'licences_remaining': max(0, ZENDESK_MAX_AGENTS - len(engineers)),
'pagename': 'Управление правами' 'pagename': 'Управление правами'
} })
return render(request, 'pages/work.html', context) return render(request, 'pages/work.html', context)
return redirect("login") 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( users = get_list_or_404(
UserProfile, role='agent') UserProfile, role='agent')
context['users'] = users context.update({
context['ZENDESK_ROLES'] = ZENDESK_ROLES 'users': users,
context['engineers'], context['light_agents'] = count_users(get_users_list()) 'ZENDESK_ROLES': ZENDESK_ROLES,
context['licences_remaining'] = max(0, ZENDESK_MAX_AGENTS - context['engineers']) '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 return context # TODO: need to get profile page url