diff --git a/generic/decorators.py b/generic/decorators.py new file mode 100755 index 00000000..f7395d29 --- /dev/null +++ b/generic/decorators.py @@ -0,0 +1,13 @@ +from django.core.exceptions import PermissionDenied +from .models import Exam + +def user_is_exam_author_or_exam_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='exam_checker').exists(): + return function(request, *args, **kwargs) + else: + raise PermissionDenied + wrap.__doc__ = function.__doc__ + wrap.__name__ = function.__name__ + return wrap \ No newline at end of file diff --git a/generic/views.py b/generic/views.py index 122ad222..3c3f442e 100644 --- a/generic/views.py +++ b/generic/views.py @@ -23,6 +23,8 @@ from .forms import ( ExaminationForm, ) +from .decorators import user_is_exam_author_or_exam_checker + from .models import Examination from rapids.models import Rapid as RapidQuestion @@ -202,6 +204,7 @@ class ExamViews(View, LoginRequiredMixin): @method_decorator(login_required) + @method_decorator(user_is_exam_author_or_exam_checker) def exam_overview(self, request, pk): #print(Exam.objects.all()) #exams = Exam.objects.all() diff --git a/rapids/decorators.py b/rapids/decorators.py index 72c1c36b..c37c63c9 100755 --- a/rapids/decorators.py +++ b/rapids/decorators.py @@ -1,5 +1,5 @@ from django.core.exceptions import PermissionDenied -from .models import Rapid +from .models import Exam def user_is_author_or_rapid_checker(function): def wrap(request, *args, **kwargs): @@ -11,3 +11,15 @@ def user_is_author_or_rapid_checker(function): wrap.__doc__ = function.__doc__ wrap.__name__ = function.__name__ return wrap + + +def user_is_exam_author_or_rapid_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='rapid_checker').exists(): + return function(request, *args, **kwargs) + else: + raise PermissionDenied + wrap.__doc__ = function.__doc__ + wrap.__name__ = function.__name__ + return wrap \ No newline at end of file