From ab949b8b17e092acbddd74e3cff8f30f0763acaa Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Wed, 9 Jul 2014 22:44:48 +0100 Subject: [PATCH] removing logic from templates, reducing the complexity of template tags --- quiz/templates/correct_answer.html | 25 +++++-------------------- quiz/templates/question.html | 10 +++++----- quiz/templates/quiz/quiz_list.html | 2 +- quiz/templatetags/quiz_tags.py | 18 ++++++++++-------- quiz/tests.py | 8 ++++---- quiz/views.py | 6 +++--- 6 files changed, 28 insertions(+), 41 deletions(-) diff --git a/quiz/templates/correct_answer.html b/quiz/templates/correct_answer.html index 0923c38..4982496 100644 --- a/quiz/templates/correct_answer.html +++ b/quiz/templates/correct_answer.html @@ -1,43 +1,28 @@ {% if user_was_incorrect %} - -
- - You answered the above question incorrectly - -
- - +
+ You answered the above question incorrectly +
{% endif %} - {% for answer in answers %} - - - {% if answer.correct %} - - - {% else %} - {% endif %} - {% endfor %} -
{{ answer.content }} This is the correct answer
{{ answer.content }} - {% ifequal question_type 'MCQuestion' %} + {% if not question_type.TF_Question %} {% if answer.id == previous_answer_id %} This was your answer. {% endif %} - {% endifequal %} + {% endif %}
diff --git a/quiz/templates/question.html b/quiz/templates/question.html index f45a1b4..fe39cb7 100644 --- a/quiz/templates/question.html +++ b/quiz/templates/question.html @@ -49,19 +49,19 @@

{{ question.content }}

-
+ - {% ifequal question_type 'TF_Question' %} + {% if question_type.TF_Question %} {% include 'answers_for_tf_question.html' %} - {% endifequal %} + {% endif %} - {% ifequal question_type 'MCQuestion' %} + {% if question_type.MCQuestion %} {% answers_for_mc_question question %} - {% endifequal %} + {% endif %}
diff --git a/quiz/templates/quiz/quiz_list.html b/quiz/templates/quiz/quiz_list.html index a5466ea..39f9758 100644 --- a/quiz/templates/quiz/quiz_list.html +++ b/quiz/templates/quiz/quiz_list.html @@ -6,7 +6,7 @@ {% if quiz_list %} {% else %} diff --git a/quiz/templatetags/quiz_tags.py b/quiz/templatetags/quiz_tags.py index c42b134..5cd583e 100644 --- a/quiz/templatetags/quiz_tags.py +++ b/quiz/templatetags/quiz_tags.py @@ -1,6 +1,7 @@ from django import template -from multichoice.models import Answer +from multichoice.models import Answer, MCQuestion +from true_false.models import TF_Question register = template.Library() @@ -17,21 +18,22 @@ def correct_answer(context, previous): processes the correct answer based on the previous question dict """ q = previous['previous_question'] + q_type = q.__class__.__name__ - if q.__class__.__name__ == "MCQuestion": - answers = Answer.objects.filter(question__id=q.id) + if isinstance(q, MCQuestion): + answers = Answer.objects.filter(question=q) previous_answer_id = int(context['previous']['previous_answer']) return {'answers': answers, - 'question_type': q.__class__.__name__, + 'question_type': {q_type: True}, 'previous_answer_id': previous_answer_id} - if q.__class__.__name__ == "TF_Question": + 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.__class__.__name__} + 'question_type': {q_type: True}} @register.inclusion_tag('correct_answer.html', takes_context=True) @@ -57,7 +59,7 @@ def correct_answer_for_all_with_users_incorrect(context, {'correct': question.check_if_correct('F'), 'content': 'False'}] - return {'answers': answers, 'user_was_incorrect': user_was_incorrect, } + return {'answers': answers, 'user_was_incorrect': user_was_incorrect} @register.inclusion_tag('user_previous_exam.html', takes_context=True) @@ -70,4 +72,4 @@ def user_previous_exam(context, exam): possible_score = exam.quiz.question_set.count() percent = int(round((float(final_score) / float(possible_score)) * 100)) return {'title': title, 'score': final_score, - 'possible': possible_score, 'percent': percent, } + 'possible': possible_score, 'percent': percent} diff --git a/quiz/tests.py b/quiz/tests.py index 2bd7ca8..7753408 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -329,7 +329,7 @@ class TestQuestionViewsAnon(TestCase): self.question1.content) self.assertEqual(response.context['question_type'], self.question1.__class__.__name__) - self.assertEqual(response.context['previous'], {}) + self.assertEqual(response.context['previous'], False) self.assertTemplateUsed('question.html') session = self.client.session @@ -474,8 +474,8 @@ class TestQuestionViewsUser(TestCase): self.assertEqual(response.context['question'].content, self.question1.content) self.assertEqual(response.context['question_type'], - self.question1.__class__.__name__) - self.assertEqual(response.context['previous'], {}) + {self.question1.__class__.__name__: True}) + self.assertEqual(response.context['previous'], False) self.assertTemplateUsed('question.html') response = self.client.get('/q/tq1/take/') @@ -539,7 +539,7 @@ class TestQuestionViewsUser(TestCase): 'question_id': 1}) self.assertNotContains(response, 'previous question') - self.assertEqual(response.context['previous'], {}) + self.assertEqual(response.context['previous'], False) response = self.client.get('/q/tq2/take/', {'guess': 'T', diff --git a/quiz/views.py b/quiz/views.py index 87fae5a..0b5db43 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -97,7 +97,7 @@ def user_load_sitting(request, quiz): def user_load_next_question(request, sitting, quiz): - previous = {} + previous = False if 'guess' in request.GET: progress, created = Progress.objects.get_or_create(user=request.user) guess = request.GET['guess'] @@ -124,7 +124,7 @@ def user_load_next_question(request, sitting, quiz): # no questions left return final_result_user(request, sitting, previous) - question_type = next_question.__class__.__name__ + question_type = {next_question.__class__.__name__: True} return render_to_response('question.html', {'quiz': quiz, @@ -232,7 +232,7 @@ def new_anon_quiz_session(request, quiz): def load_anon_next_question(request, quiz): question_list = request.session[str(quiz.id) + "_q_list"] - previous = {} + previous = False if 'guess' in request.GET and request.GET['guess']: # if there has been a previous question -- 2.39.5