diff options
-rw-r--r-- | yaksh/models.py | 15 | ||||
-rw-r--r-- | yaksh/test_models.py | 15 |
2 files changed, 20 insertions, 10 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 9a406c3..93e656a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1172,7 +1172,7 @@ class QuestionPaper(models.Model): return all_questions def make_answerpaper(self, user, ip, attempt_num, course_id): - """Creates an answer paper for the user to attempt the quiz""" + """Creates an answer paper for the user to attempt the quiz""" try: ans_paper = AnswerPaper.objects.get(user=user, attempt_number=attempt_num, @@ -1197,14 +1197,6 @@ class QuestionPaper(models.Model): ans_paper.questions_order = ",".join(question_ids) ans_paper.save() ans_paper.questions_unanswered.add(*questions) - except AnswerPaper.MultipleObjectsReturned: - ans_paper = AnswerPaper.objects.get(user=user, - attempt_number=attempt_num, - question_paper=self, - course_id=course_id - ).order_by('-id') - ans_paper = ans_paper[0] - return ans_paper def _is_attempt_allowed(self, user, course_id): @@ -1514,6 +1506,11 @@ class AnswerPaper(models.Model): objects = AnswerPaperManager() + class Meta: + unique_together = ('user', 'question_paper', + 'attempt_number', "course" + ) + def get_per_question_score(self, question_id): if question_id not in self.get_questions().values_list('id', flat=True): return 'NA' diff --git a/yaksh/test_models.py b/yaksh/test_models.py index df8372c..2aa55d4 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -12,6 +12,7 @@ from datetime import datetime, timedelta from django.utils import timezone import pytz from django.contrib.auth.models import Group +from django.db import IntegrityError from django.core.files import File from django.forms.models import model_to_dict from textwrap import dedent @@ -835,7 +836,8 @@ class AnswerPaperTestCases(unittest.TestCase): question_paper=self.question_paper, start_time=self.start_time, end_time=self.end_time, - user_ip=self.ip + user_ip=self.ip, + course=self.course ) self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, @@ -1375,6 +1377,17 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(self.user2_answerpaper2.current_question(), self.question1) + def test_duplicate_attempt_answerpaper(self): + with self.assertRaises(IntegrityError): + new_answerpaper = AnswerPaper.objects.create( + user=self.answerpaper.user, + question_paper=self.answerpaper.question_paper, + attempt_number=self.answerpaper.attempt_number, + start_time=self.answerpaper.start_time, + end_time=self.answerpaper.end_time, + course=self.answerpaper.course + ) + ############################################################################### class CourseTestCases(unittest.TestCase): |