def post_data(self): round = self.get_round() self.log_action() if round.draw_status == Round.STATUS_RELEASED: info = _( "Draw is already released, unrelease draw to redo auto-allocations." ) logger.warning(info) raise BadJsonRequestError(info) if round.draw_status != Round.STATUS_CONFIRMED: info = _( "Draw is not confirmed, confirm draw to run auto-allocations.") logger.warning(info) raise BadJsonRequestError(info) if self.get_tournament().pref('ballots_per_debate') == 'per-adj': allocator_class = HungarianAllocator else: allocator_class = ConsensusHungarianAllocator allocate_adjudicators(self.get_round(), allocator_class) return { 'debates': self.get_draw(), 'unallocatedAdjudicators': self.get_unallocated_adjudicators() }
def modify_debate(self, debate, posted_debate): posted_debateteams = posted_debate['debateTeams'] # Check that all sides are present, and without extras sides = [dt['side'] for dt in posted_debateteams] if set(sides) != set(self.tournament.sides): raise BadJsonRequestError("Sides in JSON object weren't correct") # Delete existing entries that won't be wanted (there shouldn't be any, but just in case) delete_count, deleted = debate.debateteam_set.exclude(side__in=self.tournament.sides).delete() logger.debug("Deleted %d debate teams from [%s]", deleted.get('draw.DebateTeam', 0), debate.matchup) # Check that all teams are part of the tournament team_ids = [dt['team']['id'] for dt in posted_debateteams] teams = Team.objects.filter(tournament=self.tournament, id__in=team_ids) if len(teams) != len(posted_debateteams): raise BadJsonRequestError("Not all teams specified are associated with the tournament") team_name_lookup = {team.id: team.short_name for team in teams} # for debugging messages # Update other DebateTeam objects for dt in posted_debateteams: team_id = dt['team']['id'] side = dt['side'] obj, created = DebateTeam.objects.update_or_create(debate=debate, side=side, defaults={'team_id': team_id}) logger.debug("%s debate team: %s in [%s] is now %s", "Created" if created else "Updated", side, debate.matchup, team_name_lookup[team_id]) debate._populate_teams() return debate
def check_matrix_exists(self, n_debates, n_voting): if n_voting == 0: info = _( "There are no adjudicators eligible to be a chair or " "panellist. This usually means that you need to go to the " "Draw Rules section of the Configuration area and " "decrease the \"Minimum adjudicator score to vote\" setting " "in order to allow some adjudicators to be allocated.") logger.info("No adjudicators able to panel or chair") raise BadJsonRequestError(info) if n_debates == 0: info = _("There are no debates for this round. " "Maybe you haven't created a draw yet?") logger.info("No debates available for allocator") raise BadJsonRequestError(info)
def post_data(self): self.log_action() if self.round.draw_status == Round.STATUS_RELEASED: info = "Draw is already released, unrelease draw to redo auto-allocations." logger.warning(info) raise BadJsonRequestError(info) if self.round.draw_status != Round.STATUS_CONFIRMED: info = "Draw is not confirmed, confirm draw to run auto-allocations." logger.warning(info) raise BadJsonRequestError(info) allocate_venues(self.round) return { 'debates': self.get_draw(), 'unallocatedVenues': self.get_unallocated_venues() }
def modify_debate(self, debate, posted_debate): posted_debateadjudicators = posted_debate['debateAdjudicators'] # Delete adjudicators who aren't in the posted information adj_ids = [da['adjudicator']['id'] for da in posted_debateadjudicators] delete_count, deleted = debate.debateadjudicator_set.exclude( adjudicator_id__in=adj_ids).delete() logger.debug("Deleted %d debate adjudicators from [%s]", delete_count, debate.matchup) # Check all the adjudicators are part of the tournament adjs = Adjudicator.objects.filter(Q(tournament=self.get_tournament()) | Q(tournament__isnull=True), id__in=adj_ids) if len(adjs) != len(posted_debateadjudicators): raise BadJsonRequestError( _("Not all adjudicators specified are associated with the tournament." )) adj_name_lookup = {adj.id: adj.name for adj in adjs} # for debugging messages # Update or create positions of adjudicators in debate for debateadj in posted_debateadjudicators: adj_id = debateadj['adjudicator']['id'] adjtype = debateadj['position'] obj, created = DebateAdjudicator.objects.update_or_create( debate=debate, adjudicator_id=adj_id, defaults={'type': adjtype}) logger.debug("%s debate adjudicator: %s is now %s in [%s]", "Created" if created else "Updated", adj_name_lookup[adj_id], obj.get_type_display(), debate.matchup) return debate
def __init__(self, debates, adjudicators, round): self.tournament = round.tournament self.debates = list(debates) self.adjudicators = adjudicators if len(self.adjudicators) == 0: info = _("There are no available adjudicators. Ensure there are " "adjudicators who have been marked as available for this " "round before auto-allocating.") logger.info(info) raise BadJsonRequestError(info)
def post_data(self): try: posted_debate = json.loads(self.body) except ValueError: logger.exception("Bad JSON provided for drag-and-drop edit") raise BadJsonRequestError("Malformed JSON provided") debate = self.get_debate(posted_debate['id']) debate = self.modify_debate(debate, posted_debate) self.log_action(content_object=debate) return json.dumps(debate.serialize())
def get_debate(self, id): """Returns the debate with ID `id`. If the debate doesn't exist and `self.allows_creation` is True, it creates a new debate (and saves it) and returns it. If the debate doesn't exist and `self.allows_creation` is False, it raises a BadJsonRequestError. """ r = self.round try: return Debate.objects.get(round=r, pk=id) except Debate.DoesNotExist: if not self.allows_creation: logger.exception("Debate with ID %d in round %s doesn't exist, and allows_creation was False", id, r) raise BadJsonRequestError("Debate ID %d doesn't exist" % (id,)) logger.info("Debate with ID %d in round %s doesn't exist, creating new debate", id, r.name) return Debate.objects.create(round=r)
def legacy_allocate_adjudicators(round, alloc_class): """@deprecate when legacy drag and drop UIs removed""" if round.draw_status != Round.STATUS_CONFIRMED: raise RuntimeError( "Tried to allocate adjudicators on unconfirmed draw") debates = round.debate_set.all() adjs = list(round.active_adjudicators.all()) allocator = alloc_class(debates, adjs, round) try: allocation, _ = allocator.allocate() except AdjudicatorAllocationError as e: # legacy views expect BadJsonRequestError, not AdjudicatorAllocationError raise BadJsonRequestError(e) for alloc in allocation: alloc.save()