def round_with_results(self): from results.controllers.ResultsController import ResultsController controller = ResultsController() if controller.results_entered_for_round(self.round): return self.round else: return self.round - 1
def get(self, request): team_standing_controller = TeamStandingController() results_controller = ResultsController() max_round = results_controller.max_round_with_results() template = loader.get_template('display/team_standing.html') context = RequestContext(request, { 'table' : team_standing_controller.team_standing_table_all_rounds(), 'max_round' : max_round, 'all_rounds' : range(1, max_round+1) }) return HttpResponse(template.render(context))
class TeamStandingController: points_controller = None results_controller = None def __init__(self): self.points_controller = PointsController() self.results_controller = ResultsController() def team_standing_table_all_rounds(self): return self.team_standing_table(self.results_controller.max_round_with_results()) def team_standing_table(self, max_round): rows = [] teams = list(Team.objects.all()) teams.sort(key=lambda team: (team.total_team_points, team.total_speaker_sum), reverse=True) for t in range(0, len(teams)): team = teams[t] if (team.total_speaker_sum == teams[t - 1].total_speaker_sum) and ( team.total_team_points == teams[t - 1].total_team_points ): row = ["--", team] else: row = [t + 1, team] for r in range(1, max_round + 1): row.append(self.points_controller.team_points_for_team(team, r)) row.append(team.total_speaker_sum) row.append(team.total_team_points) rows.append(row) return rows
def get(self, request): template = loader.get_template('draw/index.html') team_count = Team.objects.all().count() results_controller = ResultsController() tournament = Tournament.instance() context = RequestContext(request, { 'teams_count' : Team.objects.all().count(), 'teams_ok' : (team_count % 4 == 0) and (team_count >= 4), 'rooms' : team_count / 4, 'venues_ok' : Venue.objects.all().count() >= team_count/4, 'tournament' : tournament, 'this_round' : tournament.round, 'next_round' : tournament.round + 1, 'results_entered' : results_controller.results_entered_for_round(tournament.round) }) return HttpResponse(template.render(context))
class PointsController(): results_controller = None def __init__(self): self.results_controller = ResultsController() def team_points_for_team(self, team, round): result = self.results_controller.result_for_team(team, round) debate = result.debate if debate.OG == team: return result.og if debate.OO == team: return result.oo if debate.CG == team: return result.cg if debate.CO == team: return result.co def total_points_for_team(self, team, maxround): total = 0 for round in range(1, maxround+1): total += self.team_points_for_team(team, round) return total def speaker_points_for_team(self, team, round): result = self.results_controller.result_for_team(team, round) debate = result.debate if debate.OG == team: return [result.ogsp1, result.ogsp2] if debate.OO == team: return [result.oosp1, result.oosp2] if debate.CG == team: return [result.cgsp1, result.cgsp2] if debate.CO == team: return [result.cosp1, result.cosp2] def team_points_map_for_round(self, round, teams): points = {} for team in teams: points.update({team: self.team_points_for_team(team, round)}) return points
def setUp(self): self.resultController = ResultsController() self.result = Result() self.debate = MagicMock() self.debateController = MagicMock() self.team = MagicMock self.debateController.debate_for_round.return_value = self.debate self.debate.result.return_value = self.result self.resultController.debate_controller = self.debateController
class ResultControllerTestCase(TestCase): def setUp(self): self.resultController = ResultsController() self.result = Result() self.debate = MagicMock() self.debateController = MagicMock() self.team = MagicMock self.debateController.debate_for_round.return_value = self.debate self.debate.result.return_value = self.result self.resultController.debate_controller = self.debateController def testRaisesExceptionForRoundNotDrawn(self): tournament = Tournament.instance() tournament.round = 1 tournament.save() with self.assertRaises(TournamentStateException): self.resultController.results_entered_for_round(10) def testReturnsFalseForRoundWithoutAnyResults(self): generate_objects.setup_IV_R1() self.assertFalse(self.resultController.results_entered_for_round(1), "No results entered but returned true") def testReturnsTrueForRoundWithAllResults(self): generate_objects.setup_IV_R1() for debate in Debate.objects.filter(round=1): generate_objects.valid_result_given_debate(debate) self.assertTrue(self.resultController.results_entered_for_round(1), "All results entered but returned false") def testReturnsFalseForPartiallyEnteredRound(self): generate_objects.setup_IV_R1() generate_objects.valid_result_given_debate(Debate.objects.first()) self.assertFalse(self.resultController.results_entered_for_round(1), "Only some results entered but returned true") def testGetResultForRound(self): return_value = self.resultController.result_for_team(self.team, 1) self.assertEqual(self.debate.result, return_value)
class DrawController: resultsController = None pointsController = None def __init__(self): self.resultsController = ResultsController() self.pointsController = PointsController() def draw_next_round(self): tournament = Tournament.instance() next_round = tournament.round + 1 pools = self.create_pools(Team.objects.all(), tournament.round) pools = self.remove_empty(pools) pools = self.shuffle_pools(pools) pools = self.balance_pools(pools) debates = self.draw_from_pools(next_round, pools) tournament.round = next_round tournament.save() mapper = VenueMapper() mapper.map_venues(next_round) return debates def create_pools(self, teams, max_round): if not self.resultsController.results_entered_for_round(Tournament.instance().round): raise TournamentStateException("All results for current round must be entered to draw") pools = self.create_blank_pools(max_round) for team in teams: points = self.pointsController.total_points_for_team(team, 1) pools[points].append(team) return pools @staticmethod def create_blank_pools(max_round): pools = {} if max_round == 0: return pools for points in range(0, 4 + ((max_round - 1) * 3)): pools.update({points: []}) return pools @staticmethod def remove_empty(pools): keys = pools.keys() for key in keys: pool = pools[key] if len(pool) < 1: pools.pop(key) return pools @staticmethod def next_viable_pool(current, pools): for i in range(current + 1, len(pools.values())): next_pool = pools.values()[i] if len(next_pool) != 0: return next_pool raise ValueError("No viable pool") @staticmethod def shuffle_pools(pools): for pool in pools.values(): random.shuffle(pool) return pools def balance_pools(self, pools): flattened_pools = [item for sublist in pools.values() for item in sublist] if len(flattened_pools) % 4 != 0: raise ValueError("Number of teams must be divisible by 4") made_swap = True while made_swap: made_swap = False for i in range(0, len(pools.values())): pool = pools.values()[i] if (len(pool) % 4 != 0) and pool != pools.values()[-1]: teams_needed = 4 - (len(pool) % 4) for t in range(0, teams_needed): source_pool = self.next_viable_pool(i, pools) pool.append(source_pool.pop(0)) made_swap = True return pools def draw_from_pools(self, round, pools): debates = [] for pool in pools.values(): if len(pool) % 4 != 0: raise ValueError("Number of teams must be divisible by four") num_debates = len(pool) / 4 for i in range(0, num_debates): debate = Debate(round=round) debate.OG = pool[(i * 4)] debate.OO = pool[(i * 4) + 1] debate.CG = pool[(i * 4) + 2] debate.CO = pool[(i * 4) + 3] debate.full_clean() debate.save() debates.append(debate) return debates
def __init__(self): self.resultsController = ResultsController() self.pointsController = PointsController()
def __init__(self): self.results_controller = ResultsController()