From: tomwalker Date: Mon, 4 Aug 2014 19:39:45 +0000 (+0100) Subject: made quiz.progress.list_all_cat_scores a property, changed quiz.progress.update_score... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=bacc13fd57796936a69302873c0618c70f9cd0c8;p=django_quiz.git made quiz.progress.list_all_cat_scores a property, changed quiz.progress.update_score use a question rather than a category as its calling arguement. This is to allow easier integration of tracking subcategory scores (which has not been implemented yet) --- diff --git a/quiz/models.py b/quiz/models.py index a2bf6f1..86c9ab7 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -173,6 +173,7 @@ class Progress(models.Model): verbose_name = "User Progress" verbose_name_plural = "User progress records" + @property def list_all_cat_scores(self): """ Returns a dict in which the key is the category name and the item is @@ -215,46 +216,24 @@ class Progress(models.Model): return output - def check_cat_score(self, category_queried): - """ - Pass in a category, get the users score and possible maximum score - as the integers x,y respectively - """ - category_test = Category.objects.filter(category=category_queried) \ - .exists() - - if category_test is False: - return "error", "category does not exist" - - to_find = re.escape(category_queried) +\ - r",(?P\d+),(?P\d+)," - match = re.search(to_find, self.score, re.IGNORECASE) - - if match: - return int(match.group('score')), int(match.group('possible')) - - else: # if not found but category exists, add category with 0 points - self.score += category_queried + ",0,0," - self.save() - - return 0, 0 - - def update_score(self, category, score_to_add=0, possible_to_add=0): + def update_score(self, question, score_to_add=0, possible_to_add=0): """ - Pass in string of the category name, amount to increase score + Pass in question object, amount to increase score and max possible. Does not return anything. """ - category_test = Category.objects.filter(category=category) \ + category_test = Category.objects.filter(category=question.category)\ .exists() - if any([category_test is False, score_to_add is False, - possible_to_add is False, str(score_to_add).isdigit() is False, + 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" - to_find = re.escape(str(category)) +\ + to_find = re.escape(str(question.category)) +\ r",(?P\d+),(?P\d+)," match = re.search(to_find, self.score, re.IGNORECASE) @@ -264,7 +243,7 @@ class Progress(models.Model): updated_possible = int(match.group('possible')) +\ abs(possible_to_add) - new_score = (str(category) + "," + + new_score = (str(question.category) + "," + str(updated_score) + "," + str(updated_possible) + ",") @@ -274,7 +253,7 @@ class Progress(models.Model): else: # if not present but existing, add with the points passed in - self.score += (str(category) + "," + + self.score += (str(question.category) + "," + str(score_to_add) + "," + str(possible_to_add) + ",") self.save() diff --git a/quiz/tests.py b/quiz/tests.py index ab22bb6..26908e5 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -119,60 +119,54 @@ class TestProgress(TestCase): description='d1', url='tq1') + self.question1 = MCQuestion.objects.create(content='squawk', + category=self.c1) + self.user = User.objects.create_user(username='jacob', email='jacob@jacob.com', password='top_secret') + self.p1 = Progress.objects.new_progress(self.user) + def test_list_all_empty(self): - p1 = Progress.objects.new_progress(self.user) - self.assertEqual(p1.score, '') + self.assertEqual(self.p1.score, '') - category_dict = p1.list_all_cat_scores() + category_dict = self.p1.list_all_cat_scores - self.assertIn(str(category_dict.keys()[0]), p1.score) + self.assertIn(str(category_dict.keys()[0]), self.p1.score) - self.assertIn(self.c1.category, p1.score) + self.assertIn(self.c1.category, self.p1.score) Category.objects.new_category(category='cheese') - p1.list_all_cat_scores() - - self.assertIn('cheese', p1.score) - - def test_check_cat(self): - p1 = Progress.objects.new_progress(self.user) - elderberry_score = p1.check_cat_score('elderberries') - - self.assertEqual(elderberry_score, (0, 0)) - - fake_score = p1.check_cat_score('monkey') + self.p1.list_all_cat_scores - self.assertEqual(fake_score, ('error', 'category does not exist')) + self.assertIn('cheese', self.p1.score) - Category.objects.new_category(category='cheese') - cheese_score = p1.check_cat_score('cheese') + def test_subcategory_all_empty(self): + SubCategory.objects.create(sub_category='pickles', + category=self.c1) + self.p1.list_all_cat_scores - self.assertEqual(cheese_score, (0, 0)) - self.assertIn('cheese', p1.score) + # self.assertIn('pickles', self.p1.score) def test_update_score(self): - p1 = Progress.objects.new_progress(self.user) - p1.list_all_cat_scores() - p1.update_score('elderberries', 1, 2) - elderberry_score = p1.check_cat_score('elderberries') - - self.assertEqual(elderberry_score, (1, 2)) + self.p1.list_all_cat_scores + self.p1.update_score(self.question1, 1, 2) + self.assertIn('elderberries', self.p1.list_all_cat_scores) - Category.objects.new_category(category='cheese') - p1.update_score('cheese', 3, 4) - cheese_score = p1.check_cat_score('cheese') + cheese = Category.objects.new_category(category='cheese') + question2 = MCQuestion.objects.create(content='squeek', + category=cheese) + self.p1.update_score(question2, 3, 4) - self.assertEqual(cheese_score, (3, 4)) + self.assertIn('cheese', self.p1.list_all_cat_scores) + self.assertEqual([3, 4, 75], self.p1.list_all_cat_scores['cheese']) - fake_cat = p1.update_score('hamster', 3, 4) - self.assertIn('error', str(fake_cat)) + with self.assertRaises(AttributeError): + self.p1.update_score('hamster', 3, 4) - non_int = p1.update_score('cheese', '1', 'a') + non_int = self.p1.update_score(question2, '1', 'a') self.assertIn('error', str(non_int)) @@ -334,13 +328,15 @@ class TestNonQuestionViews(TestCase): self.assertTemplateNotUsed(response, 'progress.html') def test_progress_user(self): - self.user = User.objects.create_user(username='jacob', - email='jacob@jacob.com', - password='top_secret') + user = User.objects.create_user(username='jacob', + email='jacob@jacob.com', + password='top_secret') + question1 = MCQuestion.objects.create(content='squawk', + category=self.c1) self.client.login(username='jacob', password='top_secret') - p1 = Progress.objects.new_progress(self.user) - p1.update_score(self.c1.category, 1, 2) + p1 = Progress.objects.new_progress(user) + p1.update_score(question1, 1, 2) response = self.client.get('/progress/') self.assertContains(response, 'elderberries') @@ -732,8 +728,7 @@ class TestQuestionViewsUser(TestCase): sitting = Sitting.objects.get(quiz=self.quiz1) progress_count = Progress.objects.count() - progress = Progress.objects.get(user=sitting.user)\ - .list_all_cat_scores() + progress = Progress.objects.get(user=sitting.user).list_all_cat_scores self.assertContains(response, 'previous question', status_code=200) self.assertEqual(sitting.current_score, 0) diff --git a/quiz/views.py b/quiz/views.py index 6e52588..66c5125 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -76,7 +76,7 @@ class QuizUserProgressView(TemplateView): def get_context_data(self, **kwargs): context = super(QuizUserProgressView, self).get_context_data(**kwargs) progress, c = Progress.objects.get_or_create(user=self.request.user) - context['cat_scores'] = progress.list_all_cat_scores() + context['cat_scores'] = progress.list_all_cat_scores context['exams'] = progress.show_exams() return context @@ -175,10 +175,10 @@ class QuizTake(FormView): if is_correct is True: self.sitting.add_to_score(1) - progress.update_score(self.question.category, 1, 1) + progress.update_score(self.question, 1, 1) else: self.sitting.add_incorrect_question(self.question) - progress.update_score(self.question.category, 0, 1) + progress.update_score(self.question, 0, 1) if self.quiz.answers_at_end is not True: self.previous = {'previous_answer': guess,