From be388c251fe045f3756b8e52647cf270cd21e44c Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Wed, 9 Jul 2014 23:22:54 +0100 Subject: [PATCH] moved another function from templatetags by adding a new method to question models --- multichoice/models.py | 3 +++ multichoice/tests.py | 6 ++++-- quiz/templatetags/quiz_tags.py | 20 ++++++-------------- true_false/models.py | 6 ++++++ true_false/tests.py | 7 +++++++ 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/multichoice/models.py b/multichoice/models.py index efbb77f..ffaf41a 100644 --- a/multichoice/models.py +++ b/multichoice/models.py @@ -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" diff --git a/multichoice/tests.py b/multichoice/tests.py index b5ca1d2..fd5eb6d 100644 --- a/multichoice/tests.py +++ b/multichoice/tests.py @@ -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) diff --git a/quiz/templatetags/quiz_tags.py b/quiz/templatetags/quiz_tags.py index 5cd583e..9f103c3 100644 --- a/quiz/templatetags/quiz_tags.py +++ b/quiz/templatetags/quiz_tags.py @@ -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) diff --git a/true_false/models.py b/true_false/models.py index d1e2486..bca63f5 100644 --- a/true_false/models.py +++ b/true_false/models.py @@ -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" diff --git a/true_false/tests.py b/true_false/tests.py index 5880761..3773713 100644 --- a/true_false/tests.py +++ b/true_false/tests.py @@ -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'}]) -- 2.39.5