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
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<score>\d+),(?P<possible>\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<score>\d+),(?P<possible>\d+),"
match = re.search(to_find, self.score, re.IGNORECASE)
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) + ",")
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()
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))
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')
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)