diff --git a/docs/pip-requirements.txt b/docs/pip-requirements.txt index bf70685..0b9aa4b 100644 --- a/docs/pip-requirements.txt +++ b/docs/pip-requirements.txt @@ -11,4 +11,5 @@ logilab-common==0.60.0 pep8==1.4.6 pyflakes==0.7.3 pylint==1.0.0 +six==1.3.0 wsgiref==0.1.2 diff --git a/three_d_viewer/ajax.py b/three_d_viewer/ajax.py index 1c31d8b..d4cfb05 100644 --- a/three_d_viewer/ajax.py +++ b/three_d_viewer/ajax.py @@ -14,13 +14,7 @@ def check_answer(request, answerid, questionid): Check whether answerid is the correct answer for questionid. Returns a boolean in 'result' """ - question = Question.objects.get(id=int(questionid)) - - result = False - for answer in question.correct_answers(): - print type(answer.id) - if answer.id == int(answerid): - result = True + result = question.check_answer(answerid) return simplejson.dumps({'result': result}) diff --git a/three_d_viewer/models.py b/three_d_viewer/models.py index e855732..d501a67 100644 --- a/three_d_viewer/models.py +++ b/three_d_viewer/models.py @@ -58,6 +58,16 @@ class Question(models.Model): """ return self.answers.filter(correct=True) + def check_answer(self, answerid): + """ + Check if answerid is correct. Returns True if correct, else False + """ + for answer in self.correct_answers(): + if answer.id == int(answerid): + return True + + return False + def __unicode__(self): return self.text diff --git a/three_d_viewer/tests.py b/three_d_viewer/tests.py index dc44592..0e22446 100644 --- a/three_d_viewer/tests.py +++ b/three_d_viewer/tests.py @@ -38,8 +38,30 @@ class ModelsTest(TestCase): self.assertEqual("Test", testanswer.__unicode__()) def test_no_correct_answers(self): - testquestion = Question(text="Test") + testquestion = Question(text="Test", id=1) - Answer(text="Test", correct=False, question=testquestion) + answer = Answer(text="Test", correct=False, question=testquestion) + answer2 = Answer(text="Test2", correct=False, question=testquestion) + + testquestion.answers = [answer, answer2, ] self.assertEqual(len(testquestion.correct_answers()), 0) + + def test_correct_answers(self): + testquestion = Question(text="Test", id=1) + + answer = Answer(text="Test", correct=True, question=testquestion) + answer2 = Answer(text="Test2", correct=False, question=testquestion) + testquestion.answers = [answer, answer2] + + self.assertEqual(len(testquestion.correct_answers()), 1) + + def test_multiple_correct_answers(self): + testquestion = Question(text="Test", id=1) + + answer = Answer(text="Test", correct=True, question=testquestion) + answer2 = Answer(text="Test2", correct=False, question=testquestion) + answer3 = Answer(text="Test3", correct=True, question=testquestion) + testquestion.answers = [answer, answer2, answer3, ] + + self.assertEqual(len(testquestion.correct_answers()), 2)