Exemplo n.º 1
0
    def make_debates(self, pairings):
        from draw.models import Debate, DebateTeam
        import random

        venues = list(self.active_venues.order_by('-priority'))[:len(pairings)]

        if len(venues) < len(pairings):
            raise DrawError("There are %d debates but only %d venues." %
                            (len(pairings), len(venues)))

        random.shuffle(venues)
        random.shuffle(pairings)  # to avoid IDs indicating room ranks

        for pairing in pairings:
            try:
                if pairing.division:
                    if (pairing.teams[0].type == "B") or (
                            pairing.teams[1].type == "B"):
                        # If the match is a bye then they don't get a venue
                        selected_venue = None
                    else:
                        selected_venue = next(
                            v
                            for v in venues
                            if v.group == pairing.division.venue_group)
                        venues.pop(venues.index(selected_venue))
                else:
                    selected_venue = venues.pop(0)
            except:
                print("Error assigning venues")
                selected_venue = None

            debate = Debate(round=self, venue=selected_venue)

            debate.division = pairing.division
            debate.bracket = pairing.bracket
            debate.room_rank = pairing.room_rank
            debate.flags = ",".join(pairing.flags)  # comma-separated list
            debate.save()

            aff = DebateTeam(debate=debate,
                             team=pairing.teams[0],
                             position=DebateTeam.POSITION_AFFIRMATIVE)
            neg = DebateTeam(debate=debate,
                             team=pairing.teams[1],
                             position=DebateTeam.POSITION_NEGATIVE)

            aff.save()
            neg.save()