def scoreboard(request, competition, data): if data['is_admin'] and 'refresh' in request.POST: start = datetime.now() teams = Team.objects.filter(competition=data['competition']) refresh_teams_cache_score(teams) data['refresh_calculation_time'] = datetime.now() - start extra = {} if data['is_admin'] else {'is_test': False} sort_by_actual_score = data['is_admin'] and \ request.GET.get('sort_by_actual_score') == '1' order_by = '-cache_score' if sort_by_actual_score \ else '-cache_score_before_freeze' teams = list(Team.objects.filter(competition=competition, **extra) \ .order_by(order_by, 'id') \ .only('id', 'name', 'cache_score', 'cache_score_before_freeze', 'cache_max_score_after_freeze', 'is_test')) last_score = -1 last_position = 1 position = 1 for team in teams: team.competition = competition if not team.is_test and team.cache_score_before_freeze != last_score: last_position = position team.t_position = last_position if not team.is_test: last_score = team.cache_score_before_freeze position += 1 data['teams'] = teams data['sort_by_actual_score'] = sort_by_actual_score return data
def scoreboard(request, competition, data): if data['is_admin'] and 'refresh' in request.POST: start = datetime.now() teams = Team.objects.filter(competition=data['competition']) refresh_teams_cache_score(teams) data['refresh_calculation_time'] = datetime.now() - start extra = {} if data['is_admin'] else {'team_type': Team.TYPE_NORMAL} sort_by_actual_score = data['is_admin'] and \ request.GET.get('sort_by_actual_score') == '1' order_by = '-cache_score' if sort_by_actual_score \ else '-cache_score_before_freeze' teams = list(Team.objects.filter(competition=competition, **extra) \ .order_by(order_by, 'id') \ .only('id', 'name', 'cache_score', 'cache_score_before_freeze', 'cache_max_score_after_freeze', 'team_type', 'category')) try: team_categories = parse_team_categories(competition.team_categories) except ValueError: team_categories = [] team_categories_dict = dict(team_categories) data['team_categories_title'] = u", ".join(team_categories_dict.values()) last_score = -1 last_position = 1 position = 1 for team in teams: team.competition = competition if team.is_normal() and team.cache_score_before_freeze != last_score: last_position = position team.t_position = last_position if team_categories: team.t_category = team_categories_dict.get(team.category, team_categories[-1][1]) if team.is_normal(): last_score = team.cache_score_before_freeze position += 1 data['teams'] = teams data['sort_by_actual_score'] = sort_by_actual_score return data
def simulate_submissions(self, submissions): """ Submissions is a list of tuples: (ctask, date, action, expected_score_triple), Action can be: True -> sending a correct solution False -> sending an incorrect solution int x -> ID of the solution (assume 1-based). """ team = self.team for index, test_case in enumerate(submissions): try: ctask, date, action, expected_score_triple = test_case if isinstance(action, bool): is_correct = action result = "42" if is_correct else "0" submission = Submission.objects.create(ctask=ctask, team=team, date=date, result=result, cache_is_correct=is_correct) update_score_on_ctask_action(self.competition, team, self.chain, ctask, submission, False) else: # Raise an exception if not found. submission = Submission.objects.get(id=action) submission.delete() update_score_on_ctask_action(self.competition, team, self.chain, ctask, None, True) score_triple = (team.cache_score_before_freeze, team.cache_score, team.cache_max_score_after_freeze) refresh_teams_cache_score([team]) score_triple = (team.cache_score_before_freeze, team.cache_score, team.cache_max_score_after_freeze) self.assertEqual(score_triple, expected_score_triple) except: print "Test case #{}: {}".format(index, test_case) raise
def refresh_cache_score(self, request, queryset): refresh_teams_cache_score(queryset)
def test_refresh_score_no_submissions(self): self.set_freeze_date(self.now) refresh_teams_cache_score([self.team]) self.assertEqual(self.team.cache_score, 0) self.assertEqual(self.team.cache_score_before_freeze, 0) self.assertEqual(self.team.cache_max_score_after_freeze, 0)