From 84afc678ffffb4298cb5446a6dc3f5c09cc6ad1f Mon Sep 17 00:00:00 2001 From: TareqMonwer Date: Mon, 4 Nov 2024 03:21:35 +0600 Subject: [PATCH] refactor accounts views --- django_school_management/academics/urls.py | 2 -- django_school_management/academics/views.py | 24 +---------------- .../accounts/constants.py | 4 +++ .../account/dashboard/accounts_list.html | 2 +- django_school_management/accounts/urls.py | 10 +++++++ django_school_management/accounts/views.py | 26 ++++++++++++++++++- templates/_sidebar.html | 4 +-- 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/django_school_management/academics/urls.py b/django_school_management/academics/urls.py index f662d5e..fc11f0d 100644 --- a/django_school_management/academics/urls.py +++ b/django_school_management/academics/urls.py @@ -23,8 +23,6 @@ ), path('depts/delete//', views.delete_department, name='delete_dept'), path('academic_sessions/', views.academic_session, name='academic_sessions'), - path('add_user/', views.add_user_view, name='add_user'), - path('accounts/', AccountListView.as_view(), name='all_accounts'), path('department/update//', views.UpdateDepartment.as_view(), name='update_department'), path('subjects/', views.subject_list, name='subject_list'), diff --git a/django_school_management/academics/views.py b/django_school_management/academics/views.py index b98fc96..b91e86b 100644 --- a/django_school_management/academics/views.py +++ b/django_school_management/academics/views.py @@ -1,16 +1,14 @@ import csv, io from django.shortcuts import render, redirect, get_object_or_404 -from django.contrib.auth.decorators import login_required, user_passes_test +from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.views.generic.edit import CreateView, UpdateView from django.views.generic import ListView from django.urls import reverse_lazy -from rolepermissions.roles import assign_role from .models import (Semester, Department, AcademicSession, Subject) from .forms import SemesterForm, DepartmentForm, AcademicSessionForm -from django_school_management.accounts.forms import UserCreateFormDashboard from permission_handlers.administrative import ( user_is_admin_su_editor_or_ac_officer, user_editor_admin_or_su, @@ -19,26 +17,6 @@ from permission_handlers.basic import user_is_verified -@login_required -def add_user_view(request): - if request.user.has_perm('create_stuff'): - if request.method == 'POST': - user_form = UserCreateFormDashboard(request.POST) - if user_form.is_valid(): - user = user_form.save() - return redirect( - user.get_author_url() - ) - else: - user_form = UserCreateFormDashboard() - context = { - 'user_form': user_form, - } - return render(request, 'academics/add_user.html', context) - else: - return render(request, 'academics/permission_required.html') - - @user_passes_test(user_is_admin_su_editor_or_ac_officer) def semesters(request): ''' diff --git a/django_school_management/accounts/constants.py b/django_school_management/accounts/constants.py index eb6e6a3..7cb22f2 100644 --- a/django_school_management/accounts/constants.py +++ b/django_school_management/accounts/constants.py @@ -18,6 +18,8 @@ class AccountTypesEnum(Enum): class AccountURLEnums(Enum): + all_accounts = 'accounts/' + add_user = 'add_user/' approval_with_modification = 'modify-and-approve//' dashboard = 'dashboard/' groups = 'groups/' @@ -30,6 +32,8 @@ class AccountURLEnums(Enum): class AccountURLConstants: + all_accounts = f'account:{AccountURLEnums.all_accounts.name}' + add_user = f'account:{AccountURLEnums.add_user.name}' approval_with_modification = f'account:{AccountURLEnums.approval_with_modification.name}' dashboard = f'account:{AccountURLEnums.dashboard.name}' groups = f'account:{AccountURLEnums.groups.name}' diff --git a/django_school_management/accounts/templates/account/dashboard/accounts_list.html b/django_school_management/accounts/templates/account/dashboard/accounts_list.html index 317eca8..d3b53a7 100644 --- a/django_school_management/accounts/templates/account/dashboard/accounts_list.html +++ b/django_school_management/accounts/templates/account/dashboard/accounts_list.html @@ -43,7 +43,7 @@ diff --git a/django_school_management/accounts/urls.py b/django_school_management/accounts/urls.py index e580fe4..cbf864e 100644 --- a/django_school_management/accounts/urls.py +++ b/django_school_management/accounts/urls.py @@ -52,4 +52,14 @@ views.profile_picture_upload, name=AccountURLEnums.profile_picture_upload.name ), + path( + AccountURLEnums.all_accounts.value, + views.AccountListView.as_view(), + name=AccountURLEnums.all_accounts.name + ), + path( + AccountURLEnums.add_user.value, + views.add_user_view, + name=AccountURLEnums.add_user.name + ), ] diff --git a/django_school_management/accounts/views.py b/django_school_management/accounts/views.py index 0bfbcf0..c430e29 100644 --- a/django_school_management/accounts/views.py +++ b/django_school_management/accounts/views.py @@ -15,7 +15,7 @@ from .forms import ( ProfileCompleteForm, ApprovalProfileUpdateForm, - UserChangeFormDashboard + UserChangeFormDashboard, UserCreateFormDashboard ) from .mixins.no_permission import LoginRequiredNoPermissionMixin from .models import CustomGroup, User @@ -65,6 +65,7 @@ def profile_complete(request): return render(request, 'account/profile_complete.html', ctx) +@login_required(login_url=AccountURLConstants.permission_error) @user_passes_test( can_access_dashboard, login_url=AccountURLConstants.profile_complete @@ -81,6 +82,7 @@ def dashboard(request): return render(request, 'dashboard.html', context) +@login_required(login_url=AccountURLConstants.permission_error) @user_passes_test(user_is_admin_or_su, login_url=AccountURLConstants.permission_error) def user_approval(request, pk, approved): """ Approve or decline approval request based on parameter `approved`. @@ -109,6 +111,7 @@ def user_approval(request, pk, approved): return redirect(AccountURLConstants.user_requests) +@login_required(login_url=AccountURLConstants.permission_error) @user_passes_test(user_is_admin_or_su, login_url=AccountURLConstants.permission_error) def user_approval_with_modification(request, pk): user = User.objects.get(pk=pk) @@ -132,6 +135,27 @@ def user_approval_with_modification(request, pk): return render(request, 'account/modify_approval.html', ctx) +@login_required(login_url=AccountURLConstants.permission_error) +@user_passes_test(user_is_admin_or_su, login_url=AccountURLConstants.permission_error) +def add_user_view(request): + if request.user.has_perm('create_stuff'): + if request.method == 'POST': + user_form = UserCreateFormDashboard(request.POST) + if user_form.is_valid(): + user = user_form.save() + return redirect( + user.get_author_url() + ) + else: + user_form = UserCreateFormDashboard() + context = { + 'user_form': user_form, + } + return render(request, 'academics/add_user.html', context) + else: + return render(request, 'academics/permission_required.html') + + class AccountListView(LoginRequiredNoPermissionMixin, UserPassesTestMixin, ListView): model = User template_name = 'account/dashboard/accounts_list.html' diff --git a/templates/_sidebar.html b/templates/_sidebar.html index ebe3e83..9c692ae 100644 --- a/templates/_sidebar.html +++ b/templates/_sidebar.html @@ -273,7 +273,7 @@
{% if perms.auth.add_user %}
  • + href="{% url account_urls.add_user %}"> Add People
  • @@ -286,7 +286,7 @@
    {% endif %}
  • + href="{% url account_urls.all_accounts %}"> Users