]> git.parisson.com Git - django_quiz.git/commitdiff
Incorporate user answers in Sitting.get_questions
authorRichard Mansfield <richard@dragonfly.co.nz>
Sun, 10 Aug 2014 22:54:49 +0000 (10:54 +1200)
committerRichard Mansfield <richard@dragonfly.co.nz>
Sun, 10 Aug 2014 22:55:38 +0000 (10:55 +1200)
quiz/models.py

index bf9759d6aae9b2a9ce99c99044194afa59598347..64a5f64bc2c1a41ca004ba41ef61802ae3d2b779 100644 (file)
@@ -466,20 +466,23 @@ class Sitting(models.Model):
         self.user_answers = json.dumps(current)
         self.save()
 
-    def get_questions(self):
+    def get_questions(self, with_answers=False):
         question_ids = self._question_ids()
-        return sorted(
+        questions = sorted(
             self.quiz.question_set.filter(id__in=question_ids)
                                   .select_subclasses(),
             key=lambda q: question_ids.index(q.id))
 
+        if with_answers:
+            user_answers = json.loads(self.user_answers)
+            for question in questions:
+                question.user_answer = user_answers[unicode(question.id)]
+
+        return questions
+
     @property
     def questions_with_user_answers(self):
-        output = {}
-        user_answers = json.loads(self.user_answers)
-        for question in self.get_questions():
-            output[question] = user_answers[unicode(question.id)]
-        return output
+        return {q : q.user_answer for q in self.get_questions(with_answers=True)}
 
     @property
     def get_max_score(self):