Fix permission management

This commit is contained in:
papi 2024-01-19 10:54:18 +03:00
parent c96a56f6c9
commit 5ae45d2201
4 changed files with 85 additions and 61 deletions

View File

@ -1,54 +1,78 @@
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import Http404
from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import redirect
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
def admin_required( 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, Decorator for views that checks that the logged-in user is a superuser,
redirects to the log-in page if necessary. redirects to the specified URL if necessary.
""" """
actual_decorator = user_passes_test(
lambda u: u.is_active and u.is_superuser, # Define the test function: checks if the user is active and a superuser
login_url=login_url, def test_func(user):
redirect_field_name=redirect_field_name, return user.is_active and user.is_superuser
)
if function: # Define the wrapper function to handle the response
return actual_decorator(function) def wrapper(request, *args, **kwargs):
return actual_decorator 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

View File

@ -22,6 +22,19 @@ def home_view(request):
return render(request, "core/index.html", context) 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 @login_required
def post_add(request): def post_add(request):
if request.method == "POST": if request.method == "POST":
@ -293,16 +306,3 @@ def semester_delete_view(request, pk):
semester.delete() semester.delete()
messages.success(request, "Semester successfully deleted") messages.success(request, "Semester successfully deleted")
return redirect("semester_list") 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)

View File

@ -270,7 +270,7 @@
<div class="card w-100 h-100 p-3"> <div class="card w-100 h-100 p-3">
<h5>Latest activities</h5> <h5>Latest activities</h5>
<ul class="ps-2 small"> <ul class="ps-2 small">
{% for log in logs %}d-flex {% for log in logs %}
<li>{{ log.message }} <span class="text-muted">- {{ log.created_at }}</span></li> <li>{{ log.message }} <span class="text-muted">- {{ log.created_at }}</span></li>
{% empty %} {% empty %}
<li>No recent activity</li> <li>No recent activity</li>

View File

@ -26,7 +26,7 @@
</div> </div>
<br> <br>
<h4 class="title-1">Submit score for {{ course }} Students</h4> <h4 class="title-1">Students result form | {{ course|truncatechars:15 }}</h4>
<p>{{ course.summary }}</p> <p>{{ course.summary }}</p>
{% include 'snippets/messages.html' %} {% include 'snippets/messages.html' %}