SkyLearn-Test/accounts/decorators.py
2021-03-25 16:13:17 +03:00

49 lines
1.6 KiB
Python

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
def admin_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_superuser,
login_url=login_url,
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator