From: Tom Walker Date: Thu, 10 Jul 2014 19:26:36 +0000 (+0100) Subject: slimming down template tags, moved another function from template tags to model metho... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=4ce5f82e6bae4cc5fe3615aee5f0dfb3e0238443;p=django_quiz.git slimming down template tags, moved another function from template tags to model method, simplified a few templates --- diff --git a/.gitignore b/.gitignore index 3153618..cf86186 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.*~ *~ *.pyc +#*# /db.sqlite3 /manage.py /mysite* diff --git a/quiz/models.py b/quiz/models.py index 77de0ec..57cfcf2 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -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): diff --git a/quiz/templates/result.html b/quiz/templates/result.html index 0da8bde..63095b3 100644 --- a/quiz/templates/result.html +++ b/quiz/templates/result.html @@ -9,64 +9,70 @@
-{% if previous %} + {% if previous %} -

The previous question:

-

{{ previous.previous_question }}

-

Your answer was {{ previous.previous_outcome }}

- {% correct_answer previous %} -

Explanation:

-
-

{{ previous.previous_question.explanation }}

-
-
+

The previous question:

+

{{ previous.previous_question }}

+

Your answer was {{ previous.previous_outcome }}

+ {% correct_answer previous %} +

Explanation:

+
+

{{ previous.previous_question.explanation }}

+
+
-{% endif %} + {% endif %} -{% if score or max_score %} + {% if score or max_score %} -
-

Exam results

-

Exam title: {{ quiz.title }}

+
+

Exam results

+

+ Exam title: + {{ quiz.title }}

-

You answered {{ score }} questions correctly out of {{ max_score }}, giving you {{ percent }} percent correct

+

+ You answered {{ score }} questions correctly out of {{ max_score }}, giving you {{ percent }} percent correct +

-

Review the questions below and try the exam again in the future.

+

Review the questions below and try the exam again in the future.

- {% if user.is_authenticated %} + {% if user.is_authenticated %} -

The result of this exam will be stored in your progress section so you can review and monitor your progression.

+

The result of this exam will be stored in your progress section so you can review and monitor your progression.

- {% endif %} -
+ {% endif %} +
-{% endif %} + {% endif %} -
+
-{% if session and possible %} + {% if session and possible %} -

Your session score is {{ session }} out of a possible {{ possible }}

+

+ Your session score is {{ session }} out of a possible {{ possible }} +

-
+
-{% endif %} + {% endif %} -{% if questions %} + {% if questions %} - {% for question in questions %} + {% for question in questions %} -

{{ question.content }}

- {% correct_answer_for_all_with_users_incorrect question incorrect_questions %} +

+ {{ question.content }} +

+ {% correct_answer_for_all question %} + {% endfor %} - - {% endfor %} - -{% endif %} + {% endif %} diff --git a/quiz/templatetags/quiz_tags.py b/quiz/templatetags/quiz_tags.py index 9f103c3..f226bd0 100644 --- a/quiz/templatetags/quiz_tags.py +++ b/quiz/templatetags/quiz_tags.py @@ -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} diff --git a/quiz/tests.py b/quiz/tests.py index 7753408..7db9da7 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -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,'})