Fix permission management
This commit is contained in:
parent
c96a56f6c9
commit
5ae45d2201
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -270,7 +270,7 @@
|
||||
<div class="card w-100 h-100 p-3">
|
||||
<h5>Latest activities</h5>
|
||||
<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>
|
||||
{% empty %}
|
||||
<li>No recent activity</li>
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
</div>
|
||||
|
||||
<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>
|
||||
|
||||
{% include 'snippets/messages.html' %}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user