]> git.parisson.com Git - django_quiz.git/commitdiff
moved another function from templatetags by adding a new method to
authorTom Walker <tomwalker0472@gmail.com>
Wed, 9 Jul 2014 22:22:54 +0000 (23:22 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Wed, 9 Jul 2014 22:22:54 +0000 (23:22 +0100)
question models

multichoice/models.py
multichoice/tests.py
quiz/templatetags/quiz_tags.py
true_false/models.py
true_false/tests.py

index efbb77fc466eec8301ba96398bc927995b10690f..ffaf41a75e412482988a34f809aa94343f6f57bc 100644 (file)
@@ -12,6 +12,9 @@ class MCQuestion(Question):
         else:
             return False
 
+    def get_answers(self):
+        return Answer.objects.filter(question=self)
+
     class Meta:
         verbose_name = "Multiple Choice Question"
         verbose_name_plural = "Multiple Choice Questions"
index b5ca1d20b41016638fa932223b784329e2913174..fd5eb6d274a26c6d08423400d0e142c56fc371b2 100644 (file)
@@ -22,11 +22,13 @@ class TestMCQuestionModel(TestCase):
                                              correct=True)
 
     def test_answers(self):
-        answers = Answer.objects.filter(question__id=self.q.id)
-        correct_a = Answer.objects.get(question__id=self.q.id,
+        answers = Answer.objects.filter(question=self.q)
+        correct_a = Answer.objects.get(question=self.q,
                                        correct=True)
+        answers_by_method = self.q.get_answers()
 
         self.assertEqual(answers.count(), 2)
         self.assertEqual(correct_a.content, "European")
         self.assertEqual(self.q.check_if_correct(123), False)
         self.assertEqual(self.q.check_if_correct(456), True)
+        self.assertEqual(answers_by_method.count(), 2)
index 5cd583e9291e1f68791fef63a9f62b7b0d45bc3a..9f103c3546ac9a7b9009d5e50d88ed6f0268f841 100644 (file)
@@ -19,21 +19,13 @@ def correct_answer(context, previous):
     """
     q = previous['previous_question']
     q_type = q.__class__.__name__
-
+    answers = q.get_answers()
+    previous_answer_id = context['previous']['previous_answer']
     if isinstance(q, MCQuestion):
-        answers = Answer.objects.filter(question=q)
-        previous_answer_id = int(context['previous']['previous_answer'])
-        return {'answers': answers,
-                'question_type': {q_type: True},
-                'previous_answer_id': previous_answer_id}
-
-    if isinstance(q, TF_Question):
-        answers = [{'correct': q.check_if_correct('T'),
-                    'content': 'True'},
-                   {'correct': q.check_if_correct('F'),
-                    'content': 'False'}]
-        return {'answers': answers,
-                'question_type': {q_type: True}}
+        previous_answer_id = int(previous_answer_id)
+    return {'answers': answers,
+            'question_type': {q_type: True},
+            'previous_answer_id': previous_answer_id}
 
 
 @register.inclusion_tag('correct_answer.html', takes_context=True)
index d1e24860bbc416cc6cceef8c9b1072663147e9b1..bca63f5425474e769d01ea095e5f0a086b0d6082 100644 (file)
@@ -22,6 +22,12 @@ class TF_Question(Question):
         else:
             return False
 
+    def get_answers(self):
+        return [{'correct': self.check_if_correct('T'),
+                 'content': 'True'},
+                {'correct': self.check_if_correct('F'),
+                 'content': 'False'}]
+
     class Meta:
         verbose_name = "True/False Question"
         verbose_name_plural = "True/False Questions"
index 5880761a6e9f747ea7ff1df883de04fe66500378..377371373a11c6e09675675b5ec8d3d22c71891d 100644 (file)
@@ -22,3 +22,10 @@ class TestTrueFalseQuestionModel(TestCase):
         self.assertEqual(self.blue.correct, False)
         self.assertEqual(self.blue.check_if_correct("T"), False)
         self.assertEqual(self.blue.check_if_correct("F"), True)
+
+    def test_get_answers(self):
+        self.assertEqual(self.red.get_answers(),
+                         [{'correct': True,
+                           'content': 'True'},
+                          {'correct': False,
+                           'content': 'False'}])