def _create_debate(self, teams, adjs, votes, trainees=[], venue=None): """Enters a debate into the database, using the teams and adjudicators specified. `votes` should be a string (or iterable of characters) indicating "a" for affirmative or "n" for negative, e.g. "ann" if the chair was rolled in a decision for the negative. The method will give the winning team all 76s and the losing team all 74s. The first adjudicator is the chair; the rest are panellists.""" if venue is None: venue = Venue.objects.first() debate = Debate.objects.create(round=self.rd, venue=venue) aff, neg = teams aff_team = self._team(aff) DebateTeam.objects.create(debate=debate, team=aff_team, position=DebateTeam.POSITION_AFFIRMATIVE) neg_team = self._team(neg) DebateTeam.objects.create(debate=debate, team=neg_team, position=DebateTeam.POSITION_NEGATIVE) chair = self._adj(adjs[0]) DebateAdjudicator.objects.create(debate=debate, adjudicator=chair, type=DebateAdjudicator.TYPE_CHAIR) for p in adjs[1:]: panellist = self._adj(p) DebateAdjudicator.objects.create(debate=debate, adjudicator=panellist, type=DebateAdjudicator.TYPE_PANEL) for tr in trainees: trainee = self._adj(tr) DebateAdjudicator.objects.create(debate=debate, adjudicator=trainee, type=DebateAdjudicator.TYPE_TRAINEE) ballotsub = BallotSubmission(debate=debate, submitter_type=BallotSubmission.SUBMITTER_TABROOM) ballotset = BallotSet(ballotsub) for t in teams: team = self._team(t) speakers = team.speaker_set.all() for pos, speaker in enumerate(speakers, start=1): ballotset.set_speaker(team, pos, speaker) ballotset.set_ghost(team, pos, False) ballotset.set_speaker(team, 4, speakers[0]) ballotset.set_ghost(team, 4, False) for a, vote in zip(adjs, votes): adj = self._adj(a) if vote == 'a': teams = [aff_team, neg_team] elif vote == 'n': teams = [neg_team, aff_team] else: raise ValueError for team, score in zip(teams, (76, 74)): for pos in range(1, 4): ballotset.set_score(adj, team, pos, score) ballotset.set_score(adj, team, 4, score / 2) ballotset.confirmed = True ballotset.save() return debate
def add_ballotset(debate, submitter_type, user, discarded=False, confirmed=False, min_score=72, max_score=78, reply_random=False): """Adds a ballot set to a debate. ``debate`` is the Debate to which the ballot set should be added. ``submitter_type`` is a valid value of BallotSubmission.submitter_type. ``user`` is a User object. ``discarded`` and ``confirmed`` are whether the feedback should be discarded or confirmed, respectively. ``min_score`` and ``max_score`` are the range in which scores should be generated.""" if discarded and confirmed: raise ValueError("Ballot can't be both discarded and confirmed!") last_substantive_position = debate.round.tournament.LAST_SUBSTANTIVE_POSITION reply_position = debate.round.tournament.REPLY_POSITION # Create a new BallotSubmission bsub = BallotSubmission(submitter_type=submitter_type, debate=debate) if submitter_type == BallotSubmission.SUBMITTER_TABROOM: bsub.submitter = user bsub.save() def gen_results(): r = {'aff': (0, ), 'neg': (0, )} def do(): s = [ random.randint(min_score, max_score) for i in range(last_substantive_position) ] s.append(random.randint(min_score, max_score) / 2) return s while sum(r['aff']) == sum(r['neg']): r['aff'] = do() r['neg'] = do() return r rr = dict() for adj in debate.adjudicators.voting(): rr[adj] = gen_results() # Create relevant scores bset = BallotSet(bsub) for side in ('aff', 'neg'): speakers = getattr(debate, '%s_team' % side).speakers for i in range(1, last_substantive_position + 1): bset.set_speaker(team=side, position=i, speaker=speakers[i - 1]) bset.set_ghost(side, i, False) reply_speaker = random.randint(0, last_substantive_position - 1) if reply_random else 0 bset.set_speaker(team=side, position=reply_position, speaker=speakers[reply_speaker]) bset.set_ghost(side, reply_position, False) for adj in debate.adjudicators.voting(): for pos in debate.round.tournament.POSITIONS: bset.set_score(adj, side, pos, rr[adj][side][pos - 1]) # Pick a motion motions = debate.round.motion_set.all() if motions: motion = random.choice(motions) bset.motion = motion bset.discarded = discarded bset.confirmed = confirmed bset.save() # Update result status (only takes into account marginal effect, does not "fix") if confirmed: debate.result_status = Debate.STATUS_CONFIRMED elif not discarded and debate.result_status != Debate.STATUS_CONFIRMED: debate.result_status = Debate.STATUS_DRAFT debate.save() logger.info("{debate} won by {team} on {motion}".format( debate=debate.matchup, team=bset.aff_win and "affirmative" or "negative", motion=bset.motion and bset.motion.reference or "<No motion>")) return bset