diff options
author | adityacp | 2017-02-22 15:00:11 +0530 |
---|---|---|
committer | adityacp | 2017-03-10 15:41:07 +0530 |
commit | c2ffb981b81410006a04b43997a2441a99dbff3d (patch) | |
tree | 6b2593d06d1bc355de5813ec129cdf937215eefd /yaksh/models.py | |
parent | 344b6441017f72225a0d89f41c6cde38f4bbe187 (diff) | |
download | online_test-c2ffb981b81410006a04b43997a2441a99dbff3d.tar.gz online_test-c2ffb981b81410006a04b43997a2441a99dbff3d.tar.bz2 online_test-c2ffb981b81410006a04b43997a2441a99dbff3d.zip |
Remove manytomany relationship for fixed_questions in question paper
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 398f508..f4a3a7a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -616,11 +616,11 @@ class Quiz(models.Model): ############################################################################### class QuestionPaperManager(models.Manager): - def _create_trial_from_questionpaper(self, original_quiz_id): + def _create_trial_from_questionpaper(self, original_quiz_id, fixed_que): """Creates a copy of the original questionpaper""" trial_questionpaper = self.get(quiz_id=original_quiz_id) - trial_questions = {"fixed_questions": trial_questionpaper - .fixed_questions.all(), + trial_questions = {"fixed_questions": fixed_que.get_fixed_questions( + trial_questionpaper), "random_questions": trial_questionpaper .random_questions.all() } @@ -635,7 +635,10 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.create(quiz=trial_quiz, total_marks=10, ) - trial_questionpaper.fixed_questions.add(*questions_list) + fixed_que = FixedQuestions() + for q_id in questions_list: + que = Question.objects.get(id=q_id) + fixed_que.add_fixed_questions(trial_questionpaper, que) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): @@ -643,11 +646,16 @@ class QuestionPaperManager(models.Manager): if self.filter(quiz=trial_quiz).exists(): trial_questionpaper = self.get(quiz=trial_quiz) else: + fixed_que = FixedQuestions() trial_questionpaper, trial_questions = \ - self._create_trial_from_questionpaper(original_quiz_id) + self._create_trial_from_questionpaper(original_quiz_id, + fixed_que + ) trial_questionpaper.quiz = trial_quiz - trial_questionpaper.fixed_questions\ - .add(*trial_questions["fixed_questions"]) + for question in trial_questions["fixed_questions"]: + fixed_que.add_fixed_questions(trial_questionpaper, + question + ) trial_questionpaper.random_questions\ .add(*trial_questions["random_questions"]) trial_questionpaper.save() @@ -661,9 +669,6 @@ class QuestionPaper(models.Model): # Question paper belongs to a particular quiz. quiz = models.ForeignKey(Quiz) - # Questions that will be mandatory in the quiz. - fixed_questions = models.ManyToManyField(Question) - # Questions that will be fetched randomly from the Question Set. random_questions = models.ManyToManyField("QuestionSet") @@ -678,7 +683,8 @@ class QuestionPaper(models.Model): def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 - questions = self.fixed_questions.all() + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question in questions: marks += question.points for question_set in self.random_questions.all(): @@ -687,7 +693,8 @@ class QuestionPaper(models.Model): def _get_questions_for_answerpaper(self): """ Returns fixed and random questions for the answer paper""" - questions = list(self.fixed_questions.filter(active=True)) + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question_set in self.random_questions.all(): questions += question_set.get_random_questions() return questions @@ -705,8 +712,10 @@ class QuestionPaper(models.Model): ans_paper.question_paper = self ans_paper.save() questions = self._get_questions_for_answerpaper() - ans_paper.questions.add(*questions) - ans_paper.questions_unanswered.add(*questions) + 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): @@ -748,14 +757,33 @@ class QuestionPaper(models.Model): summary="Yaksh Demo Question", user=user) # add fixed set of questions to the question paper + fixed_que = FixedQuestions() for question in questions: - question_paper.fixed_questions.add(question) + fixed_que.add_fixed_questions(questionpaper, question) def __str__(self): return "Question Paper for " + self.quiz.description ############################################################################### +class FixedQuestions(models.Model): + questionpaper = models.ForeignKey(QuestionPaper) + question = models.ForeignKey(Question) + + def get_fixed_questions(self, qp): + fixed_q = FixedQuestions.objects.filter(questionpaper=qp).order_by('pk') + questions = [que.question for que in fixed_q + if que.question.active == True] + return questions + + def add_fixed_questions(self, qp, que): + FixedQuestions.objects.create(questionpaper=qp, question=que) + + def remove_fixed_questions(self, que_ids): + FixedQuestions.objects.filter(question_id__in=que_ids).delete() + + +############################################################################### class QuestionSet(models.Model): """Question set contains a set of questions from which random questions will be selected for the quiz. |