diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/models.py | 43 | ||||
-rw-r--r-- | yaksh/test_models.py | 7 | ||||
-rw-r--r-- | yaksh/views.py | 22 |
3 files changed, 49 insertions, 23 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 979740d..063e1e3 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -855,21 +855,34 @@ class QuestionPaper(models.Model): def make_answerpaper(self, user, ip, attempt_num): """Creates an answer paper for the user to attempt the quiz""" - ans_paper = AnswerPaper( - user=user, - user_ip=ip, - attempt_number=attempt_num - ) - ans_paper.start_time = timezone.now() - ans_paper.end_time = ans_paper.start_time + \ - timedelta(minutes=self.quiz.duration) - ans_paper.question_paper = self - ans_paper.save() - questions = self._get_questions_for_answerpaper() - for question in questions: - ans_paper.questions.add(question) - for question in questions: - ans_paper.questions_unanswered.add(question) + try: + ans_paper = AnswerPaper.objects.get(user=user, + attempt_number=attempt_num, + question_paper=self + ) + except AnswerPaper.DoesNotExist: + ans_paper = AnswerPaper( + user=user, + user_ip=ip, + attempt_number=attempt_num + ) + ans_paper.start_time = timezone.now() + ans_paper.end_time = ans_paper.start_time + \ + timedelta(minutes=self.quiz.duration) + ans_paper.question_paper = self + ans_paper.save() + questions = self._get_questions_for_answerpaper() + for question in questions: + ans_paper.questions.add(question) + for question in questions: + ans_paper.questions_unanswered.add(question) + except AnswerPaper.MultipleObjectsReturned: + ans_paper = AnswerPaper.objects.get(user=user, + attempt_number=attempt_num, + question_paper=self + ).order_by('-id') + ans_paper = ans_paper[0] + return ans_paper def _is_questionpaper_passed(self, user): diff --git a/yaksh/test_models.py b/yaksh/test_models.py index a940c0f..bc7f114 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -451,6 +451,13 @@ class QuestionPaperTestCases(unittest.TestCase): self.assertFalse(self.question_paper.is_prerequisite_passed(self.user)) # test can_attempt_now(self): self.assertFalse(self.question_paper.can_attempt_now(self.user)) + # trying to create an answerpaper with same parameters passed. + answerpaper2 = self.question_paper.make_answerpaper(self.user, self.ip, + attempt_num) + # check if make_answerpaper returned an object instead of creating one. + self.assertEqual(answerpaper, answerpaper2) + + def test_create_trial_paper_to_test_quiz(self): qu_list = [str(self.questions_list[0]), str(self.questions_list[1])] diff --git a/yaksh/views.py b/yaksh/views.py index 8ce0c02..1c6feca 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -420,11 +420,11 @@ def start(request, questionpaper_id=None, attempt_num=None): if is_moderator(user): return redirect("/exam/manage") return redirect("/exam/quizzes") + if not last_attempt: + attempt_number = 1 + else: + attempt_number = last_attempt.attempt_number + 1 if attempt_num is None: - if not last_attempt: - attempt_number = 1 - else: - last_attempt.attempt_number + 1 context = { 'user': user, 'questionpaper': quest_paper, @@ -439,9 +439,14 @@ def start(request, questionpaper_id=None, attempt_num=None): if not hasattr(user, 'profile'): msg = 'You do not have a profile and cannot take the quiz!' raise Http404(msg) - new_paper = quest_paper.make_answerpaper(user, ip, attempt_num) - return show_question(request, new_paper.current_question(), new_paper) - + new_paper = quest_paper.make_answerpaper(user, ip, attempt_number) + if new_paper.status == 'inprogress': + return show_question(request, new_paper.current_question(), + new_paper + ) + else: + msg = 'You have already finished the quiz!' + raise Http404(msg) @login_required @email_verified @@ -638,7 +643,8 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): else: return show_question(request, current_question, paper) - +@login_required +@email_verified def quit(request, reason=None, attempt_num=None, questionpaper_id=None): """Show the quit page when the user logs out.""" paper = AnswerPaper.objects.get(user=request.user, |