Change leaderboard to support multiple submissions

WARNING: Makes get_leaderboard O(num teams)
This commit is contained in:
Ian Adam Naval 2014-11-02 21:45:39 -05:00
parent a88e67a7d8
commit 9f1d7d6d99
2 changed files with 8 additions and 9 deletions

View File

@ -49,13 +49,12 @@ class Team(models.Model):
return points[0]['total_points'] if points else 0
@classmethod
def get_leaderboard(self):
sum_points = models.Sum('player__solution__challenge__points')
def get_leaderboard(cls):
"""Gets the leaderboard by most points and most recent solution."""
max_solved_at = models.Max('player__solution__solved_at')
all_annotated = Team.objects.all().annotate(points=sum_points)
all_annotated = all_annotated.annotate(solved_at=max_solved_at)
all_sorted = all_annotated.order_by('-points', 'solved_at')
return all_sorted
all_annotated = Team.objects.all().annotate(solved_at=max_solved_at)
all_sorted = all_annotated.order_by('solved_at')
return sorted(all_sorted, key=lambda t: t.points(), reverse=True)
def save(self, *args, **kwargs):
if not hasattr(self, 'creator'):

View File

@ -110,9 +110,9 @@ def test_leaderboard_counts_challenges_once(challenge, player_factory, fresh_tea
other_team.save()
member = Player(username='player', password='', team=other_team)
member.save()
Solution(challenge=challenge, solver=member).save()
Solution(challenge=challenge, solver=other_team.creator).save()
Solution(challenge=other_challenge, solver=fresh_team.creator).save()
Solution(challenge=other_challenge, solver=member).save()
Solution(challenge=other_challenge, solver=other_team.creator).save()
Solution(challenge=challenge, solver=fresh_team.creator).save()
leaderboard = Team.get_leaderboard()
assert (leaderboard[0] == other_team and
leaderboard[1] == fresh_team and