diff options
-rw-r--r-- | yaksh/models.py | 37 | ||||
-rw-r--r-- | yaksh/views.py | 24 |
2 files changed, 31 insertions, 30 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 979740d..591f0e4 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -855,21 +855,28 @@ 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) + ans_paper = AnswerPaper.objects.filter(user=user, + attempt_number=attempt_num, + question_paper=self + ).order_by['-id'] + if ans_paper: + ans_paper = ans_paper[0] + else: + 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) return ans_paper def _is_questionpaper_passed(self, user): diff --git a/yaksh/views.py b/yaksh/views.py index 9f70299..add791e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -393,8 +393,8 @@ def start(request, questionpaper_id=None, attempt_num=None): if is_moderator(user): return redirect("/exam/manage") return redirect("/exam/quizzes") + attempt_number = 1 if not last_attempt else last_attempt.attempt_number + 1 if attempt_num is None: - attempt_number = 1 if not last_attempt else last_attempt.attempt_number +1 context = {'user': user, 'questionpaper': quest_paper, 'attempt_num': attempt_number} if is_moderator(user): @@ -402,24 +402,18 @@ def start(request, questionpaper_id=None, attempt_num=None): return my_render_to_response('yaksh/intro.html', context, context_instance=ci) else: - attempted_paper = AnswerPaper.objects.filter(user=user, - question_paper=quest_paper, - attempt_number=attempt_num - ) - if not attempted_paper: - ip = request.META['REMOTE_ADDR'] - 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) + ip = request.META['REMOTE_ADDR'] + 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_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) - - + msg = 'You have already finished the quiz!' + raise Http404(msg) @login_required @email_verified |