32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
from django.core.exceptions import PermissionDenied
|
|
from .models import Exam, Question
|
|
|
|
def user_is_author_or_shorts_checker(function):
|
|
def wrap(request, *args, **kwargs):
|
|
if "pk" in kwargs:
|
|
question_id = kwargs["pk"]
|
|
elif "question_id" in kwargs:
|
|
question_id = kwargs["question_id"]
|
|
else:
|
|
raise PermissionDenied
|
|
|
|
question = Question.objects.get(pk=question_id)
|
|
if request.user in question.author.all() or request.user.groups.filter(name='shorts_checker').exists():
|
|
return function(request, *args, **kwargs)
|
|
else:
|
|
raise PermissionDenied
|
|
wrap.__doc__ = function.__doc__
|
|
wrap.__name__ = function.__name__
|
|
return wrap
|
|
|
|
|
|
def user_is_exam_author_or_shorts_checker(function):
|
|
def wrap(request, *args, **kwargs):
|
|
exam = Exam.objects.get(pk=kwargs['pk'])
|
|
if request.user in exam.author.all() or request.user.groups.filter(name='shorts_checker').exists():
|
|
return function(request, *args, **kwargs)
|
|
else:
|
|
raise PermissionDenied
|
|
wrap.__doc__ = function.__doc__
|
|
wrap.__name__ = function.__name__
|
|
return wrap |