]> git.parisson.com Git - django_quiz.git/commitdiff
removing logic from templates, reducing the complexity of template tags
authorTom Walker <tomwalker0472@gmail.com>
Wed, 9 Jul 2014 21:44:48 +0000 (22:44 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Wed, 9 Jul 2014 21:44:48 +0000 (22:44 +0100)
quiz/templates/correct_answer.html
quiz/templates/question.html
quiz/templates/quiz/quiz_list.html
quiz/templatetags/quiz_tags.py
quiz/tests.py
quiz/views.py

index 0923c38bd35cafc36b2f1449d2f98804da972d7e..498249626dc7035bf4e4ad26085e4efc50bc6e32 100644 (file)
@@ -1,43 +1,28 @@
 {% if user_was_incorrect %}
-
-       <div class="alert alert-error">
-
-               <strong>You answered the above question incorrectly</strong>
-
-       </div>
-
-
+  <div class="alert alert-error">
+       <strong>You answered the above question incorrectly</strong>
+  </div>
 {% endif %}
 
 <table class="table table-striped table-bordered">
   <tbody>
-
        {% for answer in answers %}
-
-
-
       {% if answer.correct %}
-
            <tr class="success">
                  <td>{{ answer.content }}</td>
                  <td><strong>This is the correct answer</strong></td>
-
-
       {% else %}
                <tr>
          <td>{{ answer.content }}</td>
                  <td>
-                       {% ifequal question_type 'MCQuestion' %}
+                       {% if not question_type.TF_Question %}
                          {% if answer.id == previous_answer_id %}
                            This was your answer.
                          {% endif %}
-                       {% endifequal %}
+                       {% endif %}
                  </td>
-
          {% endif %}
            </tr>
-
        {% endfor %}
-
   </tbody>
 </table>
index f45a1b4a6668b6247902b8bfcac51e6c68f49633..fe39cb7cdaf7ccdc927efde30dd9499364b38174 100644 (file)
 
 <p class="lead">{{ question.content }}</p>
 
-<form action="{% url 'quiz_question' quiz.url %}" method="get">
+<form action="" method="get">
   <input type=hidden name="question_id" value="{{ question.id }}">
 
   <table class="table table-hover table-bordered" id="answerchoice">
        <tbody>
 
-         {% 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 %}
 
        </tbody>
   </table>
index a5466eaec11b3745344c766499e4a7dcf1aba491..39f97581f6dead4a99a1fc37317b7ed8da8e2845 100644 (file)
@@ -6,7 +6,7 @@
     {% if quiz_list %}
         <ul>
         {% for quiz in quiz_list %}
-            <li><a href="{% url 'quiz.views.quiz_take' quiz_name=quiz.url %}">{{ quiz.title }}</a></li>
+            <li><a href="{% url 'quiz_start_page' slug=quiz.url %}">{{ quiz.title }}</a></li>
         {% endfor %}
         </ul>
     {% else %}
index c42b1345f298e73ef83d0a06cb39fc2a02150dd3..5cd583e9291e1f68791fef63a9f62b7b0d45bc3a 100644 (file)
@@ -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}
index 2bd7ca819db2f9474cc9b24b8163ec917971f903..7753408a41dc7351e5c6a83699ab556b3a470cd0 100644 (file)
@@ -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',
index 87fae5a2b15b5cdb3ed1930ef1bba7f08bd29f92..0b5db434dd5d2e4fc10050cf48d04a30296ca4bd 100644 (file)
@@ -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