diff options
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 1bbd035..7bd0a9e 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -414,6 +414,23 @@ class AnswerPaperManager(models.Manager): question_stats[question] = [0, questions[question.id]] return question_stats + def get_answerpapers_for_quiz(self, questionpaper_id): + return self.filter(question_paper_id=questionpaper_id) + + def _get_answerpapers_users(self, answerpapers): + return answerpapers.values_list('user', flat=True).distinct() + + def get_latest_attempts(self, questionpaper_id): + papers = self.get_answerpapers_for_quiz(questionpaper_id) + users = self._get_answerpapers_users(papers) + latest_attempts = [] + for user in users: + latest_attempts.append(self._get_latest_attempt(papers, user)) + return latest_attempts + + def _get_latest_attempt(self, answerpapers, user_id): + return answerpapers.filter(user_id=user_id).order_by('-attempt_number')[0] + ############################################################################### class AnswerPaper(models.Model): |