Skip to content

Commit c867d69

Browse files
committed
XML Import: Use DebateResult for results
This commit replaces all the logic to determine the result of a debate, such as the margin/splits, etc. with completing a DebateResult object and saving that. This commit refers to methods created as part of TabbycatDebate#1180.
1 parent 5403217 commit c867d69

File tree

1 file changed

+23
-101
lines changed

1 file changed

+23
-101
lines changed

tabbycat/importer/archive.py

+23-101
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from statistics import mean
21
from xml.etree.ElementTree import Element, SubElement
32

43
from django.core.exceptions import ObjectDoesNotExist
@@ -15,7 +14,7 @@
1514
UADCPreferences, WADLPreferences, WSDCPreferences)
1615
from participants.emoji import EMOJI_BY_NAME
1716
from participants.models import Adjudicator, Institution, Region, Speaker, SpeakerCategory, Team
18-
from results.models import BallotSubmission, SpeakerScore, SpeakerScoreByAdj, Submission, TeamScore
17+
from results.models import BallotSubmission, Submission
1918
from results.prefetch import populate_confirmed_ballots, populate_wins
2019
from results.result import (BaseConsensusDebateResultWithSpeakers, BaseDebateResultWithSpeakers,
2120
BaseEliminationDebateResult, DebateResult, VotingDebateResult)
@@ -595,35 +594,6 @@ def import_motions(self):
595594
motion_obj.save()
596595
self.motions[motion.get('id')] = motion_obj
597596

598-
def _get_margin(self, debate):
599-
side_ts = []
600-
for side in debate.findall('side'):
601-
scores = []
602-
for b in side.findall("ballot[@ignored='False']"):
603-
try:
604-
scores.append(float(b.text))
605-
except ValueError:
606-
continue
607-
side_ts.append(mean(scores))
608-
return (max(side_ts), max(side_ts) - min(side_ts))
609-
610-
def _get_adj_split(self, debate, numeric):
611-
split = [0, 0, 0, 0]
612-
side_ballot_by_adj = {adj: [] for adj in self._get_voting_adjs(debate)}
613-
for side in debate.findall('side'):
614-
for ballot in side.findall('ballot'):
615-
for adj in ballot.get('adjudicators').split():
616-
if numeric:
617-
side_ballot_by_adj[adj] = float(ballot.text)
618-
else:
619-
side_ballot_by_adj[adj] = int(ballot.text == 'True')
620-
for adj, scores in side_ballot_by_adj.items():
621-
max_score = max(scores)
622-
for i, score in enumerate(split):
623-
if score == max_score:
624-
split[i] += 1
625-
return (split, len(side_ballot_by_adj))
626-
627597
def import_results(self):
628598
for round in self.root.findall('round'):
629599
consensus = self.preliminary_consensus if round.get('elimination') == 'False' else self.elimination_consensus
@@ -633,89 +603,41 @@ def import_results(self):
633603
version=1, submitter_type=Submission.SUBMITTER_TABROOM, confirmed=True,
634604
debate=self.debates[debate.get('id')], motion=self.motions.get(debate.get('motion'))
635605
)
636-
ballotsubmissions.append(bs_obj)
606+
bs_obj.save()
607+
dr = DebateResult(bs_obj)
637608

638-
ts_max, ts_margin = (0, 0)
639609
numeric_scores = True
640610
try:
641611
float(debate.find("side/ballot").text)
642612
except ValueError:
643613
numeric_scores = False
644614

645-
if not self.is_bp and numeric_scores:
646-
ts_max, ts_margin = self._get_margin(debate)
647-
648-
adj_split, num_adjs = (None, None)
649-
if not consensus:
650-
adj_split, num_adjs = self._get_adj_split(debate, numeric_scores)
651-
652-
for i, side in enumerate(debate.findall('side')):
653-
dt = self.debateteams.get((debate.get('id'), side.get('team')))
615+
for side, side_code in zip(debate.findall('side'), self.tournament.sides):
654616

655617
if side.get('motion-veto') is not None:
656-
motion_veto = DebateTeamMotionPreference(
657-
debate_team=dt, motion=self.motions.get(side.get('motion-veto')), preference=3
618+
bs_obj.debateteammotionpreference_set.add(
619+
debate_team=self.debateteams.get((debate.get('id'), side.get('team'))),
620+
motion=self.motions.get(side.get('motion-veto')), preference=3
658621
)
659-
motion_vetos_bs.append((motion_veto, bs_obj))
660-
661-
team_ballot = side.find("ballot[@ignored='False']")
662-
points = 4 - int(team_ballot.get('rank')) if self.is_bp else 2 - int(team_ballot.get('rank'))
663-
664-
not_ignored = side.findall("ballot[@ignored='False']")
665-
666-
ts_obj = TeamScore(debate_team=dt, points=points)
667-
teamscores.append((ts_obj, bs_obj))
668-
669-
if not self.is_bp:
670-
ts_obj.win = points == 1
671-
672-
if numeric_scores:
673-
scores = []
674-
for b in not_ignored:
675-
try:
676-
scores.append(float(b.text))
677-
except ValueError:
678-
continue
679-
ts_obj.score = mean(scores)
680-
if not self.is_bp:
681-
ts_obj.margin = ts_margin if float(team_ballot.text) == ts_max else -ts_margin
682-
else:
683-
ts_obj.win = team_ballot.text # Boolean for advancing otherwise
684622

623+
for speech, pos in zip(side.findall('speech'), self.tournament.positions):
624+
if numeric_scores:
625+
if consensus:
626+
dr.set_score(side_code, pos, float(speech.find('ballot').get('score')))
627+
else:
628+
for ballot in speech.findall('ballot'):
629+
for adj in [self.adjudicators[a] for a in ballot.get('adjudicators', []).split(" ")]:
630+
dr.set_score(adj, side_code, pos, ballot.get('score'))
631+
# Note: Dependent on #1180
685632
if consensus:
686-
ts_obj.votes_given = 1 if ts_obj.win else 0
687-
ts_obj.votes_possible = 1
633+
if side.find('ballot').get('rank') == 1:
634+
dr.add_winner(side_code)
688635
else:
689-
ts_obj.votes_given = adj_split[i]
690-
ts_obj.votes_possible = num_adjs
691-
692-
for i, speech in enumerate(side.findall('speech'), 1):
693-
ss_obj = None
694-
if consensus:
695-
speech_ballot = speech.find('ballot')
696-
697-
ss_obj = SpeakerScore(debate_team=dt, speaker=self.speakers[speech.get('speaker')],
698-
score=float(speech_ballot.text), position=i)
699-
else:
700-
speech_ballots = speech.findall('ballot')
701-
for speech_ballot in speech_ballots:
702-
d_adj = self.debateadjudicators.get((debate.get('id'), speech_ballot.get('adjudicators')))
703-
ss_adj_obj = SpeakerScoreByAdj(debate_adjudicator=d_adj,
704-
debate_team=dt, score=float(speech_ballot.text), position=i)
705-
speakerscores_adj.append((ss_adj_obj, bs_obj))
706-
707-
included_adjs = [b.get('adjudicators') for b in not_ignored]
708-
ss_obj = SpeakerScore(debate_team=dt, speaker=self.speakers[speech.get('speaker')], position=i,
709-
score=mean([float(b.score) for b in speech_ballots if b.adjudicators in included_adjs])
710-
)
711-
speakerscores.append((ss_obj, bs_obj))
712-
713-
BallotSubmission.objects.bulk_create(ballotsubmissions)
714-
715-
DebateTeamMotionPreference.objects.bulk_create(self._add_foreign_key(motion_vetos_bs, 'ballot_submission'))
716-
TeamScore.objects.bulk_create(self._add_foreign_key(teamscores, 'ballot_submission'))
717-
SpeakerScore.objects.bulk_create(self._add_foreign_key(speakerscores, 'ballot_submission'))
718-
SpeakerScoreByAdj.objects.bulk_create(self._add_foreign_key(speakerscores_adj, 'ballot_submission'))
636+
for ballot in side.findall('ballot'):
637+
for adj in [self.adjudicators[a] for a in ballot.get('adjudicators', []).split(" ")]:
638+
if ballot.get('rank') == 1:
639+
dr.add_winner(adj, side_code, side_code)
640+
dr.save()
719641

720642
def import_feedback(self):
721643
for adj in self.root.findall('participants/adjudicator'):

0 commit comments

Comments
 (0)