From e44090c8ed6c7845b04c0f4cd661d43973186004 Mon Sep 17 00:00:00 2001 From: tomwalker Date: Thu, 24 Jul 2014 22:16:42 +0100 Subject: [PATCH] add_user_answer added to the sitting model --- quiz/models.py | 11 ++++++++--- quiz/tests.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/quiz/models.py b/quiz/models.py index c7541a7..6ffea6c 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -1,4 +1,4 @@ -import re +import re, json from django.db import models from django.core.exceptions import ValidationError from django.core.validators import MaxValueValidator @@ -312,6 +312,9 @@ class Sitting(models.Model): Incorrect_questions is a list in the same format. Sitting deleted when quiz finished unless quiz.exam_paper is true. + + User_answers is a json object in which the question PK is stored + with the answer the user gave. """ user = models.ForeignKey('auth.User') @@ -327,7 +330,7 @@ class Sitting(models.Model): complete = models.BooleanField(default=False, blank=False) - user_answers = models.TextField(blank=True) + user_answers = models.TextField(blank=True, default='{}') objects = SittingManager() @@ -414,7 +417,9 @@ class Sitting(models.Model): return self.quiz.fail_text def add_user_answer(self, question, guess): - pass + current = json.loads(self.user_answers) + current[question.id] = guess + self.user_answers = json.dumps(current) class Question(models.Model): diff --git a/quiz/tests.py b/quiz/tests.py index 9d883c1..e4f3486 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -244,7 +244,7 @@ class TestSitting(TestCase): self.sitting.mark_quiz_complete() self.assertEqual(self.sitting.complete, True) - def test_user_answers(self): + def test_add_user_answer(self): self.answer1 = Answer.objects.create(id=123, question=self.question1, content='bing', -- 2.39.5