我如何限制没有付款的用户不能访问测验部分

时间:2019-05-03 16:24:47

标签: django

class QuizTake(FormView):
form_class = QuestionForm
template_name = 'question.html'
result_template_name = 'result.html'
single_complete_template_name = 'single_complete.html'

def dispatch(self, request, *args, **kwargs):
    self.quiz = get_object_or_404(Quiz, url=self.kwargs['quiz_name'])
    if self.quiz.draft and not request.user.has_perm('quiz.change_quiz'):
        raise PermissionDenied

    try:
        self.logged_in_user = self.request.user.is_authenticated()
    except TypeError:
        self.logged_in_user = self.request.user.is_authenticated

    if self.logged_in_user:
        self.sitting = Sitting.objects.user_sitting(request.user,
                                                    self.quiz)
    else:
        self.sitting = self.anon_load_sitting()

    if self.sitting is False:
        return render(request, self.single_complete_template_name)

    return super(QuizTake, self).dispatch(request, *args, **kwargs)

def get_form(self, *args, **kwargs):
    if self.logged_in_user:
        self.question = self.sitting.get_first_question()
        self.progress = self.sitting.progress()
    else:
        self.question = self.anon_next_question()
        self.progress = self.anon_sitting_progress()

    if self.question.__class__ is Essay_Question:
        form_class = EssayForm
    else:
        form_class = self.form_class

    return form_class(**self.get_form_kwargs())

def get_form_kwargs(self):
    kwargs = super(QuizTake, self).get_form_kwargs()

    return dict(kwargs, question=self.question)

def form_valid(self, form):
    if self.logged_in_user:
        self.form_valid_user(form)
        if self.sitting.get_first_question() is False:
            return self.final_result_user()
    else:
        self.form_valid_anon(form)
        if not self.request.session[self.quiz.anon_q_list()]:
            return self.final_result_anon()

    self.request.POST = {}

    return super(QuizTake, self).get(self, self.request)

上面的代码是我的观点 请为没有付款的用户设置限制,使其无法访问考试部分。

@python_2_unicode_compatible

类测验(models.Model):

title = models.CharField(
    verbose_name=_("Title"),
    max_length=60, blank=False)

description = models.TextField(
    verbose_name=_("Description"),
    blank=True, help_text=_("a description of the quiz"))

url = models.SlugField(
    max_length=60, blank=False,
    help_text=_("a user friendly url"),
    verbose_name=_("user friendly url"))

category = models.ForeignKey(
    Category, null=True, blank=True,
    verbose_name=_("Category"), on_delete=models.CASCADE)

random_order = models.BooleanField(
    blank=False, default=False,
    verbose_name=_("Random Order"),
    help_text=_("Display the questions in "
                "a random order or as they "
                "are set?"))

max_questions = models.PositiveIntegerField(
    blank=True, null=True, verbose_name=_("Max Questions"),
    help_text=_("Number of questions to be answered on each attempt."))

answers_at_end = models.BooleanField(
    blank=False, default=False,
    help_text=_("Correct answer is NOT shown after question."
                " Answers displayed at the end."),
    verbose_name=_("Answers at end"))

exam_paper = models.BooleanField(
    blank=False, default=False,
    help_text=_("If yes, the result of each"
                " attempt by a user will be"
                " stored. Necessary for marking."),
    verbose_name=_("Exam Paper"))

这是我的模特,请提出任何建议 我已经搜索过google,但我得到的所有信息都不适合我,因此对于每个测验,用户都可以在访问之前获得许可,例如在实际进行测验之前满足一些要求。

0 个答案:

没有答案