Exemplo n.º 1
0
    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, side=DebateTeam.SIDE_AFF)
        neg_team = self._team(neg)
        DebateTeam.objects.create(debate=debate, team=neg_team, side=DebateTeam.SIDE_NEG)

        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)
        result = VotingDebateResult(ballotsub)

        for t, side in zip(teams, ('aff', 'neg')):
            team = self._team(t)
            speakers = team.speaker_set.all()
            for pos, speaker in enumerate(speakers, start=1):
                result.set_speaker(side, pos, speaker)
                result.set_ghost(side, pos, False)
            result.set_speaker(side, 4, speakers[0])
            result.set_ghost(side, 4, False)

        for a, vote in zip(adjs, votes):
            adj = self._adj(a)
            if vote == 'a':
                sides = ('aff', 'neg')
            elif vote == 'n':
                sides = ('neg', 'aff')
            else:
                raise ValueError
            for side, score in zip(sides, (76, 74)):
                for pos in range(1, 4):
                    result.set_score(adj, side, pos, score)
                result.set_score(adj, side, 4, score / 2)

        ballotsub.confirmed = True
        ballotsub.save()
        result.save()

        return debate
Exemplo n.º 2
0
    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, side=DebateTeam.SIDE_AFF)
        neg_team = self._team(neg)
        DebateTeam.objects.create(debate=debate, team=neg_team, side=DebateTeam.SIDE_NEG)

        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)
        result = VotingDebateResult(ballotsub)

        for t, side in zip(teams, ('aff', 'neg')):
            team = self._team(t)
            speakers = team.speaker_set.all()
            for pos, speaker in enumerate(speakers, start=1):
                result.set_speaker(side, pos, speaker)
                result.set_ghost(side, pos, False)
            result.set_speaker(side, 4, speakers[0])
            result.set_ghost(side, 4, False)

        for a, vote in zip(adjs, votes):
            adj = self._adj(a)
            if vote == 'a':
                sides = ('aff', 'neg')
            elif vote == 'n':
                sides = ('neg', 'aff')
            else:
                raise ValueError
            for side, score in zip(sides, (76, 74)):
                for pos in range(1, 4):
                    result.set_score(adj, side, pos, score)
                result.set_score(adj, side, 4, score / 2)

        ballotsub.confirmed = True
        ballotsub.save()
        result.save()

        return debate
Exemplo n.º 3
0
    def import_results(self):
        for round in self.root.findall('round'):
            consensus = self.preliminary_consensus if round.get('elimination') == 'false' else self.elimination_consensus

            for debate in round.findall('debate'):
                bs_obj = BallotSubmission(
                    version=1, submitter_type=Submission.SUBMITTER_TABROOM, confirmed=True,
                    debate=self.debates[debate.get('id')], motion=self.motions.get(debate.get('motion')))
                bs_obj.save()
                dr = DebateResult(bs_obj)

                numeric_scores = True
                try:
                    float(debate.find("side/ballot").text)
                except ValueError:
                    numeric_scores = False

                for side, side_code in zip(debate.findall('side'), self.tournament.sides):

                    if side.get('motion-veto') is not None:
                        bs_obj.debateteammotionpreference_set.add(
                            debate_team=self.debateteams.get((debate.get('id'), side.get('team'))),
                            motion=self.motions.get(side.get('motion-veto')), preference=3)

                    for speech, pos in zip(side.findall('speech'), self.tournament.positions):
                        if numeric_scores:
                            dr.set_speaker(side_code, pos, self.speakers.get(speech.get('speaker')))
                            if consensus:
                                dr.set_score(side_code, pos, float(speech.find('ballot').text))
                            else:
                                for ballot in speech.findall('ballot'):
                                    for adj in [self.adjudicators[a] for a in ballot.get('adjudicators', "").split(" ")]:
                                        dr.set_score(adj, side_code, pos, float(ballot.text))
                    # Note: Dependent on #1180
                    if consensus:
                        if int(side.find('ballot').get('rank')) == 1:
                            dr.add_winner(side_code)
                    else:
                        for ballot in side.findall('ballot'):
                            for adj in [self.adjudicators.get(a) for a in ballot.get('adjudicators', "").split(" ")]:
                                if int(ballot.get('rank')) == 1:
                                    dr.add_winner(adj, side_code)
                dr.save()
Exemplo n.º 4
0
    def _save_complete_ballotset(self,
                                 teams,
                                 testdata,
                                 post_ballotset_create=None):
        # unconfirm existing ballot
        try:
            existing = BallotSubmission.objects.get(debate=self.debate,
                                                    confirmed=True)
        except BallotSubmission.DoesNotExist:
            pass
        else:
            existing.confirmed = False
            existing.save()

        ballotsub = BallotSubmission(
            debate=self.debate,
            submitter_type=BallotSubmission.SUBMITTER_TABROOM)
        ballotset = BallotSet(ballotsub)
        if post_ballotset_create:
            post_ballotset_create(ballotset)
        scores = testdata['scores']

        for team in teams:
            speakers = self._get_team(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 adj, sheet in zip(self.adjs, scores):
            for team, teamscores in zip(teams, sheet):
                for pos, score in enumerate(teamscores, start=1):
                    ballotset.set_score(adj, team, pos, score)

        ballotset.confirmed = True
        ballotset.save()

        return ballotset
Exemplo n.º 5
0
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])

        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])

        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
Exemplo n.º 6
0
def add_result(debate,
               submitter_type,
               user,
               discarded=False,
               confirmed=False,
               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!")

    t = debate.round.tournament

    if not debate.sides_confirmed:
        debate.sides_confirmed = True
        debate.save()

    # Create a new BallotSubmission
    bsub = BallotSubmission(submitter_type=submitter_type, debate=debate)
    if submitter_type == BallotSubmission.SUBMITTER_TABROOM:
        bsub.submitter = user
    bsub.save()

    # Create relevant scores
    result = DebateResult(bsub)

    if result.uses_speakers:
        for side in t.sides:
            speakers = list(debate.get_team(side).speakers)  # fix order
            for i in range(1, t.last_substantive_position + 1):
                result.set_speaker(side, i, speakers[i - 1])
                result.set_ghost(side, i, False)

            if t.reply_position is not None:
                reply_speaker = random.randint(0, t.last_substantive_position -
                                               2) if reply_random else 0
                result.set_speaker(side, t.reply_position,
                                   speakers[reply_speaker])
                result.set_ghost(side, t.reply_position, False)

    if result.is_voting:
        for scoresheet in result.scoresheets.values():
            fill_scoresheet_randomly(scoresheet, t)
    elif result.uses_advancing:
        result.set_advancing(random.sample(t.sides, 2))
    else:
        fill_scoresheet_randomly(result.scoresheet, t)

    assert result.is_valid()
    result.save()

    # Pick a motion
    motions = debate.round.motion_set.all()
    if motions:
        num_motions = 3 if motions.count() > 3 else motions.count()
        sample = random.sample(list(motions), k=num_motions)
        motion = sample[0]
        bsub.motion = motion

        if t.pref('motion_vetoes_enabled') and len(sample) == len(t.sides) + 1:
            for i, side in enumerate(t.sides, 1):
                dt = debate.get_dt(side)
                dt.debateteammotionpreference_set.create(
                    motion=sample[i],
                    preference=3,
                    ballot_submission=bsub,
                )

    bsub.discarded = discarded
    bsub.confirmed = confirmed

    bsub.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()

    if t.pref('teams_in_debate') == 'two':
        logger.info(
            "%(debate)s won by %(team)s on %(motion)s", {
                'debate': debate.matchup,
                'team': result.winning_side(),
                'motion': bsub.motion and bsub.motion.reference
                or "<No motion>"
            })
    elif t.pref('teams_in_debate') == 'bp':
        if result.uses_advancing:
            logger.info(
                "%(debate)s: %(advancing)s on %(motion)s", {
                    'debate':
                    debate.matchup,
                    'advancing':
                    ", ".join(result.advancing_sides()),
                    'motion':
                    bsub.motion and bsub.motion.reference or "<No motion>"
                })
        else:
            logger.info(
                "%(debate)s: %(ranked)s on %(motion)s", {
                    'debate':
                    debate.matchup,
                    'ranked':
                    ", ".join(result.scoresheet.ranked_sides()),
                    'motion':
                    bsub.motion and bsub.motion.reference or "<No motion>"
                })

    return result
Exemplo n.º 7
0
def add_result(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!")

    t = debate.round.tournament

    # Create a new BallotSubmission
    bsub = BallotSubmission(submitter_type=submitter_type, debate=debate)
    if submitter_type == BallotSubmission.SUBMITTER_TABROOM:
        bsub.submitter = user
    bsub.save()

    # Create relevant scores
    result = DebateResult(bsub)

    for side in t.sides:
        speakers = debate.get_team(side).speakers
        for i in range(1, t.last_substantive_position + 1):
            result.set_speaker(side, i, speakers[i - 1])
            result.set_ghost(side, i, False)

        if t.reply_position is not None:
            reply_speaker = random.randint(0, t.last_substantive_position -
                                           1) if reply_random else 0
            result.set_speaker(side, t.reply_position, speakers[reply_speaker])
            result.set_ghost(side, t.reply_position, False)

    if result.is_voting:
        for scoresheet in result.scoresheets.values():
            fill_scoresheet_randomly(scoresheet, t)
    else:
        fill_scoresheet_randomly(result.scoresheet, t)

    result.save()

    # Pick a motion
    motions = debate.round.motion_set.all()
    if motions:
        motion = random.choice(motions)
        bsub.motion = motion

    bsub.discarded = discarded
    bsub.confirmed = confirmed

    bsub.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()

    if t.pref('teams_in_debate') == 'two':
        logger.info(
            "%(debate)s won by %(team)s on %(motion)s", {
                'debate': debate.matchup,
                'team': result.winning_side(),
                'motion': bsub.motion and bsub.motion.reference
                or "<No motion>"
            })
    elif t.pref('teams_in_debate') == 'bp':
        logger.info(
            "%(debate)s: %(ranked)s on %(motion)s", {
                'debate': debate.matchup,
                'ranked': ", ".join(result.scoresheet.ranked_sides()),
                'motion': bsub.motion and bsub.motion.reference
                or "<No motion>"
            })

    return result
Exemplo n.º 8
0
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
Exemplo n.º 9
0
def add_result(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!")

    t = debate.round.tournament

    # Create a new BallotSubmission
    bsub = BallotSubmission(submitter_type=submitter_type, debate=debate)
    if submitter_type == BallotSubmission.SUBMITTER_TABROOM:
        bsub.submitter = user
    bsub.save()

    # Create relevant scores
    result = DebateResult(bsub)

    for side in t.sides:
        speakers = list(debate.get_team(side).speakers)  # fix order
        for i in range(1, t.last_substantive_position+1):
            result.set_speaker(side, i, speakers[i-1])
            result.set_ghost(side, i, False)

        if t.reply_position is not None:
            reply_speaker = random.randint(0, t.last_substantive_position-1) if reply_random else 0
            result.set_speaker(side, t.reply_position, speakers[reply_speaker])
            result.set_ghost(side, t.reply_position, False)

    if result.is_voting:
        for scoresheet in result.scoresheets.values():
            fill_scoresheet_randomly(scoresheet, t)
    else:
        fill_scoresheet_randomly(result.scoresheet, t)

    assert result.is_valid()
    result.save()

    # Pick a motion
    motions = debate.round.motion_set.all()
    if motions:
        motion = random.choice(motions)
        bsub.motion = motion

    bsub.discarded = discarded
    bsub.confirmed = confirmed

    bsub.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()

    if t.pref('teams_in_debate') == 'two':
        logger.info("%(debate)s won by %(team)s on %(motion)s", {
            'debate': debate.matchup,
            'team': result.winning_side(),
            'motion': bsub.motion and bsub.motion.reference or "<No motion>"
        })
    elif t.pref('teams_in_debate') == 'bp':
        logger.info("%(debate)s: %(ranked)s on %(motion)s", {
            'debate': debate.matchup,
            'ranked': ", ".join(result.scoresheet.ranked_sides()),
            'motion': bsub.motion and bsub.motion.reference or "<No motion>"
        })

    return result