From aba6caca3517a415e0bd8ae05457b6bbb7314b31 Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Sun, 14 Jun 2015 14:58:19 +0100 Subject: [PATCH 1/3] add league user --- scripts/fix_average_score.py | 24 ++++++++++++++++++++---- src/api/question.py | 7 +++++-- src/models.py | 5 ++++- src/tests/test_api_question.py | 2 +- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/scripts/fix_average_score.py b/scripts/fix_average_score.py index 31f58e6..34347e6 100755 --- a/scripts/fix_average_score.py +++ b/scripts/fix_average_score.py @@ -31,13 +31,16 @@ def auth_func(): return os.environ['USER_EMAIL'], getpass.getpass('Password:') - -def fix_user(u): - uqs = [uq for uq in models.UserQuestion.query( +def get_uqs(u): + return [uq for uq in models.UserQuestion.query( models.UserQuestion.user==u.key, models.UserQuestion.complete==True, ndb.OR(models.UserQuestion.score==0, models.UserQuestion.score==None))] +def fix_user(u): + uqs = get_uqs(u) + lus = models.LeagueUser.query(models.LeagueUser.user==u.key) + correction = 0 for uq in uqs: @@ -47,9 +50,22 @@ def fix_user(u): u.active_questions_answered = u.questions_answered - correction print '%s: old: %d, new: %d' % (u.username, u.questions_answered, u.active_questions_answered) - if correction > 1: + if correction > 0: u.put() + for lu in lus: + l_correction = 0 + league = lu.league.get() + for uq in uqs: + if (uq.created and + uq.created.date() > league.created and + (uq.created - uq.question.get().posed) > datetime.timedelta(days=7)): + l_correction += 1 + lu.active_questions_answered = lu.questions_answered - l_correction + print ' league - %s: old: %d, new: %d' % (u.username, lu.questions_answered, lu.active_questions_answered) + if l_correction > 0: + lu.put() + def main(): # Use 'localhost:8080' for dev server. diff --git a/src/api/question.py b/src/api/question.py index 5ff45e8..8ce2ef4 100644 --- a/src/api/question.py +++ b/src/api/question.py @@ -45,7 +45,7 @@ def update_users_season_score(user_season, score, num_guesses): user_season.questions_answered += 1 -def update_users_league_scores(user_key, score, num_guesses): +def update_users_league_scores(user_key, score, num_guesses, is_current): to_put = [] user = user_key.get() for league_key in user.leagues: @@ -53,6 +53,8 @@ def update_users_league_scores(user_key, score, num_guesses): user_league.score += score user_league.clues += num_guesses - 1 user_league.questions_answered += 1 + if is_current: + user_league.active_questions_answered += 1 to_put.append(user_league) ndb.put_multi(to_put) @@ -130,7 +132,8 @@ def get_or_post(self, question_id): deferred.defer(update_users_league_scores, user.key, score, - num_guesses) + num_guesses, + question.is_current) # Update the user season. if question.season and not user.is_anonymous: diff --git a/src/models.py b/src/models.py index 6aef162..beca613 100755 --- a/src/models.py +++ b/src/models.py @@ -254,6 +254,7 @@ def to_leaderboard_json(user): 'score': user.overall_score, 'clues': user.overall_clues, 'answered': user.questions_answered, + 'active_answered': user.active_questions_answered, } @@ -380,7 +381,7 @@ def to_leaderboard_json(user_season): 'user_pic': user.pic_url(size=30), 'score': user_season.score, 'clues': user_season.clues, - 'answered': user_season.questions_answered, + 'answered': user_season.questions_answered } @@ -456,6 +457,7 @@ class LeagueUser(ndb.Model): score = ndb.IntegerProperty(default=0) clues = ndb.IntegerProperty(default=0) questions_answered = ndb.IntegerProperty(default=0) + active_questions_answered = ndb.IntegerProperty(default=0) @classmethod def from_league_user(cls, league_key, user_key): @@ -480,4 +482,5 @@ def to_leaderboard_json(league_user): 'score': league_user.score, 'clues': league_user.clues, 'answered': league_user.questions_answered, + 'active_answered': league_user.active_questions_answered, } \ No newline at end of file diff --git a/src/tests/test_api_question.py b/src/tests/test_api_question.py index 120c8ed..7177ada 100644 --- a/src/tests/test_api_question.py +++ b/src/tests/test_api_question.py @@ -244,7 +244,7 @@ def testUpdateUsersLeagueScore(self): clues=5, questions_answered=1) league_user.put() - question_api.update_users_league_scores(user_key, 5, 5) + question_api.update_users_league_scores(user_key, 5, 5, False) self.assertEqual(league_user.score, 10) self.assertEqual(league_user.clues, 9) self.assertEqual(league_user.questions_answered, 2) From 6a2a645342c0fe359ea8358941a80370fef0231b Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Sun, 14 Jun 2015 15:23:38 +0100 Subject: [PATCH 2/3] add active questions answered to api response for users and leagueusers --- scripts/fix_average_score.py | 4 ++-- src/models.py | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/scripts/fix_average_score.py b/scripts/fix_average_score.py index 34347e6..24029d5 100755 --- a/scripts/fix_average_score.py +++ b/scripts/fix_average_score.py @@ -50,7 +50,7 @@ def fix_user(u): u.active_questions_answered = u.questions_answered - correction print '%s: old: %d, new: %d' % (u.username, u.questions_answered, u.active_questions_answered) - if correction > 0: + if u.active_questions_answered > 0: u.put() for lu in lus: @@ -63,7 +63,7 @@ def fix_user(u): l_correction += 1 lu.active_questions_answered = lu.questions_answered - l_correction print ' league - %s: old: %d, new: %d' % (u.username, lu.questions_answered, lu.active_questions_answered) - if l_correction > 0: + if lu.active_questions_answered > 0: lu.put() diff --git a/src/models.py b/src/models.py index beca613..5ff4954 100755 --- a/src/models.py +++ b/src/models.py @@ -253,8 +253,7 @@ def to_leaderboard_json(user): 'user_pic': user.pic_url(size=30), 'score': user.overall_score, 'clues': user.overall_clues, - 'answered': user.questions_answered, - 'active_answered': user.active_questions_answered, + 'answered': user.active_questions_answered } @@ -481,6 +480,5 @@ def to_leaderboard_json(league_user): 'user_pic': user.pic_url(size=30), 'score': league_user.score, 'clues': league_user.clues, - 'answered': league_user.questions_answered, - 'active_answered': league_user.active_questions_answered, + 'answered': league_user.active_questions_answered } \ No newline at end of file From 11faf8e4b6eefca5a12de33ac894c0eb245c7d0d Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Sun, 14 Jun 2015 15:33:48 +0100 Subject: [PATCH 3/3] fix tests --- src/tests/helpers.py | 1 + src/tests/test_api_leaderboard.py | 6 ++++-- src/tests/test_model_league_user.py | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tests/helpers.py b/src/tests/helpers.py index 91d2917..a6276a5 100644 --- a/src/tests/helpers.py +++ b/src/tests/helpers.py @@ -95,6 +95,7 @@ def league_user(id=None, league=None, score=None, clues=None, + active_questions_answered=None, questions_answered=None): return models.LeagueUser(**locals()) diff --git a/src/tests/test_api_leaderboard.py b/src/tests/test_api_leaderboard.py index 38d07ed..bd19540 100644 --- a/src/tests/test_api_leaderboard.py +++ b/src/tests/test_api_leaderboard.py @@ -42,7 +42,8 @@ def setUp(self): username='user-%d' % i, overall_score=i, overall_clues=i, - questions_answered=i)) + questions_answered=i, + active_questions_answered=i)) to_put.append(helpers.user_question( user_is_admin=False, complete=True, @@ -55,7 +56,8 @@ def setUp(self): user=user_key, score=i, clues=i, - questions_answered=i)) + questions_answered=i, + active_questions_answered=i)) to_put.append(helpers.user_season( score=i, clues=i, diff --git a/src/tests/test_model_league_user.py b/src/tests/test_model_league_user.py index 92718d7..486c808 100644 --- a/src/tests/test_model_league_user.py +++ b/src/tests/test_model_league_user.py @@ -31,6 +31,7 @@ def testToLeaderboardJson(self): league_user.score = 1 league_user.clues = 2 league_user.questions_answered = 3 + league_user.active_questions_answered = 3 league_user_json = models.LeagueUser.to_leaderboard_json(league_user) self.assertEqual(league_user_json['user_name'], 'foo') self.assertEqual(league_user_json['score'], 1)