Skip to content

Commit

Permalink
refactor accounts views
Browse files Browse the repository at this point in the history
  • Loading branch information
TareqMonwer committed Nov 3, 2024
1 parent 7d05b26 commit 84afc67
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
2 changes: 0 additions & 2 deletions django_school_management/academics/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
),
path('depts/delete/<int:pk>/', 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/<int:pk>/', views.UpdateDepartment.as_view(),
name='update_department'),
path('subjects/', views.subject_list, name='subject_list'),
Expand Down
24 changes: 1 addition & 23 deletions django_school_management/academics/views.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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):
'''
Expand Down
4 changes: 4 additions & 0 deletions django_school_management/accounts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class AccountTypesEnum(Enum):


class AccountURLEnums(Enum):
all_accounts = 'accounts/'
add_user = 'add_user/'
approval_with_modification = 'modify-and-approve/<int:pk>/'
dashboard = 'dashboard/'
groups = 'groups/'
Expand All @@ -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}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item">
<a href="{% url 'academics:add_user' %}">add new user</a>
<a href="{% url accounts_url.add_user %}">add new user</a>
</li>
</ul>
</div> <!-- /sidebar -->
Expand Down
10 changes: 10 additions & 0 deletions django_school_management/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
]
26 changes: 25 additions & 1 deletion django_school_management/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .forms import (
ProfileCompleteForm,
ApprovalProfileUpdateForm,
UserChangeFormDashboard
UserChangeFormDashboard, UserCreateFormDashboard
)
from .mixins.no_permission import LoginRequiredNoPermissionMixin
from .models import CustomGroup, User
Expand Down Expand Up @@ -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
Expand All @@ -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`.
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions templates/_sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ <h5 class="lh-1 mB-0 logo-text">
{% if perms.auth.add_user %}
<li>
<a class="sidebar-link"
href="{% url 'academics:add_user' %}">
href="{% url account_urls.add_user %}">
Add People
</a>
</li>
Expand All @@ -286,7 +286,7 @@ <h5 class="lh-1 mB-0 logo-text">
{% endif %}
<li>
<a class="sidebar-link"
href="{% url 'academics:all_accounts' %}">
href="{% url account_urls.all_accounts %}">
Users
</a>
</li>
Expand Down

0 comments on commit 84afc67

Please sign in to comment.