Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login mixins #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions django_school/classroom/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Mixins for class-based views,
that check user auth. and role
"""

from django.contrib.auth.mixins import UserPassesTestMixin


class StudentRequiredMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_authenticated and self.request.user.is_student


class TeacherRequiredMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_authenticated and self.request.user.is_teacher
10 changes: 4 additions & 6 deletions django_school/classroom/views/students.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..decorators import student_required
from ..forms import StudentInterestsForm, StudentSignUpForm, TakeQuizForm
from ..models import Quiz, Student, TakenQuiz, User
from ..mixins import StudentRequiredMixin


class StudentSignUpView(CreateView):
Expand All @@ -28,8 +29,7 @@ def form_valid(self, form):
return redirect('students:quiz_list')


@method_decorator([login_required, student_required], name='dispatch')
class StudentInterestsView(UpdateView):
class StudentInterestsView(StudentRequiredMixin, UpdateView):
model = Student
form_class = StudentInterestsForm
template_name = 'classroom/students/interests_form.html'
Expand All @@ -43,8 +43,7 @@ def form_valid(self, form):
return super().form_valid(form)


@method_decorator([login_required, student_required], name='dispatch')
class QuizListView(ListView):
class QuizListView(StudentRequiredMixin, ListView):
model = Quiz
ordering = ('name', )
context_object_name = 'quizzes'
Expand All @@ -61,8 +60,7 @@ def get_queryset(self):
return queryset


@method_decorator([login_required, student_required], name='dispatch')
class TakenQuizListView(ListView):
class TakenQuizListView(StudentRequiredMixin, ListView):
model = TakenQuiz
context_object_name = 'taken_quizzes'
template_name = 'classroom/students/taken_quiz_list.html'
Expand Down
19 changes: 7 additions & 12 deletions django_school/classroom/views/teachers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..decorators import teacher_required
from ..forms import BaseAnswerInlineFormSet, QuestionForm, TeacherSignUpForm
from ..models import Answer, Question, Quiz, User
from ..mixins import TeacherRequiredMixin


class TeacherSignUpView(CreateView):
Expand All @@ -30,8 +31,7 @@ def form_valid(self, form):
return redirect('teachers:quiz_change_list')


@method_decorator([login_required, teacher_required], name='dispatch')
class QuizListView(ListView):
class QuizListView(TeacherRequiredMixin,ListView):
model = Quiz
ordering = ('name', )
context_object_name = 'quizzes'
Expand All @@ -45,8 +45,7 @@ def get_queryset(self):
return queryset


@method_decorator([login_required, teacher_required], name='dispatch')
class QuizCreateView(CreateView):
class QuizCreateView(TeacherRequiredMixin, CreateView):
model = Quiz
fields = ('name', 'subject', )
template_name = 'classroom/teachers/quiz_add_form.html'
Expand All @@ -59,8 +58,7 @@ def form_valid(self, form):
return redirect('teachers:quiz_change', quiz.pk)


@method_decorator([login_required, teacher_required], name='dispatch')
class QuizUpdateView(UpdateView):
class QuizUpdateView(TeacherRequiredMixin, UpdateView):
model = Quiz
fields = ('name', 'subject', )
context_object_name = 'quiz'
Expand All @@ -82,8 +80,7 @@ def get_success_url(self):
return reverse('teachers:quiz_change', kwargs={'pk': self.object.pk})


@method_decorator([login_required, teacher_required], name='dispatch')
class QuizDeleteView(DeleteView):
class QuizDeleteView(TeacherRequiredMixin, DeleteView):
model = Quiz
context_object_name = 'quiz'
template_name = 'classroom/teachers/quiz_delete_confirm.html'
Expand All @@ -98,8 +95,7 @@ def get_queryset(self):
return self.request.user.quizzes.all()


@method_decorator([login_required, teacher_required], name='dispatch')
class QuizResultsView(DetailView):
class QuizResultsView(TeacherRequiredMixin, DetailView):
model = Quiz
context_object_name = 'quiz'
template_name = 'classroom/teachers/quiz_results.html'
Expand Down Expand Up @@ -188,8 +184,7 @@ def question_change(request, quiz_pk, question_pk):
})


@method_decorator([login_required, teacher_required], name='dispatch')
class QuestionDeleteView(DeleteView):
class QuestionDeleteView(TeacherRequiredMixin, DeleteView):
model = Question
context_object_name = 'question'
template_name = 'classroom/teachers/question_delete_confirm.html'
Expand Down