]> git.parisson.com Git - django_quiz.git/commitdiff
slimming down template tags, moved another function from template tags to model metho...
authorTom Walker <tomwalker0472@gmail.com>
Thu, 10 Jul 2014 19:26:36 +0000 (20:26 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Thu, 10 Jul 2014 19:26:36 +0000 (20:26 +0100)
.gitignore
quiz/models.py
quiz/templates/result.html
quiz/templatetags/quiz_tags.py
quiz/tests.py

index 3153618de8baa10df5f0a7e99a8a8fdeeb1e3765..cf86186ad3b1a0f82562fef69bd1630bc985e2a8 100644 (file)
@@ -1,6 +1,7 @@
 *.*~
 *~
 *.pyc
+#*#
 /db.sqlite3
 /manage.py
 /mysite*
index 77de0ec563da030cc332d9387af29f772800fd3c..57cfcf28118981b48d3d42b82287b44dbecd5409 100644 (file)
@@ -121,6 +121,9 @@ class Quiz(models.Model):
     def __unicode__(self):
         return unicode(self.title)
 
+    def get_max_score(self):
+        return self.question_set.count()
+
 """
 Progress is used to track an individual signed in users score on different
 quiz's and categories
@@ -212,7 +215,7 @@ class Progress(models.Model):
                                         .exists()
 
         if category_test is False:
-            return "error",  "category does not exist"
+            return "error", "category does not exist"
 
         to_find = re.escape(category_queried) + r",(\d+),(\d+),"
 
@@ -245,7 +248,7 @@ class Progress(models.Model):
         if any([category_test is False, score_to_add is False,
                 possible_to_add is False, str(score_to_add).isdigit() is False,
                 str(possible_to_add).isdigit() is False]):
-            return "error",  "category does not exist or invalid score"
+            return "error", "category does not exist or invalid score"
 
         to_find = re.escape(str(category_queried)) + r",(\d+),(\d+),"
 
@@ -358,7 +361,7 @@ class Sitting(models.Model):
     def remove_first_question(self):
         first_comma = self.question_list.find(',')
         if first_comma != -1 or first_comma != 0:
-            self.question_list = self.question_list[first_comma+1:]
+            self.question_list = self.question_list[first_comma + 1:]
             self.save()
 
     def add_to_score(self, points):
index 0da8bdea06a92d0c60931061e2ac95a0370579c8..63095b3106b6c45c155f16fab8738d66c837e363 100644 (file)
@@ -9,64 +9,70 @@
 
 <div class="container">
 
-{% if previous %}
+  {% if previous %}
 
-        <p class="muted"><small>The previous question:</small></p>
-        <p>{{ previous.previous_question }}</p>
-        <p>Your answer was <strong>{{ previous.previous_outcome }}</strong></p>
-               {% correct_answer previous %}
-        <p><strong>Explanation:</strong></p>
-        <div class="well " style="background-color: #fcf8e3;">
-               <p>{{ previous.previous_question.explanation }}</p>
-        </div>
-<hr>
+  <p class="muted"><small>The previous question:</small></p>
+  <p>{{ previous.previous_question }}</p>
+  <p>Your answer was <strong>{{ previous.previous_outcome }}</strong></p>
+  {% correct_answer previous %}
+  <p><strong>Explanation:</strong></p>
+  <div class="well " style="background-color: #fcf8e3;">
+    <p>{{ previous.previous_question.explanation }}</p>
+  </div>
+  <hr>
 
-{% endif %}
+  {% endif %}
 
-{% if score or max_score %}
+  {% if score or max_score %}
 
-       <div>
-               <h2>Exam results</h2>
-               <p><small class="muted">Exam title:</small> <strong>{{ quiz.title }}</strong></p>
+  <div>
+       <h2>Exam results</h2>
+       <p>
+         <small class="muted">Exam title: </small>
+         <strong>{{ quiz.title }}</strong></p>
 
-               <p class="lead">You answered {{ score }} questions correctly out of {{ max_score }}, giving you {{ percent }} percent correct</p>
+       <p class="lead">
+         You answered {{ score }} questions correctly out of {{ max_score }}, giving you {{ percent }} percent correct
+       </p>
 
-               <p>Review the questions below and try the exam again in the future.</p>
+       <p>Review the questions below and try the exam again in the future.</p>
 
-               {% if user.is_authenticated %}
+       {% if user.is_authenticated %}
 
-                       <p>The result of this exam will be stored in your progress section so you can review and monitor your progression.</p>
+         <p>The result of this exam will be stored in your progress section so you can review and monitor your progression.</p>
 
-               {% endif %}
-        </div>
+       {% endif %}
+  </div>
 
 
-{% endif %}
+  {% endif %}
 
 
-    <hr>
+  <hr>
 
-{% if session and possible %}
+  {% if session and possible %}
 
-    <p class="lead">Your session score is {{ session }} out of a possible {{ possible }}</p>
+  <p class="lead">
+       Your session score is {{ session }} out of a possible {{ possible }}
+  </p>
 
-    <hr>
+  <hr>
 
-{% endif %}
+  {% endif %}
 
-{% if questions %}
+  {% if questions %}
 
-       {% for question in questions %}
+    {% for question in questions %}
 
-               <p class="lead">{{ question.content }}</p>
-                       {% correct_answer_for_all_with_users_incorrect question incorrect_questions %}
+      <p class="lead">
+               {{ question.content }}
+         </p>
 
+         {% correct_answer_for_all question %}
 
+  {% endfor %}
 
-
-       {% endfor %}
-
-{% endif %}
+  {% endif %}
 
 
 
index 9f103c3546ac9a7b9009d5e50d88ed6f0268f841..f226bd0aa2b4323ec728e5962a5cd1dd640db958 100644 (file)
@@ -1,15 +1,13 @@
 from django import template
 
 from multichoice.models import Answer, MCQuestion
-from true_false.models import TF_Question
 
 register = template.Library()
 
 
 @register.inclusion_tag('answers_for_mc_question.html', takes_context=True)
 def answers_for_mc_question(context, question):
-    answers = Answer.objects.filter(question__id=question.id).order_by('?')
-    return {'answers': answers}
+    return {'answers': Answer.objects.filter(question=question).order_by('?')}
 
 
 @register.inclusion_tag('correct_answer.html', takes_context=True)
@@ -19,38 +17,26 @@ 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):
         previous_answer_id = int(previous_answer_id)
-    return {'answers': answers,
+    return {'answers': q.get_answers(),
             'question_type': {q_type: True},
             'previous_answer_id': previous_answer_id}
 
 
 @register.inclusion_tag('correct_answer.html', takes_context=True)
-def correct_answer_for_all_with_users_incorrect(context,
-                                                question,
-                                                incorrect_list):
+def correct_answer_for_all(context, question):
     """
     processes the correct answer based on a given question object
     if the answer is incorrect, informs the user
     """
-    question_id = str(question.id)
-    if question_id in incorrect_list:
+    answers = question.get_answers()
+    incorrect_list = context.get('incorrect_questions', '')
+    if str(question.id) in incorrect_list:
         user_was_incorrect = True
     else:
         user_was_incorrect = False
-
-    if question.__class__.__name__ == "MCQuestion":
-        answers = Answer.objects.filter(question__id=question.id)
-
-    if question.__class__.__name__ == "TF_Question":
-        answers = [{'correct': question.check_if_correct('T'),
-                    'content': 'True'},
-                   {'correct': question.check_if_correct('F'),
-                    'content': 'False'}]
-
     return {'answers': answers, 'user_was_incorrect': user_was_incorrect}
 
 
@@ -59,9 +45,10 @@ def user_previous_exam(context, exam):
     """
     Provides details of finished exams
     """
-    title = exam.quiz.title
     final_score = exam.current_score
-    possible_score = exam.quiz.question_set.count()
+    possible_score = exam.quiz.get_max_score()
     percent = int(round((float(final_score) / float(possible_score)) * 100))
-    return {'title': title, 'score': final_score,
-            'possible': possible_score, 'percent': percent}
+    return {'title': exam.quiz.title,
+            'score': final_score,
+            'possible': possible_score,
+            'percent': percent}
index 7753408a41dc7351e5c6a83699ab556b3a470cd0..7db9da701cd3c72d0758ff07e13cad0774af03c8 100644 (file)
@@ -33,19 +33,23 @@ class TestQuiz(TestCase):
         self.quiz1 = Quiz.objects.create(id=1,
                                          title='test quiz 1',
                                          description='d1',
-                                         url='tq1',)
+                                         url='tq1')
         self.quiz2 = Quiz.objects.create(id=2,
                                          title='test quiz 2',
                                          description='d2',
-                                         url='t q2',)
+                                         url='t q2')
         self.quiz3 = Quiz.objects.create(id=3,
                                          title='test quiz 3',
                                          description='d3',
-                                         url='t   q3',)
+                                         url='t   q3')
         self.quiz4 = Quiz.objects.create(id=4,
                                          title='test quiz 4',
                                          description='d4',
-                                         url='T-!£$%^&*Q4',)
+                                         url='T-!£$%^&*Q4')
+
+        self.question1 = MCQuestion.objects.create(id=1,
+                                                   content='squawk')
+        self.question1.quiz.add(self.quiz1)
 
     def test_quiz_url(self):
         self.assertEqual(self.quiz1.url, 'tq1')
@@ -59,7 +63,7 @@ class TestQuiz(TestCase):
                                  description='d5',
                                  url='tq5',
                                  category=self.c1,
-                                 exam_paper=True,)
+                                 exam_paper=True)
 
         self.assertEqual(q5.category.category, self.c1.category)
         self.assertEqual(q5.random_order, False)
@@ -72,6 +76,9 @@ class TestQuiz(TestCase):
 
         self.assertEqual(self.quiz1.exam_paper, True)
 
+    def test_get_max_score(self):
+        self.assertEqual(self.quiz1.get_max_score(), 1)
+
 
 class TestProgress(TestCase):
     def setUp(self):
@@ -80,7 +87,7 @@ class TestProgress(TestCase):
         self.quiz1 = Quiz.objects.create(id=1,
                                          title='test quiz 1',
                                          description='d1',
-                                         url='tq1',)
+                                         url='tq1')
 
         self.user = User.objects.create_user(username='jacob',
                                              email='jacob@jacob.com',
@@ -144,14 +151,14 @@ class TestSitting(TestCase):
         self.quiz1 = Quiz.objects.create(id=1,
                                          title='test quiz 1',
                                          description='d1',
-                                         url='tq1',)
+                                         url='tq1')
 
         self.question1 = MCQuestion.objects.create(id=1,
-                                                   content='squawk',)
+                                                   content='squawk')
         self.question1.quiz.add(self.quiz1)
 
         self.question2 = MCQuestion.objects.create(id=2,
-                                                   content='squeek',)
+                                                   content='squeek')
         self.question2.quiz.add(self.quiz1)
 
         self.user = User.objects.create_user(username='jacob',
@@ -196,7 +203,7 @@ class TestSitting(TestCase):
         self.assertIn('1', self.sitting.get_incorrect_questions())
 
         question3 = TF_Question.objects.create(id=3,
-                                               content='oink',)
+                                               content='oink')
         self.sitting.add_incorrect_question(question3)
         self.assertIn('3', self.sitting.get_incorrect_questions())
 
@@ -232,7 +239,7 @@ class TestNonQuestionViews(TestCase):
         self.quiz2 = Quiz.objects.create(id=2,
                                          title='test quiz 2',
                                          description='d2',
-                                         url='t q2',)
+                                         url='t q2')
 
     def test_index(self):
         response = self.client.get('/q/')
@@ -294,22 +301,22 @@ class TestQuestionViewsAnon(TestCase):
                                          category=self.c1)
 
         self.question1 = MCQuestion.objects.create(id=1,
-                                                   content='squawk',)
+                                                   content='squawk')
         self.question1.quiz.add(self.quiz1)
 
         self.question2 = MCQuestion.objects.create(id=2,
-                                                   content='squeek',)
+                                                   content='squeek')
         self.question2.quiz.add(self.quiz1)
 
         self.answer1 = Answer.objects.create(id=123,
                                              question=self.question1,
                                              content='bing',
-                                             correct=False,)
+                                             correct=False)
 
         self.answer2 = Answer.objects.create(id=456,
                                              question=self.question2,
                                              content='bong',
-                                             correct=True,)
+                                             correct=True)
 
     def test_quiz_take_anon_view_only(self):
         found = resolve('/q/tq1/take/')
@@ -450,12 +457,12 @@ class TestQuestionViewsUser(TestCase):
         self.answer1 = Answer.objects.create(id=123,
                                              question=self.question1,
                                              content='bing',
-                                             correct=False,)
+                                             correct=False)
 
         self.answer2 = Answer.objects.create(id=456,
                                              question=self.question2,
                                              content='bong',
-                                             correct=True,)
+                                             correct=True)
 
     def test_quiz_take_user_view_only(self):
         sittings_before = Sitting.objects.count()
@@ -589,12 +596,12 @@ class TestTemplateTags(TestCase):
         self.answer1 = Answer.objects.create(id=123,
                                              question=self.question1,
                                              content='bing',
-                                             correct=False,)
+                                             correct=False)
 
         self.answer2 = Answer.objects.create(id=456,
                                              question=self.question1,
                                              content='bong',
-                                             correct=True,)
+                                             correct=True)
 
         self.question2 = TF_Question.objects.create(id=3,
                                                     content='oink',
@@ -602,7 +609,7 @@ class TestTemplateTags(TestCase):
         self.quiz1 = Quiz.objects.create(id=1,
                                          title='test quiz 1',
                                          description='d1',
-                                         url='tq1',)
+                                         url='tq1')
 
         self.question1.quiz.add(self.quiz1)
         self.question2.quiz.add(self.quiz1)
@@ -654,8 +661,7 @@ class TestTemplateTags(TestCase):
 
     def test_correct_answer_all_anon(self):
         template = Template('{% load quiz_tags %}' +
-                            '{% correct_answer_for_all_with_users_incorrect' +
-                            ' question  incorrect_questions %}')
+                            '{% correct_answer_for_all question %}')
 
         context = Context({'question': self.question1})
 
@@ -665,8 +671,7 @@ class TestTemplateTags(TestCase):
 
     def test_correct_answer_all_user(self):
         template = Template('{% load quiz_tags %}' +
-                            '{% correct_answer_for_all_with_users_incorrect ' +
-                            'question  incorrect_questions %}')
+                            '{% correct_answer_for_all question %}')
 
         context = Context({'question': self.question1,
                            'incorrect_questions': '1,'})