diff --git a/accounts/decorators.py b/accounts/decorators.py index 162aa1b..12aeded 100644 --- a/accounts/decorators.py +++ b/accounts/decorators.py @@ -1,54 +1,78 @@ from django.contrib.auth import REDIRECT_FIELD_NAME -from django.http import Http404 from django.contrib.auth.decorators import user_passes_test - - -def student_required( - function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=Http404 -): - """ - Decorator for views that checks that the logged in user is a student, - redirects to the log-in page if necessary. - """ - actual_decorator = user_passes_test( - lambda u: u.is_active and u.is_student or u.is_superuser, - login_url=login_url, - redirect_field_name=redirect_field_name, - ) - if function: - return actual_decorator(function) - return actual_decorator - - -def lecturer_required( - function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=Http404 -): - """ - Decorator for views that checks that the logged in user is a teacher, - redirects to the log-in page if necessary. - """ - actual_decorator = user_passes_test( - lambda u: u.is_active and u.is_lecturer or u.is_superuser, - login_url=login_url, - redirect_field_name=redirect_field_name, - ) - if function: - return actual_decorator(function) - return actual_decorator +from django.shortcuts import redirect def admin_required( - function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=Http404 + function=None, + redirect_to="/", ): """ - Decorator for views that checks that the logged in user is a teacher, - redirects to the log-in page if necessary. + Decorator for views that checks that the logged-in user is a superuser, + redirects to the specified URL if necessary. """ - actual_decorator = user_passes_test( - lambda u: u.is_active and u.is_superuser, - login_url=login_url, - redirect_field_name=redirect_field_name, - ) - if function: - return actual_decorator(function) - return actual_decorator + + # Define the test function: checks if the user is active and a superuser + def test_func(user): + return user.is_active and user.is_superuser + + # Define the wrapper function to handle the response + def wrapper(request, *args, **kwargs): + if test_func(request.user): + # Call the original function if the user passes the test + return function(request, *args, **kwargs) if function else None + else: + # Redirect to the specified URL if the user fails the test + return redirect(redirect_to) + + return wrapper if function else test_func + + +def lecturer_required( + function=None, + redirect_to="/", +): + """ + Decorator for views that checks that the logged-in user is a superuser, + redirects to the specified URL if necessary. + """ + + # Define the test function: checks if the user is active and a superuser + def test_func(user): + return user.is_active and user.is_lecturer or user.is_superuser + + # Define the wrapper function to handle the response + def wrapper(request, *args, **kwargs): + if test_func(request.user): + # Call the original function if the user passes the test + return function(request, *args, **kwargs) if function else None + else: + # Redirect to the specified URL if the user fails the test + return redirect(redirect_to) + + return wrapper if function else test_func + + +def student_required( + function=None, + redirect_to="/", +): + """ + Decorator for views that checks that the logged-in user is a superuser, + redirects to the specified URL if necessary. + """ + + # Define the test function: checks if the user is active and a superuser + def test_func(user): + return user.is_active and user.is_student or user.is_superuser + + # Define the wrapper function to handle the response + def wrapper(request, *args, **kwargs): + if test_func(request.user): + # Call the original function if the user passes the test + return function(request, *args, **kwargs) if function else None + else: + # Redirect to the specified URL if the user fails the test + return redirect(redirect_to) + + return wrapper if function else test_func diff --git a/core/views.py b/core/views.py index e2da465..e2dceeb 100644 --- a/core/views.py +++ b/core/views.py @@ -22,6 +22,19 @@ def home_view(request): return render(request, "core/index.html", context) +@login_required +@admin_required +def dashboard_view(request): + logs = ActivityLog.objects.all().order_by("-created_at")[:10] + context = { + "student_count": User.get_student_count(), + "lecturer_count": User.get_lecturer_count(), + "superuser_count": User.get_superuser_count(), + "logs": logs, + } + return render(request, "core/dashboard.html", context) + + @login_required def post_add(request): if request.method == "POST": @@ -293,16 +306,3 @@ def semester_delete_view(request, pk): semester.delete() messages.success(request, "Semester successfully deleted") return redirect("semester_list") - - -@login_required -@admin_required -def dashboard_view(request): - logs = ActivityLog.objects.all().order_by("-created_at")[:10] - context = { - "student_count": User.get_student_count(), - "lecturer_count": User.get_lecturer_count(), - "superuser_count": User.get_superuser_count(), - "logs": logs, - } - return render(request, "core/dashboard.html", context) diff --git a/templates/core/dashboard.html b/templates/core/dashboard.html index 716f066..d872b18 100644 --- a/templates/core/dashboard.html +++ b/templates/core/dashboard.html @@ -270,7 +270,7 @@
{{ course.summary }}
{% include 'snippets/messages.html' %}