def get_table_by_team(self): round = self.get_round() tournament = self.get_tournament() teamscores = TeamScore.objects.filter( debate_team__debate__round=round, ballot_submission__confirmed=True).prefetch_related( 'debate_team__team__speaker_set', 'debate_team__team__institution', 'debate_team__debate__debateadjudicator_set__adjudicator', 'debate_team__debate__debateteam_set__team', 'debate_team__debate__round').select_related( 'ballot_submission') debates = [ts.debate_team.debate for ts in teamscores] if tournament.pref('teams_in_debate') == 'two': populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots( debates, motions=True, results=tournament.pref('ballots_per_debate') == 'per-adj') table = TabbycatTableBuilder(view=self, sort_key=_("Team")) table.add_team_columns([ts.debate_team.team for ts in teamscores]) table.add_debate_result_by_team_column(teamscores) table.add_debate_side_by_team_column(teamscores) if not (tournament.pref('teams_in_debate') == 'bp' and round.is_break_round): table.add_debate_ballot_link_column(debates) table.add_debate_adjudicators_column(debates, show_splits=True) if tournament.pref('show_motions_in_results'): table.add_debate_motion_column(debates) return table
def get_table_by_team(self): round = self.get_round() tournament = self.get_tournament() teamscores = TeamScore.objects.filter(debate_team__debate__round=round, ballot_submission__confirmed=True).prefetch_related( 'debate_team__team__speaker_set', 'debate_team__team__institution', 'debate_team__debate__debateadjudicator_set__adjudicator') debates = [ts.debate_team.debate for ts in teamscores] populate_opponents([ts.debate_team for ts in teamscores]) for pos in [DebateTeam.POSITION_AFFIRMATIVE, DebateTeam.POSITION_NEGATIVE]: debates_for_pos = [ts.debate_team.debate for ts in teamscores if ts.debate_team.position == pos] populate_confirmed_ballots(debates_for_pos, motions=True) table = TabbycatTableBuilder(view=self, sort_key="Team") table.add_team_columns([ts.debate_team.team for ts in teamscores]) table.add_debate_result_by_team_columns(teamscores) table.add_debate_ballot_link_column(debates) table.add_debate_adjudicators_column(debates, show_splits=True) if tournament.pref('show_motions_in_results'): table.add_motion_column([debate.confirmed_ballot.motion if debate.confirmed_ballot else None for debate in debates]) return table
def annotate(self, queryset, standings, round=None): if not queryset.exists(): return logger.info("Running points query for draw strength:") prefetch_queryset = DebateTeam.objects.filter( debate__round__stage=Round.STAGE_PRELIMINARY) if round is not None: prefetch_queryset = prefetch_queryset.filter( debate__round__seq__lte=round.seq) points_queryset = PointsMetricAnnotator().get_annotated_queryset( queryset[0].tournament.team_set.all(), 'points', round).prefetch_related( Prefetch('debateteam_set', queryset=prefetch_queryset, to_attr='debateteams')) points_queryset_teams = {team.id: team for team in points_queryset} points_queryset_debateteams = { team.id: list(team.debateteams) for team in points_queryset } populate_opponents( [dt for dts in points_queryset_debateteams.values() for dt in dts]) for team in queryset: draw_strength = 0 for dt in points_queryset_debateteams[team.id]: points = points_queryset_teams[dt.opponent.team_id].points if points is not None: # points is None when no debates have happened draw_strength += points standings.add_metric(team, self.key, draw_strength)
def add_team_round_results(standings, rounds, lookup=None, id_attr='instance_id'): """Sets, on each item `info` in `standings`, an attribute `info.round_results` to be a list of `TeamScore` objects, one for each round in `rounds` (in the same order), relating to the team associated with that item. If, for some team and round, there is no relevant `TeamScore`, then the corresponding element of `info.round_results` will be `None`. If `lookup` is given, it should be a function that takes two arguments `(standings, x)` and returns the element in `standings` relating to the `Team` object `x`. By default, it just uses `standings.get_standings(x)`. """ if lookup is None: lookup = lambda standings, x: standings.get_standing(x) # flake8: noqa teams = [getattr(info, id_attr) for info in standings] teamscores = TeamScore.objects.select_related( 'debate_team__team', 'debate_team__debate__round').filter( ballot_submission__confirmed=True, debate_team__debate__round__in=rounds, debate_team__team_id__in=teams ) populate_opponents([ts.debate_team for ts in teamscores]) for info in standings: info.round_results = [None] * len(rounds) round_lookup = {r: i for i, r in enumerate(rounds)} for ts in teamscores: info = lookup(standings, ts.debate_team.team) info.round_results[round_lookup[ts.debate_team.debate.round]] = ts
def get_table(self): """On team record pages, the table is the results table.""" tournament = self.get_tournament() teamscores = TeamScore.objects.filter(debate_team__team=self.object, ballot_submission__confirmed=True, debate_team__debate__round__seq__lt=tournament.current_round.seq, debate_team__debate__round__draw_status=Round.STATUS_RELEASED, debate_team__debate__round__silent=False).select_related( 'debate_team__debate', 'debate_team__debate__round').prefetch_related( 'debate_team__debate__debateadjudicator_set__adjudicator') debates = [ts.debate_team.debate for ts in teamscores] populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots(debates, motions=True, ballotsets=True) table = TabbycatTableBuilder(view=self, title="Results", sort_key="Round") table.add_round_column([debate.round for debate in debates]) table.add_debate_result_by_team_columns(teamscores) table.add_debate_adjudicators_column(debates, show_splits=True) if self.admin or tournament.pref('public_motions'): table.add_motion_column([debate.confirmed_ballot.motion if debate.confirmed_ballot else None for debate in debates]) table.add_debate_ballot_link_column(debates) return table
def add_team_round_results(standings, rounds, lookup=None, id_attr='instance_id'): """Sets, on each item `info` in `standings`, an attribute `info.round_results` to be a list of `TeamScore` objects, one for each round in `rounds` (in the same order), relating to the team associated with that item. If, for some team and round, there is no relevant `TeamScore`, then the corresponding element of `info.round_results` will be `None`. If `lookup` is given, it should be a function that takes two arguments `(standings, x)` and returns the element in `standings` relating to the `Team` object `x`. By default, it just uses `standings.get_standings(x)`. """ if lookup is None: lookup = lambda standings, x: standings.get_standing(x) # flake8: noqa teams = [getattr(info, id_attr) for info in standings] teamscores = TeamScore.objects.select_related( 'debate_team__team', 'debate_team__debate__round').filter( ballot_submission__confirmed=True, debate_team__debate__round__in=rounds, debate_team__team_id__in=teams) populate_opponents([ts.debate_team for ts in teamscores]) for info in standings: info.round_results = [None] * len(rounds) round_lookup = {r: i for i, r in enumerate(rounds)} for ts in teamscores: info = lookup(standings, ts.debate_team.team) info.round_results[round_lookup[ts.debate_team.debate.round]] = ts
def get_table_by_team(self): teamscores = TeamScore.objects.filter( debate_team__debate__round=self.round, ballot_submission__confirmed=True).prefetch_related( 'debate_team__team__speaker_set', 'debate_team__debate__debateadjudicator_set__adjudicator', 'debate_team__debate__debateadjudicator_set__adjudicator__institution', 'debate_team__debate__debateteam_set__team').select_related( 'ballot_submission', 'debate_team__team__institution', 'debate_team__debate__round') debates = [ts.debate_team.debate for ts in teamscores] if self.tournament.pref('teams_in_debate') == 'two': populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots(debates, motions=True, results=self.round.ballots_per_debate == 'per-adj') table = TabbycatTableBuilder(view=self, sort_key="team") table.add_team_columns([ts.debate_team.team for ts in teamscores]) table.add_debate_result_by_team_column(teamscores) table.add_debate_side_by_team_column(teamscores, self.tournament) if not (self.tournament.pref('teams_in_debate') == 'bp' and self.round.is_break_round): table.add_debate_ballot_link_column(debates) table.add_debate_adjudicators_column(debates, show_splits=True) if self.tournament.pref('show_motions_in_results'): table.add_debate_motion_column(debates) return table
def annotate(self, queryset, standings, round=None): if not queryset.exists(): return logger.info("Running points query for draw strength:") prefetch_queryset = DebateTeam.objects.filter(debate__round__stage=Round.STAGE_PRELIMINARY) if round is not None: prefetch_queryset = prefetch_queryset.filter(debate__round__seq__lte=round.seq) points_queryset = PointsMetricAnnotator().get_annotated_queryset( queryset[0].tournament.team_set.all(), 'points', round).prefetch_related( Prefetch('debateteam_set',queryset=prefetch_queryset, to_attr='debateteams')) points_queryset_teams = {team.id: team for team in points_queryset} points_queryset_debateteams = {team.id: list(team.debateteams) for team in points_queryset} populate_opponents([dt for dts in points_queryset_debateteams.values() for dt in dts]) for team in queryset: draw_strength = 0 for dt in points_queryset_debateteams[team.id]: points = points_queryset_teams[dt.opponent.team_id].points if points is not None: # points is None when no debates have happened draw_strength += points standings.add_metric(team, self.key, draw_strength)
def get_table(cls, view, participant): """On team record pages, the table is the results table.""" table = TeamResultTableBuilder(view=view, title=view.table_title, sort_key="round") tournament = view.tournament teamscores = TeamScore.objects.filter( debate_team__team=participant, ballot_submission__confirmed=True, ).select_related( 'debate_team__debate__round__tournament', ).prefetch_related( Prefetch('debate_team__debate__debateadjudicator_set', queryset=DebateAdjudicator.objects.select_related( 'adjudicator__institution')), 'debate_team__debate__debateteam_set__team', 'debate_team__debate__round__motion_set', Prefetch('debate_team__speakerscore_set', queryset=SpeakerScore.objects.filter( ballot_submission__confirmed=True).select_related( 'speaker').order_by('position'), to_attr='speaker_scores'), ).order_by('debate_team__debate__round__seq') if not table.admin and not tournament.pref('all_results_released'): teamscores = teamscores.filter( debate_team__debate__round__draw_status=Round.STATUS_RELEASED, debate_team__debate__round__silent=False, debate_team__debate__round__completed=True, ) debates = [ts.debate_team.debate for ts in teamscores] populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots(debates, motions=True, results=True) table.add_round_column([debate.round for debate in debates]) table.add_debate_result_by_team_column(teamscores) table.add_cumulative_team_points_column(teamscores) if table.admin or tournament.pref( 'all_results_released') and tournament.pref( 'speaker_tab_released') and tournament.pref( 'speaker_tab_limit') == 0: table.add_speaker_scores_column(teamscores) table.add_debate_side_by_team_column(teamscores) table.add_debate_adjudicators_column(debates, show_splits=True) if table.admin or tournament.pref('public_motions'): table.add_debate_motion_column(debates) if not table.private_url: table.add_debate_ballot_link_column(debates) return table
def get_table(self): """On team record pages, the table is the results table.""" tournament = self.tournament teamscores = TeamScore.objects.filter( debate_team__team=self.object, ballot_submission__confirmed=True, ).select_related( 'debate_team__debate__round__tournament' ).prefetch_related( Prefetch('debate_team__debate__debateadjudicator_set', queryset=DebateAdjudicator.objects.select_related('adjudicator__institution')), 'debate_team__debate__debateteam_set__team', 'debate_team__debate__round__motion_set', Prefetch('debate_team__speakerscore_set', queryset=SpeakerScore.objects.filter(ballot_submission__confirmed=True).select_related('speaker').order_by('position'), to_attr='speaker_scores'), ).order_by('debate_team__debate__round__seq') if not self.admin and not tournament.pref('all_results_released'): teamscores = teamscores.filter( debate_team__debate__round__draw_status=Round.STATUS_RELEASED, debate_team__debate__round__silent=False, debate_team__debate__round__seq__lt=tournament.current_round.seq ) debates = [ts.debate_team.debate for ts in teamscores] populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots(debates, motions=True, results=True) table = TeamResultTableBuilder(view=self, title=_("Results"), sort_key="round") table.add_round_column([debate.round for debate in debates]) table.add_debate_result_by_team_column(teamscores) table.add_cumulative_team_points_column(teamscores) if self.admin or tournament.pref('all_results_released') and tournament.pref('speaker_tab_released') and tournament.pref('speaker_tab_limit') == 0: table.add_speaker_scores_column(teamscores) table.add_debate_side_by_team_column(teamscores) table.add_debate_adjudicators_column(debates, show_splits=True) if self.admin or tournament.pref('public_motions'): table.add_debate_motion_column(debates) table.add_debate_ballot_link_column(debates) return table
def get_table(self): """On team record pages, the table is the results table.""" tournament = self.get_tournament() teamscores = TeamScore.objects.filter( debate_team__team=self.object, ballot_submission__confirmed=True, debate_team__debate__round__draw_status=Round.STATUS_RELEASED ).select_related('debate_team__debate__round').prefetch_related( Prefetch('debate_team__debate__debateadjudicator_set', queryset=DebateAdjudicator.objects.select_related( 'adjudicator__institution')), 'debate_team__debate__debateteam_set') if not tournament.pref('all_results_released'): teamscores = teamscores.filter( debate_team__debate__round__silent=False, debate_team__debate__round__seq__lt=tournament.current_round. seq) debates = [ts.debate_team.debate for ts in teamscores] populate_opponents([ts.debate_team for ts in teamscores]) populate_confirmed_ballots(debates, motions=True, ballotsets=True) table = TabbycatTableBuilder(view=self, title="Results", sort_key="Round") table.add_round_column([debate.round for debate in debates]) table.add_debate_result_by_team_columns(teamscores) table.add_debate_adjudicators_column(debates, show_splits=True) if self.admin or tournament.pref('public_motions'): table.add_motion_column([ debate.confirmed_ballot.motion if debate.confirmed_ballot else None for debate in debates ]) table.add_debate_ballot_link_column(debates) return table