예제 #1
0
        def create(self, validated_data):
            result = DebateResult(validated_data['ballot'], tournament=self.context.get('tournament'))

            sheets = self.SheetSerializer(context=self.context)
            for sheet in validated_data['sheets']:
                sheets._validated_data = sheet
                sheets.save(result=result)

            result.save()
            return result
예제 #2
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()
예제 #3
0
파일: dbutils.py 프로젝트: vicbab/tabbycat
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
예제 #4
0
파일: dbutils.py 프로젝트: tfpk/tabbycat
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
예제 #5
0
파일: dbutils.py 프로젝트: czlee/tabbycat
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