diff options
author | adityacp | 2017-10-06 17:54:09 +0530 |
---|---|---|
committer | adityacp | 2017-10-06 17:54:09 +0530 |
commit | 30f4b893d4a36e0e1408ef2a07c92521fc4074bc (patch) | |
tree | 04730e146e8a9e173fe06fed3ed8c3bcf79f1b20 | |
parent | 474fc2c54eda0e8c890ea17b791b2fe74d07c611 (diff) | |
download | online_test-30f4b893d4a36e0e1408ef2a07c92521fc4074bc.tar.gz online_test-30f4b893d4a36e0e1408ef2a07c92521fc4074bc.tar.bz2 online_test-30f4b893d4a36e0e1408ef2a07c92521fc4074bc.zip |
Add new field in AnswerPaper model and fetch questions as per order
-rw-r--r-- | yaksh/models.py | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 78669e7..9ac629c 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -854,7 +854,6 @@ class QuestionPaper(models.Model): questions += question_set.get_random_questions() if self.shuffle_questions: all_questions = self.get_shuffled_questions(questions) - print("in _get_questions_for_answerpaper", all_questions, "\n") else: all_questions = questions return all_questions @@ -878,10 +877,11 @@ class QuestionPaper(models.Model): 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.questions.add(*questions) + question_ids = [str(que.id) for que in questions] + 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, @@ -953,9 +953,13 @@ class QuestionPaper(models.Model): def get_shuffled_questions(self, questions): """Get shuffled questions if auto suffle is enabled""" random.shuffle(questions) - print("in get_shuffled_questions", questions, "\n") return questions + def has_questions(self): + questions = self.get_ordered_questions() + \ + list(self.random_questions.all()) + return False if len(questions) == 0 else True + def __str__(self): return "Question Paper for " + self.quiz.description @@ -1176,13 +1180,27 @@ class AnswerPaper(models.Model): default='inprogress' ) + # set question order + questions_order = models.CharField(max_length=255, blank=True, default='') + objects = AnswerPaperManager() def current_question(self): """Returns the current active question to display.""" if self.questions_unanswered.all(): - return self.questions_unanswered.all()[0] - return self.questions.all()[0] + cur_question = self.get_current_question( + self.questions_unanswered.all()) + else: + cur_question = self.get_current_question(self.questions.all()) + return cur_question + + def get_current_question(self, questions): + if self.questions_order: + question_id = int(self.questions_order.split(',')[0]) + question = self.questions_unanswered.get(id=question_id) + else: + question = questions.first() + return question def questions_left(self): """Returns the number of questions left.""" @@ -1207,17 +1225,30 @@ class AnswerPaper(models.Model): Skips the current question and returns the next sequentially available question. """ - all_questions = self.questions.all() - unanswered_questions = self.questions_unanswered.all() - questions = list(all_questions.values_list('id', flat=True)) - if len(questions) == 0: + if self.questions_order: + all_questions = [int(q_id) + for q_id in self.questions_order.split(',')] + else: + all_questions = list(self.questions.all().values_list( + 'id', flat=True)) + if len(all_questions) == 0: return None try: - index = questions.index(int(question_id)) - next_id = questions[index+1] + index = all_questions.index(int(question_id)) + next_id = all_questions[index+1] except (ValueError, IndexError): - next_id = questions[0] - return all_questions.get(id=next_id) + next_id = all_questions[0] + return self.questions.get(id=next_id) + + def get_all_ordered_questions(self): + """Get all questions in a specific""" + if self.questions_order: + que_ids = [int(q_id) for q_id in self.questions_order.split(',')] + questions = [self.questions.get(id=que_id) + for que_id in que_ids] + else: + questions = list(self.questions.all()) + return questions def time_left(self): """Return the time remaining for the user in seconds.""" |