예제 #1
0
 def import_venues(self):
     for line in self.load_table('venues'):
         id, name, active, priority, c, u = line.split('\t')
         v = m.Venue(tournament=self.tournament,
                     name=name.strip(),
                     priority=int(priority))
         v.save()
         self.venues[int(id)] = v
예제 #2
0
def main():
    m.Tournament.objects.filter(slug='preaustrals').delete()
    t = m.Tournament(slug='preaustrals')
    t.save()

    for i in range(1, 6):
        if i == 1:
            rtype = m.Round.TYPE_RANDOM
        else:
            rtype = m.Round.TYPE_PRELIM

        m.Round(
            tournament=t,
            seq=i,
            name='Round %d' % i,
            type=rtype,
            feedback_weight=min((i - 1) * 0.1, 0.5),
        ).save()

    t.current_round = m.Round.objects.get(tournament=t, seq=1)
    t.save()

    reader = csv.reader(open('institutions.csv'))
    for code, name in reader:
        i = m.Institution(code=code, name=name, tournament=t)
        i.save()

    reader = csv.reader(open('speakers.csv'))
    for _, ins_name, name, in reader:
        print ins_name
        ins = m.Institution.objects.get(name=ins_name, tournament=t)
        team_name = ins.code
        team, _ = m.Team.objects.get_or_create(institution=ins, name=team_name)
        m.Speaker(name=name, team=team).save()

    reader = csv.reader(open('judges.csv'))
    for _, ins_name, name, score in reader:
        print ins_name
        ins = m.Institution.objects.get(name=ins_name, tournament=t)
        m.Adjudicator(name=name, institution=ins).save()

    reader = csv.reader(open('venues.csv'))
    for room, priority, group in reader:

        try:
            group = int(group)
        except ValueError:
            group = None

        m.Venue(tournament=t, group=group, name=room, priority=priority).save()
예제 #3
0
    def setUp(self):
        self.t = m.Tournament(slug="resulttest", name="ResultTest")
        self.t.save()
        for i in range(2):
            inst = m.Institution(code="Inst%d"%i, name="Institution %d"%i)
            inst.save()
            team = m.Team(tournament=self.t, institution=inst, reference="Team %d"%i,
                use_institution_prefix=False)
            team.save()
            for j in range(3):
                speaker = m.Speaker(team=team, name="Speaker %d-%d"%(i,j))
                speaker.save()
        inst = m.Institution(code="Indep", name="Independent %d"%i)
        inst.save()
        for i in range(3):
            adj = m.Adjudicator(tournament=self.t, institution=inst,
                    name="Adjudicator %d"%i, test_score=5)
            adj.save()
        venue = m.Venue(name="Venue", priority=10)
        venue.save()

        self.adjs = list(m.Adjudicator.objects.all())
        self.teams = list(m.Team.objects.all())

        self.round = m.Round(tournament=self.t, seq=1, abbreviation="R1")
        self.round.save()
        for venue in m.Venue.objects.all():
            self.round.activate_venue(venue, True)
        self.debate = m.Debate(round=self.round, venue=venue)
        self.debate.save()
        positions = [m.DebateTeam.POSITION_AFFIRMATIVE, m.DebateTeam.POSITION_NEGATIVE]
        for team, pos in zip(self.teams, positions):
            self.round.activate_team(team, True)
            m.DebateTeam(debate=self.debate, team=team, position=pos).save()
        adjtypes = [m.DebateAdjudicator.TYPE_CHAIR, m.DebateAdjudicator.TYPE_PANEL, m.DebateAdjudicator.TYPE_PANEL]
        for adj, adjtype in zip(self.adjs, adjtypes):
            self.round.activate_adjudicator(adj, True)
            m.DebateAdjudicator(debate=self.debate, adjudicator=adj, type=adjtype).save()
예제 #4
0
    def handle(self, *args, **options):
        if len(args) < 1:
            raise CommandError("Not enough arguments.")

        # Getting the command line variable
        folder = args[0]
        total_errors = 0

        # Where to find the data
        base_path = os.path.join(settings.PROJECT_PATH, 'data')
        data_path = os.path.join(base_path, folder)
        self.stdout.write('importing from ' + data_path)

        # Where to find the data
        template_path = os.path.join(base_path, "league_template")
        self.stdout.write('using template from ' + data_path)

        try:
            # if m.Tournament.objects.filter(slug=slugify(unicode(folder))).exists():
            #     self.stdout.write("WARNING! A tournament called '" + folder + "' already exists.")
            #     self.stdout.write("You are about to delete EVERYTHING for this tournament.")
            #     response = raw_input("Are you sure? ")
            #     if response != "yes":
            #         self.stdout.write("Cancelled.")
            #         raise CommandError("Cancelled by user.")
            #     m.Tournament.objects.filter(slug=slugify(unicode(folder))).delete()

            # # Tournament
            # self.stdout.write('**** Attempting to create tournament ' + folder)
            # try:
            #     slug = slugify(unicode(folder))
            #     short_name = (folder[:24] + '..') if len(folder) > 75 else folder
            #     t = m.Tournament(name=folder, short_name=short_name, slug=slugify(unicode(folder)))
            #     t.save()
            # except Exception as inst:
            #     total_errors += 1
            #     print inst

            # self.stdout.write('Made tournament: \t' + folder)

            t = m.Tournament.objects.get(slug=slugify(unicode(folder)))
            self.stdout.write('Found tournament: \t' + folder)

            self.stdout.write('**** Attempting to create rounds ')

            # If importing from the CSV
            try:
                reader = csv.reader(
                    open(os.path.join(template_path, 'rounds.csv')))
                reader.next()  # Skipping header row
            except:
                self.stdout.write('rounds.csv file is missing or damaged')
                reader = None

            if reader:
                rounds_count = 0
                i = 1
                for line in reader:
                    seq = line[0]
                    if not seq:
                        seq = i
                    name = str(line[1])
                    abbv = str(line[2])
                    draw_stage = str(line[3]) or "Preliminary"
                    draw_type = str(line[4]) or "Random"
                    is_silent = int(line[5]) or 0
                    feedback_weight = float(line[6]) or 0.7

                    if draw_stage.lower() in ("preliminary", "p"):
                        draw_stage = "P"
                    elif draw_stage.lower() in ("elimination", "break", "e",
                                                "b"):
                        draw_stage = "E"
                    else:
                        draw_stage = None

                    if draw_type.lower() in ("random", "r"):
                        draw_type = "R"
                    elif draw_type.lower() in ("round-robin", "round robin",
                                               "d"):
                        draw_type = "D"
                    elif draw_type.lower() in ("power-paired", "power paired",
                                               "p"):
                        draw_type = "P"
                    elif draw_type.lower() in ("first elimination",
                                               "first-elimination",
                                               "1st elimination", "1", "e"):
                        draw_type = "F"
                    elif draw_type.lower() in ("subsequent elimination",
                                               "subsequent-elimination",
                                               "2nd elimination", "2"):
                        draw_type = "B"
                    else:
                        draw_type = None

                    if is_silent > 0:
                        is_silent = True
                    else:
                        is_silent = False

                    try:
                        the_round, created = m.Round.objects.get_or_create(
                            tournament=t,
                            seq=seq,
                            name=name,
                            abbreviation=abbv,
                            draw_type=draw_type,
                            stage=draw_stage,
                            feedback_weight=min((int(seq) - 1) * 0.1, 0.5),
                            silent=is_silent)
                        if created:
                            rounds_count += 1
                            i += 1
                            print "Made round: \t\t%s" % name
                    except Exception as inst:
                        total_errors += 1
                        self.stdout.write('Couldnt make round ' + name)
                        print inst

                t.current_round = m.Round.objects.get(tournament=t, seq=1)
                t.save()
                self.stdout.write('**** Created ' + str(rounds_count) +
                                  ' rounds')

            # Config
            try:
                reader = csv.reader(
                    open(os.path.join(template_path, 'config.csv')))
                reader.next()  # Skipping header row
            except:
                self.stdout.write('config.csv file is missing or damaged')
                reader = None

            if reader:
                for line in reader:
                    key = line[0]
                    value_type = line[1]
                    if value_type == "str":
                        value = str(line[2])
                    elif value_type == "int":
                        value = int(line[2])
                    elif value_type == "float":
                        value = float(line[2])
                    elif value_type == "bool":
                        if line[2] == "True":
                            value = True
                        elif line[2] == "False":
                            value = False
                        else:
                            print "Error %s not properly set" % key

                    t.config.set(key, value)
                    print "Made setting \t%s as %s" % (key, value)

            # Venues
            self.stdout.write('**** Attempting to create the venue groups')
            try:
                reader = csv.reader(
                    open(os.path.join(data_path, 'venue_groups.csv')))
                reader.next()  # Skipping header row
            except:
                self.stdout.write(
                    'venues_groups.csv file is missing or damaged')

            venue_count = 0
            venue_group_count = 0
            for line in reader:
                long_name = line[0] or None
                short_name = line[1] or None
                team_capacity = line[2] or None
                try:
                    venue_group, created = m.VenueGroup.objects.get_or_create(
                        name=long_name,
                        short_name=short_name,
                        team_capacity=team_capacity,
                    )

                    if created:
                        print "Made venue group: \t%s" % venue_group
                        venue_group_count = venue_group_count + 1
                    else:
                        print "Matched venue group: \t%s" % venue_group

                except ValueError:
                    total_errors += 1
                    self.stdout.write('Couldnt make venue group ' + group)
                    venue_group = None

            # Venues
            self.stdout.write('**** Attempting to create the venues')
            try:
                reader = csv.reader(open(os.path.join(data_path,
                                                      'venues.csv')))
                reader.next()  # Skipping header row
            except:
                self.stdout.write('venues.csv file is missing or damaged')

            venue_count = 0
            venue_group_count = 0
            for line in reader:
                round_abbv = line[0]
                group_name = line[1]
                time = line[2]

                try:
                    venue_group = m.VenueGroup.objects.get(name=group_name)
                except:
                    try:
                        venue_group = m.VenueGroup.objects.get(
                            short_name=group_name)
                    except Exception as inst:
                        self.stdout.write('Couldnt find the venue group ' +
                                          group_name)
                        total_errors += 1
                        print inst

                try:
                    round_obj = m.Round.objects.get(abbreviation=round_abbv,
                                                    tournament=t)
                except Exception as inst:
                    self.stdout.write('Couldnt find the round ' + round_abbv)
                    total_errors += 1
                    print inst

                venue_rooms = int(float(venue_group.team_capacity) / 2)
                venue_round = m.Round.objects.get(abbreviation=round_abbv,
                                                  tournament=t)

                for i in range(1, venue_rooms + 1):
                    room_name = "Room %s" % i
                    try:
                        venue = m.Venue(tournament=t,
                                        group=venue_group,
                                        name=room_name,
                                        priority=100,
                                        time=time)
                        venue.save()
                        venue_count = venue_count + 1

                        active_venue = m.ActiveVenue(venue=venue,
                                                     round=round_obj).save()

                    except Exception as inst:
                        total_errors += 1
                        self.stdout.write('Couldnt make venue ' + room_name)
                        print inst

            # Teams by Institution
            self.stdout.write('**** Attempting to create the teams')
            try:
                reader = csv.reader(
                    open(
                        os.path.join(data_path,
                                     'institutional_teams_and_prefs.csv'),
                        'rU'))
                reader.next()  # Skipping header row
            except:
                self.stdout.write(
                    'institutional_teams_and_prefs.csv file is missing or damaged'
                )

            institution_tally = {}
            for line in reader:
                try:
                    ins_name = line[0]
                    teams_count = int(line[1])
                    pref1 = line[2] or None
                    pref2 = line[3] or None
                    pref3 = line[4] or None
                    pref4 = line[5] or None
                    pref5 = line[6] or None
                    pref6 = line[7] or None
                    pref7 = line[8] or None
                    pref8 = line[9] or None
                    pref9 = line[10] or None
                    pref10 = line[11] or None
                    venue_preferences = [
                        pref1, pref2, pref3, pref4, pref5, pref6, pref7, pref8,
                        pref9, pref10
                    ]

                    if ins_name not in institution_tally:
                        institution_tally[ins_name] = 0

                    try:
                        ins = m.Institution.objects.get(name=ins_name)
                    except:
                        try:
                            ins = m.Institution.objects.get(code=ins_name)
                        except Exception as inst:
                            self.stdout.write("error with finding inst " +
                                              ins_name)
                            total_errors += 1
                            print type(inst)  # the exception instance
                            print inst  # __str__ allows args to printed directly

                    for i in range(1, teams_count + 1):
                        name = str(i + institution_tally[ins_name])
                        short_name = str(i + institution_tally[ins_name])
                        team, created = m.Team.objects.get_or_create(
                            institution=ins,
                            reference=name,
                            short_reference=short_name,
                            tournament=t,
                            use_institution_prefix=True)
                        team.save()

                        m.Speaker(name="1st Speaker", team=team).save()
                        m.Speaker(name="2nd Speaker", team=team).save()
                        m.Speaker(name="3rd Speaker", team=team).save()
                        m.Speaker(name="Reply Speaker", team=team).save()

                        for index, venue in enumerate(venue_preferences):
                            if venue:
                                try:
                                    venue_group = m.VenueGroup.objects.get(
                                        name=venue)
                                except:
                                    try:
                                        venue_group = m.VenueGroup.objects.get(
                                            short_name=venue)
                                    except Exception as inst:
                                        self.stdout.write(
                                            "error with finding venue: " +
                                            venue)
                                        total_errors += 1
                                        print type(
                                            inst)  # the exception instance
                                        print inst  # __str__ allows args to printed directly

                                preference = m.TeamVenuePreference(
                                    team=team,
                                    venue_group=venue_group,
                                    priority=index)
                                preference.save()

                    institution_tally[ins_name] += teams_count
                    print "Made %s teams for\t%s (total of %s)" % (
                        teams_count, ins, institution_tally[ins_name])

                except Exception as inst:
                    self.stdout.write('Couldnt make the teams for ' + line[0])
                    total_errors += 1
                    print inst

            # Motions
            try:
                reader = csv.reader(
                    open(os.path.join(template_path, 'motions.csv')))
                reader.next()  # Skipping header row
            except:
                self.stdout.write('motions.csv file is missing or damaged')

            motions_count = 0
            for line in reader:
                round_abbv = str(line[0])
                motion_seq = int(line[1])
                reference = str(line[2])
                text = str(line[3])

                try:
                    round = m.Round.objects.get(abbreviation=round_abbv,
                                                tournament=t)
                    m.Motion(round=round,
                             seq=motion_seq,
                             reference=reference,
                             text=text).save()
                    self.stdout.write('Made motion: \t\t' + round_abbv + ': ' +
                                      text)
                    motions_count += 1
                except m.Round.DoesNotExist:
                    total_errors += 1
                    self.stdout.write(
                        'Couldnt find round with abbreviation: ' + round_abbv)

            self.stdout.write('**** Created ' + str(motions_count) +
                              ' motions')

        except Exception:
            import traceback
            traceback.print_exc()
            self.stdout.write('Failed')
예제 #5
0
    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError("Not enough arguments.")

        # Getting the command line variable
        folder = args[0]
        rounds_count = int(args[1])

        # Where to find the data
        base_path = os.path.join(settings.PROJECT_PATH, 'data')
        data_path = os.path.join(base_path, folder)
        self.stdout.write('importing from ' + data_path)

        try:
            if m.Tournament.objects.filter(slug=folder).exists():
                self.stdout.write("WARNING! A tournament called '" + folder +
                                  "' already exists.")
                self.stdout.write(
                    "You are about to delete EVERYTHING for this tournament.")
                response = raw_input("Are you sure? ")
                if response != "yes":
                    self.stdout.write("Cancelled.")
                    raise CommandError("Cancelled by user.")
                m.Tournament.objects.filter(slug=folder).delete()

            # Tournament
            self.stdout.write('*** Attempting to create tournament ' + folder)
            try:
                t = m.Tournament(slug=folder)
                t.save()
            except Exception as inst:
                print inst

            self.stdout.write('*** Created the tournament: ' + folder)

            self.stdout.write('*** Attempting to create rounds ')
            # TODO get this to use rounds.csv
            try:
                for i in range(1, rounds_count + 1):
                    if i == 1:
                        draw_type = m.Round.DRAW_RANDOM
                    else:
                        draw_type = m.Round.DRAW_POWERPAIRED

                    m.Round(
                        tournament=t,
                        seq=i,
                        name='Round %d' % i,
                        abbreviation='R%d' % i,
                        draw_type=draw_type,
                        feedback_weight=min((i - 1) * 0.1, 0.5),
                        silent=(i >= rounds_count),
                    ).save()

                t.current_round = m.Round.objects.get(tournament=t, seq=1)
                t.save()
                self.stdout.write('*** Created ' + str(rounds_count) +
                                  ' rounds')
            except Exception as inst:
                print inst

            # Venues
            self.stdout.write('*** Attempting to create the venues')
            try:
                reader = csv.reader(open(os.path.join(data_path,
                                                      'venues.csv')))
            except:
                self.stdout.write('venues.csv file is missing or damaged')

            venue_count = 0
            for line in reader:
                if len(line) == 3:
                    room, priority, group = line
                    try:
                        group = int(group)
                    except ValueError:
                        group = None
                elif len(line) == 2:
                    room, priority = line
                    group = None
                else:
                    continue

                try:
                    priority = int(priority)
                except ValueError:
                    priority = None

                m.Venue(tournament=t,
                        group=group,
                        name=room,
                        priority=priority).save()
                print room

                venue_count = venue_count + 1

            self.stdout.write('*** Created ' + str(venue_count) + ' venues')

            # Institutions
            self.stdout.write('*** Attempting to create the institutions')
            try:
                reader = csv.reader(
                    open(os.path.join(data_path, 'institutions.csv')))
            except:
                self.stdout.write(
                    'institutions.csv file is missing or damaged')

            institutions_count = 0
            for line in reader:
                if len(line) == 3:
                    abbreviation, code, name = line
                elif len(line) == 2:
                    abbreviation = None
                    code, name = line
                else:
                    continue
                i = m.Institution(code=code, name=name, tournament=t)
                i.save()
                institutions_count = institutions_count + 1
                print name

            self.stdout.write('*** Created ' + str(institutions_count) +
                              ' institutions')

            # Speakers
            self.stdout.write('*** Attempting to create the teams/speakers')
            try:
                reader = csv.reader(
                    open(os.path.join(data_path, 'speakers.csv'), 'rU'))
            except:
                self.stdout.write('speakers.csv file is missing or damaged')

            speakers_count = 0
            teams_count = 0
            for name, ins_name, team_name in reader:
                try:
                    ins = m.Institution.objects.get(code=ins_name)
                except:
                    try:
                        ins = m.Institution.objects.get(name=ins_name)
                    except Exception as inst:
                        self.stdout.write("error with " + ins_name)
                        print type(inst)  # the exception instance
                        print inst  # __str__ allows args to printed directly

                try:
                    team, created = m.Team.objects.get_or_create(
                        institution=ins,
                        reference=team_name,
                        use_institution_prefix=True)
                    if created:
                        teams_count = teams_count + 1
                except Exception as inst:
                    self.stdout.write("error with " + str(team_name))
                    print type(inst)  # the exception instance
                    print inst  # __str__ allows args to printed directly

                # Resetting the variable incase create/get above fails
                speakers_team = m.Team.objects.get(institution=ins,
                                                   reference=team_name)

                name = name.strip()
                try:
                    m.Speaker(name=name, team=speakers_team).save()
                    speakers_count = speakers_count + 1
                except:
                    self.stdout.write('Couldnt make the speaker ' + name)

                print team, "-", name

            self.stdout.write('*** Created ' + str(speakers_count) +
                              ' speakers and ' + str(teams_count) + ' teams')

            # Judges
            self.stdout.write('*** Attempting to create the judges')
            try:
                reader = csv.reader(
                    open(os.path.join(data_path, 'institutions.csv')))
            except:
                self.stdout.write(
                    'institutions.csv file is missing or damaged')

            adjs_count = 0
            reader = csv.reader(open(os.path.join(data_path, 'judges.csv')))
            for line in reader:
                ins_name, name, test_score = line[0:3]
                phone = len(line) > 3 and line[3] or None
                email = len(line) > 4 and line[4] or None
                institution_conflicts = len(line) > 5 and line[5] or None
                team_conflicts = len(line) > 6 and line[6] or None

                try:
                    test_score = float(test_score)
                except ValueError:
                    self.stdout.write(
                        'Could not interpret adj score for {0}: {1}'.format(
                            name, test_score))
                    test_score = 0

                try:
                    phone = str(phone)
                except ValueError:
                    self.stdout.write(
                        'Could not interpret adj phone for {0}: {1}'.format(
                            name, phone))
                    phone = None

                try:
                    email = str(email)
                except ValueError:
                    self.stdout.write(
                        'Could not interpret adj email for {0}: {1}'.format(
                            name, email))
                    email = None

                # People can either input instutions as name or short name
                ins_name = ins_name.strip()
                try:
                    ins = m.Institution.objects.get(name=ins_name,
                                                    tournament=t)
                except m.Institution.DoesNotExist:
                    ins = m.Institution.objects.get(code=ins_name,
                                                    tournament=t)

                name = name.strip()
                adj = m.Adjudicator(name=name,
                                    institution=ins,
                                    test_score=test_score,
                                    phone=phone,
                                    email=email)
                adj.save()
                print "Adjudicator", name

                m.AdjudicatorTestScoreHistory(adjudicator=adj,
                                              score=test_score,
                                              round=None).save()

                m.AdjudicatorInstitutionConflict(adjudicator=adj,
                                                 institution=ins).save()

                if institution_conflicts:
                    for ins_conflict_name in institution_conflicts.split(","):
                        ins_conflict_name = ins_conflict_name.strip()
                        try:
                            ins_conflict = m.Institution.objects.get(
                                name=ins_conflict_name, tournament=t)
                        except m.Institution.DoesNotExist:
                            print ins_conflict_name
                            ins_conflict = m.Institution.objects.get(
                                code=ins_conflict_name, tournament=t)
                        m.AdjudicatorInstitutionConflict(
                            adjudicator=adj, institution=ins_conflict).save()
                        print "    conflicts with", ins_conflict.name

                if team_conflicts:
                    for team_conflict_name in team_conflicts.split(","):
                        team_conflict_ins_name, team_conflict_ref = team_conflict_name.rsplit(
                            None, 1)
                        team_conflict_ins_name = team_conflict_ins_name.strip()
                        try:
                            team_conflict_ins = m.Institution.objects.get(
                                name=team_conflict_ins_name, tournament=t)
                        except m.Institution.DoesNotExist:
                            team_conflict_ins = m.Institution.objects.get(
                                code=team_conflict_ins_name, tournament=t)
                        try:
                            team_conflict = m.Team.objects.get(
                                institution=team_conflict_ins,
                                reference=team_conflict_ref)
                        except m.Team.DoesNotExist:
                            self.stdout.write(
                                'No team exists to conflict with {0}: {1}'.
                                format(name, team_conflict_name))
                        m.AdjudicatorConflict(adjudicator=adj,
                                              team=team_conflict).save()
                        print "    conflicts with", team_conflict.short_name

                adjs_count = adjs_count + 1

            self.stdout.write('*** Created ' + str(adjs_count) + ' judges')

            # Motions
            if os.path.isfile(os.path.join(data_path, 'motions.csv')):
                motions_count = 0
                reader = csv.reader(
                    open(os.path.join(data_path, 'motions.csv')))
                for r, seq, reference, text in reader:
                    try:
                        round = m.Round.objects.get(abbreviation=r)
                    except m.Round.DoesNotExist:
                        round = m.Round.objects.get(seq=int(r))
                    seq = int(seq)
                    m.Motion(round=round,
                             seq=seq,
                             reference=reference,
                             text=text).save()
                    self.stdout.write(text)
                    motions_count += 1

                self.stdout.write('*** Created ' + str(motions_count) +
                                  ' motions')

            # Sides
            if os.path.isfile(os.path.join(data_path, 'sides.csv')):
                sides_count = 0
                reader = csv.reader(open(os.path.join(data_path, 'sides.csv')))
                for line in reader:
                    ins_name = line[0]
                    team_name = line[1]
                    ins_name = ins_name.strip()
                    try:
                        ins = m.Institution.objects.get(name=ins_name,
                                                        tournament=t)
                    except m.Institution.DoesNotExist:
                        ins = m.Institution.objects.get(code=ins_name,
                                                        tournament=t)
                    team = m.Team.objects.get(institution=ins,
                                              reference=team_name)
                    for seq, side in enumerate(line[2:], start=1):
                        round = m.Round.objects.get(seq=seq)
                        if side.lower() in ["a", "aff"]:
                            pos = m.TeamPositionAllocation.POSITION_AFFIRMATIVE
                        elif side.lower() in ["n", "neg"]:
                            pos = m.TeamPositionAllocation.POSITION_NEGATIVE
                        else:
                            self.stdout.write(
                                "Skipping round {0} allocation for team {1}, invalid side: {2}"
                                .format(seq, team.short_name, side))
                        m.TeamPositionAllocation(round=round,
                                                 team=team,
                                                 position=pos).save()
                        sides_count += 1
                    self.stdout.write(team.short_name)

                self.stdout.write('*** Created ' + str(sides_count) +
                                  ' side allocations')

            self.stdout.write('*** Successfully imported all data')

        except Exception:
            import traceback
            traceback.print_exc()
            self.stdout.write('Failed')
예제 #6
0
def main():
    m.Tournament.objects.filter(slug='australs').delete()
    t = m.Tournament(slug='australs')
    t.save()

    for i in range(1, 9):
        if i == 1:
            rtype = m.Round.TYPE_RANDOM
        else:
            rtype = m.Round.TYPE_PRELIM

        m.Round(
            tournament=t,
            seq=i,
            name='Round %d' % i,
            type=rtype,
            feedback_weight=min((i - 1) * 0.1, 0.5),
        ).save()

    t.current_round = m.Round.objects.get(tournament=t, seq=1)
    t.save()

    reader = csv.reader(open('institutions.csv'))
    for code, name in reader:
        i = m.Institution(code=code, name=name, tournament=t)
        i.save()

    reader = csv.reader(open('people.csv'))
    for barcode, ins_name, team, first, last in reader:
        print ins_name
        ins = m.Institution.objects.get(name=ins_name, tournament=t)
        if team == 'Judge':
            m.Adjudicator(
                name='%s %s' % (first, last),
                institution=ins,
                barcode_id=int(barcode),
            ).save()
        elif team == 'Observer':
            continue
        else:
            team_num = int(team)

            team_name = '%s %d' % (ins.code, team_num)

            team, _ = m.Team.objects.get_or_create(
                institution=ins,
                name=team_name,
            )

            m.Speaker(
                name='%s %s' % (first, last),
                team=team,
            ).save()

    #TODO (dummy venues)
    reader = csv.reader(open('venues.csv'))
    for data in reader:
        building, group, room, priority = data[:4]

        try:
            group = int(group)
        except ValueError:
            group = None

        m.Venue(
            tournament=t,
            group=group,
            name='%s %s' % (building, room),
            priority=priority,
        ).save()

    # swing team
    ins = m.Institution(
        tournament=t,
        code='SWING',
        name='SWING',
    )
    ins.save()
    team = m.Team(
        institution=ins,
        name='SWING',
    )
    team.save()
    for i in range(1, 4):
        m.Speaker(name='Swing %d' % i, team=team).save()

    # TODO [remove]
    #for r in m.Round.objects.all():
    #    r.activate_all()

    from django.contrib.auth.models import User

    def add_conflicts(adj, teams):
        for team in teams:
            m.AdjudicatorConflict(
                adjudicator=adj,
                team=team,
            ).save()

    from debate.models import Adjudicator
    for adj in Adjudicator.objects.all():
        add_conflicts(adj, m.Team.objects.filter(institution=adj.institution))

    reader = csv.reader(open('conflicts.csv'))
    for data in reader:
        barcode, institution, first, last, personal, add_institution, score = data
        adj = Adjudicator.objects.get(barcode_id=barcode)

        if score == 'T':
            adj.is_trainee = True
            adj.test_score = 1
        elif score.strip():
            adj.test_score = float(score)

        adj.save()

        for ins_code in add_institution.split(','):
            ins_code = ins_code.strip()
            if ins_code:
                print ins_code
                ins = m.Institution.objects.get(code=ins_code, tournament=t)
                add_conflicts(adj, m.Team.objects.filter(institution=ins))

        for team_name in personal.split(','):
            team_name = team_name.strip()
            if team_name:
                print team_name
                team = m.Team.objects.get(name=team_name,
                                          institution__tournament=t)
                add_conflicts(adj, [team])
예제 #7
0
    def handle(self, *args, **options):
        if len(args) < 2:
            raise CommandError("Not enough arguments.")

        # Getting the command line variable
        folder = args[0]
        try:
            rounds_to_auto_make = int(args[1])
        except:
            rounds_to_auto_make = 0

        total_errors = 0

        # Where to find the data
        base_path = os.path.join(settings.PROJECT_PATH, 'data')
        data_path = os.path.join(base_path, folder)
        if not os.path.isdir(data_path):
            # If it isn't in the standard data folder try the datasets module
            data_path = os.path.join(base_path, 'tabbycat_datasets', folder)

        if not os.path.isdir(data_path):
            self.stdout.write('Couldn\'t find the specified folder:' +
                              data_path)
        else:
            self.stdout.write('importing from ' + data_path)

            try:
                if m.Tournament.objects.filter(
                        slug=slugify(unicode(folder))).exists():
                    self.stdout.write("WARNING! A tournament called '" +
                                      folder + "' already exists.")
                    self.stdout.write(
                        "You are about to delete EVERYTHING for this tournament."
                    )
                    response = raw_input("Are you sure? ")
                    if response != "yes":
                        self.stdout.write("Cancelled.")
                        raise CommandError("Cancelled by user.")
                    m.Tournament.objects.filter(
                        slug=slugify(unicode(folder))).delete()

                # Tournament
                self.stdout.write('**** Attempting to create tournament ' +
                                  folder)
                try:
                    slug = slugify(unicode(folder))
                    short_name = (folder[:24] +
                                  '..') if len(folder) > 75 else folder
                    t = m.Tournament(name=folder,
                                     short_name=short_name,
                                     slug=slugify(unicode(folder)))
                    t.save()
                except Exception as inst:
                    total_errors += 1
                    print inst

                self.stdout.write('Made tournament: \t' + folder)
                self.stdout.write('**** Attempting to create rounds ')
                rounds_count = 0

                if rounds_to_auto_make > 0:
                    # If using the CLI arg
                    try:
                        for i in range(1, rounds_to_auto_make + 1):
                            if i == 1:
                                draw_type = m.Round.DRAW_RANDOM
                            else:
                                draw_type = m.Round.DRAW_POWERPAIRED

                            m.Round(
                                tournament=t,
                                seq=i,
                                name='Round %d' % i,
                                abbreviation='R%d' % i,
                                draw_type=draw_type,
                                feedback_weight=min((i - 1) * 0.1, 0.5),
                                silent=(i >= rounds_to_auto_make),
                            ).save()
                            print "Auto-made round: \tRound %s" % i
                            rounds_count += 1

                    except Exception as inst:
                        total_errors += 1
                        print inst
                else:
                    # If importing from the CSV
                    try:
                        reader = csv.reader(
                            open(os.path.join(data_path, 'rounds.csv')))
                        reader.next()  # Skipping header row
                    except Exception as e:
                        print e
                        self.stdout.write(
                            'rounds.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                        )

                    i = 1
                    for line in reader:
                        seq = line[0]
                        if not seq:
                            seq = i
                        name = str(line[1])
                        abbv = str(line[2])
                        draw_stage = str(line[3]) or "Preliminary"
                        draw_type = str(line[4]) or "Random"
                        is_silent = int(line[5]) or 0
                        feedback_weight = float(line[6]) or 0.7

                        if draw_stage.lower() in ("preliminary", "p"):
                            draw_stage = "P"
                        elif draw_stage.lower() in ("elimination", "break",
                                                    "e", "b"):
                            draw_stage = "E"
                        else:
                            draw_stage = None

                        if draw_type.lower() in ("random", "r"):
                            draw_type = "R"
                        elif draw_type.lower() in ("round-robin",
                                                   "round robin", "d"):
                            draw_type = "D"
                        elif draw_type.lower() in ("power-paired",
                                                   "power paired", "p"):
                            draw_type = "P"
                        elif draw_type.lower() in ("first elimination",
                                                   "first-elimination",
                                                   "1st elimination", "1",
                                                   "e"):
                            draw_type = "F"
                        elif draw_type.lower() in ("subsequent elimination",
                                                   "subsequent-elimination",
                                                   "2nd elimination", "2"):
                            draw_type = "B"
                        else:
                            draw_type = None

                        if is_silent > 0:
                            is_silent = True
                        else:
                            is_silent = False

                        try:
                            m.Round(tournament=t,
                                    seq=seq,
                                    name=name,
                                    abbreviation=abbv,
                                    draw_type=draw_type,
                                    stage=draw_stage,
                                    feedback_weight=min((int(seq) - 1) * 0.1,
                                                        0.5),
                                    silent=is_silent).save()
                            rounds_count += 1
                            i += 1
                            print "Made round: \t\t%s" % name
                        except Exception as inst:
                            total_errors += 1
                            self.stdout.write('Couldnt make round ' + name)
                            print inst

                t.current_round = m.Round.objects.get(tournament=t, seq=1)
                t.save()
                self.stdout.write('**** Created ' + str(rounds_count) +
                                  ' rounds')

                # Config
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'config.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'config.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )
                    reader = None

                if reader:
                    config_count = 0
                    for line in reader:
                        key = line[0]
                        value_type = line[1]
                        if str(line[2]) == '':
                            value = None
                        elif value_type == "string" or value_type == "str":
                            value = str(line[2])
                        elif value_type == "int":
                            value = int(line[2])
                        elif value_type == "float":
                            try:
                                value = float(line[2])
                            except:
                                value = float(int(line[2]))
                        elif value_type == "bool" or value_type == "_bool":
                            if line[2] == "True" or line[2] == "1" or line[
                                    2] == "TRUE":
                                value = True
                            elif line[2] == "False" or line[2] == "0" or line[
                                    2] == "FALSE":
                                value = False
                            else:
                                print "Error %s not properly set" % key

                        if value is not None:
                            t.config.set(key, value)
                            config_count += 1
                            print "Made setting \t%s as %s" % (key, value)

                    self.stdout.write('**** Created ' + str(config_count) +
                                      ' settings')

                # Venues
                self.stdout.write('**** Attempting to create the venue groups')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'venue_groups.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'venues_groups.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )
                    reader = None

                if reader:
                    venue_count = 0
                    venue_group_count = 0
                    for line in reader:
                        long_name = line[0] or None
                        short_name = line[1] or None
                        team_capacity = line[2] or None
                        try:
                            venue_group, created = m.VenueGroup.objects.get_or_create(
                                name=long_name,
                                short_name=short_name,
                                team_capacity=team_capacity,
                                tournament=t)

                            if created:
                                print "Made venue group: \t%s" % venue_group
                                venue_group_count = venue_group_count + 1
                            else:
                                print "Matched venue group: \t%s" % venue_group

                        except ValueError:
                            total_errors += 1
                            self.stdout.write('Couldnt make venue group ' +
                                              group)
                            venue_group = None

                # Venues
                self.stdout.write('**** Attempting to create the venues')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'venues.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'venues.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )
                    reader = None

                if reader:
                    venue_count = 0
                    venue_group_count = 0
                    for line in reader:
                        room_name = line[0]
                        priority = len(line) > 1 and line[1] or 10
                        group_name = len(line) > 2 and line[2] or None
                        time = len(line) > 3 and str(line[3]) or None

                        if group_name:
                            try:
                                venue_group, created = m.VenueGroup.objects.get_or_create(
                                    name=group_name)

                                if created:
                                    print "Made venue group: \t%s" % group_name
                                    venue_group_count = venue_group_count + 1

                            except ValueError:
                                total_errors += 1
                                self.stdout.write('Couldnt make venue group ' +
                                                  group_name)
                                venue_group = None
                        else:
                            venue_group = None

                        try:
                            m.Venue(tournament=t,
                                    group=venue_group,
                                    name=room_name,
                                    priority=priority,
                                    time=time).save()
                            #print "Made venue: \t\t%s" % room_name

                            venue_count = venue_count + 1

                        except Exception as inst:
                            total_errors += 1
                            self.stdout.write('Couldnt make venue ' +
                                              room_name)
                            print inst

                self.stdout.write('**** Created ' + str(venue_group_count) +
                                  ' venue groups')
                self.stdout.write('**** Created ' + str(venue_count) +
                                  ' venues')

                # Institutions
                self.stdout.write('**** Attempting to create the institutions')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'institutions.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'institutions.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )

                institutions_count = 0
                for line in reader:
                    name = str(line[0])
                    code = str(line[1])
                    abbv = len(line) > 2 and line[2] or ""

                    try:
                        inst, created = m.Institution.objects.get_or_create(
                            code=code, name=name, abbreviation=abbv)
                        if created:
                            print "Made institution: \t%s (%s / %s)" % (
                                name, code, abbv)
                        else:
                            print "Matched institution: \t%s (%s / %s)" % (
                                name, code, abbv)

                        institutions_count = institutions_count + 1
                    except Exception as inst:
                        total_errors += 1
                        self.stdout.write('Couldnt make institution ' + name)
                        print inst

                self.stdout.write('**** Created ' + str(institutions_count) +
                                  ' institutions')

                # Teams
                self.stdout.write('**** Attempting to create the teams')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'teams.csv'), 'rU'))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'teams.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )

                # Getting a list of all assigned emoji
                assigned_emoji_teams = m.Team.objects.filter(
                    emoji_seq__isnull=False).values_list('emoji_seq',
                                                         flat=True)
                unassigned_emoji_teams = m.Team.objects.filter(
                    emoji_seq__isnull=True).values_list('id', flat=True)

                # The list of possible emoji, then culled to prevent duplicates
                emoji_options = range(0, len(EMOJI_LIST) - 1)

                def get_emoji(emoji_options):
                    try:
                        emoji_id = random.choice(emoji_options)
                        emoji_options.remove(emoji_id)
                    except:
                        emoji_id = random.randint(0, len(EMOJI_LIST) - 1)
                    return emoji_id

                for index in assigned_emoji_teams:
                    if index in emoji_options:
                        emoji_options.remove(index)

                for index in unassigned_emoji_teams:
                    if index in emoji_options:
                        emoji_options.remove(index)

                teams_count = 0
                for line in reader:
                    try:
                        name = line[0]
                        ins = line[1]
                        short_name = name[:34]
                        emoji_id = get_emoji(emoji_options)
                        try:
                            ins = m.Institution.objects.get(name=ins)
                        except:
                            try:
                                ins = m.Institution.objects.get(code=ins)
                            except Exception as inst:
                                self.stdout.write("error with finding inst " +
                                                  ins)
                                total_errors += 1
                                print type(inst)  # the exception instance
                                print inst  # __str__ allows args to printed directly

                        team, created = m.Team.objects.get_or_create(
                            institution=ins,
                            reference=name,
                            short_reference=short_name,
                            tournament=t,
                            emoji_seq=emoji_id)
                        team.save()

                        m.Speaker(name="1st Speaker", team=team).save()
                        m.Speaker(name="2nd Speaker", team=team).save()
                        m.Speaker(name="3rd Speaker", team=team).save()
                        m.Speaker(name="Reply Speaker", team=team).save()
                        teams_count = teams_count + 1
                        print "Made team:\t\t%s  %s of %s" % (
                            EMOJI_LIST[emoji_id], name, ins)
                    except Exception as inst:
                        self.stdout.write('Couldnt make the team ' + line[0] +
                                          ' of ' + line[1])
                        total_errors += 1
                        print inst

                # Speakers
                self.stdout.write(
                    '**** Attempting to create the teams/speakers')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'speakers.csv'), 'rU'))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'speakers.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )

                speakers_count = 0
                teams_count = 0
                for line in reader:
                    name = line[0]
                    ins_name = line[1]
                    team_name = line[2]
                    try:
                        prefix = int(line[3]) or 0
                    except:
                        prefix = False
                    try:
                        gender = str(line[4]) or None
                        if gender != "M" and gender != "F" and gender != "O":
                            gender = None
                    except:
                        gender = None
                    try:
                        novice_status = int(line[5]) or 0
                    except:
                        novice_status = 0

                    try:
                        ins = m.Institution.objects.get(code=ins_name)
                    except:
                        try:
                            ins = m.Institution.objects.get(name=ins_name)
                        except:
                            try:
                                ins = m.Institution.objects.get(
                                    abbreviation=ins_name)
                            except Exception as inst:
                                self.stdout.write(
                                    'Could not find the institution of {0} for {1}'
                                    .format(ins_name, name))
                                self.stdout.write("error with " + ins_name)
                                total_errors += 1
                                print type(inst)  # the exception instance
                                print inst  # __str__ allows args to printed directly

                    try:
                        team, created = m.Team.objects.get_or_create(
                            institution=ins,
                            reference=team_name,
                            use_institution_prefix=prefix,
                            tournament=t)
                        if created:
                            team.emoji_seq = get_emoji(emoji_options)
                            team.save()
                            teams_count = teams_count + 1
                            print "Made team:\t\t%s  %s of %s" % (
                                EMOJI_LIST[team.emoji_seq], team_name, ins)

                    except Exception as inst:
                        total_errors += 1
                        self.stdout.write("error with " + str(team_name))
                        print type(inst)  # the exception instance
                        print inst  # __str__ allows args to printed directly

                    # Resetting the variable incase create/get above fails
                    speakers_team = m.Team.objects.get(institution=ins,
                                                       reference=team_name,
                                                       tournament=t)

                    name = name.strip()
                    try:
                        m.Speaker(name=name,
                                  team=speakers_team,
                                  gender=gender,
                                  novice=novice_status).save()
                        speakers_count = speakers_count + 1
                    except Exception as inst:
                        self.stdout.write('Couldnt make the speaker ' + name)
                        total_errors += 1
                        print inst

                    print "Made speaker:\t\t\t  %s (%s) of %s" % (name, gender,
                                                                  ins)

                self.stdout.write('**** Created ' + str(speakers_count) +
                                  ' speakers and ' + str(teams_count) +
                                  ' teams')

                # Judges
                self.stdout.write('**** Attempting to create the judges')
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'judges.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'judges.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )

                adjs_count = 0
                for line in reader:
                    name, ins_name, test_score = line[0:3]
                    try:
                        gender = str(line[3]) or None
                        if gender != "M" and gender != "F" and gender != "O":
                            gender = None
                    except:
                        gender = None
                    try:
                        novice_status = int(line[4]) or 0
                    except:
                        novice_status = 0
                    phone = len(line) > 4 and line[5] or None
                    email = len(line) > 5 and line[6] or None
                    notes = len(line) > 6 and line[7] or None
                    institution_conflicts = len(line) > 8 and line[8] or None
                    team_conflicts = len(line) > 9 and line[9] or None

                    try:
                        test_score = float(test_score)
                    except ValueError:
                        self.stdout.write(
                            'Could not interpret adj score for {0}: {1}'.
                            format(name, test_score))
                        test_score = 0
                        total_errors += 1

                    try:
                        phone = str(phone)
                    except ValueError:
                        self.stdout.write(
                            'Could not interpret adj phone for {0}: {1}'.
                            format(name, phone))
                        phone = None
                        total_errors += 1

                    try:
                        email = str(email)
                    except ValueError:
                        self.stdout.write(
                            'Could not interpret adj email for {0}: {1}'.
                            format(name, email))
                        email = None
                        total_errors += 1

                    try:
                        notes = str(notes)
                    except ValueError:
                        self.stdout.write(
                            'Could not interpret adj note for {0}: {1}'.format(
                                name, notes))
                        notes = None
                        total_errors += 1

                    # People can either input instutions as name or short name
                    ins_name = ins_name.strip()
                    try:
                        ins = m.Institution.objects.get(name=ins_name)
                    except m.Institution.DoesNotExist:
                        try:
                            ins = m.Institution.objects.get(code=ins_name)
                        except m.Institution.DoesNotExist:
                            try:
                                ins = m.Institution.objects.get(
                                    abbreviation=ins_name)
                            except:
                                self.stdout.write(
                                    'Could not find the institution of {0} for {1}'
                                    .format(ins_name, name))

                    name = name.strip()

                    adj = m.Adjudicator(name=name,
                                        institution=ins,
                                        test_score=test_score,
                                        gender=gender,
                                        novice=novice_status,
                                        phone=phone,
                                        email=email,
                                        notes=notes,
                                        tournament=t)
                    adj.save()
                    print "Made adjudicator: \t%s (%s) of %s" % (name, gender,
                                                                 ins)

                    m.AdjudicatorTestScoreHistory(adjudicator=adj,
                                                  score=test_score,
                                                  round=None).save()
                    m.AdjudicatorInstitutionConflict(adjudicator=adj,
                                                     institution=ins).save()

                    if institution_conflicts:
                        for ins_conflict_name in institution_conflicts.split(
                                ","):
                            ins_conflict_name = ins_conflict_name.strip()
                            try:
                                ins_conflict = m.Institution.objects.get(
                                    name=ins_conflict_name)
                            except m.Institution.DoesNotExist:
                                try:
                                    ins_conflict = m.Institution.objects.get(
                                        code=ins_conflict_name)
                                except m.Institution.DoesNotExist:
                                    try:
                                        ins_conflict = m.Institution.objects.get(
                                            abbreviation=ins_conflict_name)
                                    except:
                                        self.stdout.write(
                                            'Could not find the institution conflict {0} for {1}'
                                            .format(ins_conflict, name))

                            m.AdjudicatorInstitutionConflict(
                                adjudicator=adj,
                                institution=ins_conflict).save()
                            print "\t\t\tconflicts with", ins_conflict.name

                    if team_conflicts:
                        for team_conflict_name in team_conflicts.split(","):
                            team_conflict_ins_name, team_conflict_ref = team_conflict_name.rsplit(
                                None, 1)
                            team_conflict_ins_name = team_conflict_ins_name.strip(
                            )
                            try:
                                team_conflict_ins = m.Institution.objects.get(
                                    name=team_conflict_ins_name)
                            except m.Institution.DoesNotExist:
                                try:
                                    team_conflict_ins = m.Institution.objects.get(
                                        code=team_conflict_ins_name)
                                except m.Institution.DoesNotExist:
                                    try:
                                        team_conflict_ins = m.Institution.objects.get(
                                            abbreviation=team_conflict_ins_name
                                        )
                                    except:
                                        print "couldn't find team conflict institution for %s" % team_conflict_ins_name

                            try:
                                team_conflict = m.Team.objects.get(
                                    institution=team_conflict_ins,
                                    reference=team_conflict_ref)
                            except m.Team.DoesNotExist:
                                self.stdout.write(
                                    'No team exists to conflict with {0}: {1}'.
                                    format(name, team_conflict_name))
                                total_errors += 1
                            m.AdjudicatorConflict(adjudicator=adj,
                                                  team=team_conflict).save()
                            print "\t\t\tconflicts with", team_conflict.short_name

                    adjs_count = adjs_count + 1

                self.stdout.write('**** Created ' + str(adjs_count) +
                                  ' judges')

                # Motions
                try:
                    reader = csv.reader(
                        open(os.path.join(data_path, 'motions.csv')))
                    reader.next()  # Skipping header row
                except Exception as e:
                    print e
                    self.stdout.write(
                        'motions.csv file is missing or damaged - ensure saved as plain CSV (or MS-DOS CSV)'
                    )

                motions_count = 0
                for line in reader:
                    round_abbv = str(line[0])
                    motion_seq = int(line[1])
                    reference = str(line[2])
                    text = str(line[3])

                    try:
                        round = m.Round.objects.get(abbreviation=round_abbv,
                                                    tournament=t)
                        m.Motion(round=round,
                                 seq=motion_seq,
                                 reference=reference,
                                 text=text).save()
                        self.stdout.write('Made motion: \t\t' + round_abbv +
                                          ': ' + text)
                        motions_count += 1
                    except m.Round.DoesNotExist:
                        total_errors += 1
                        self.stdout.write(
                            'Couldnt find round with abbreviation: ' +
                            round_abbv)

                self.stdout.write('**** Created ' + str(motions_count) +
                                  ' motions')

                # Sides
                if os.path.isfile(os.path.join(data_path, 'sides.csv')):
                    sides_count = 0
                    reader = csv.reader(
                        open(os.path.join(data_path, 'sides.csv')))
                    reader.next()  # Skipping header row
                    for line in reader:
                        ins_name = line[0]
                        team_name = line[1]
                        ins_name = ins_name.strip()
                        try:
                            ins = m.Institution.objects.get(name=ins_name)
                        except m.Institution.DoesNotExist:
                            ins = m.Institution.objects.get(code=ins_name)
                        team = m.Team.objects.get(institution=ins,
                                                  reference=team_name,
                                                  tournament=t)
                        for seq, side in enumerate(line[2:], start=1):
                            round = m.Round.objects.get(seq=seq)
                            if side.lower() in ["a", "aff"]:
                                pos = m.TeamPositionAllocation.POSITION_AFFIRMATIVE
                            elif side.lower() in ["n", "neg"]:
                                pos = m.TeamPositionAllocation.POSITION_NEGATIVE
                            else:
                                self.stdout.write(
                                    "Skipping round {0} allocation for team {1}, invalid side: {2}"
                                    .format(seq, team.short_name, side))
                            m.TeamPositionAllocation(round=round,
                                                     team=team,
                                                     position=pos).save()
                            sides_count += 1
                        self.stdout.write(team.short_name)

                    self.stdout.write('**** Created ' + str(sides_count) +
                                      ' side allocations')

                if total_errors == 0:
                    self.stdout.write('**** Successfully imported all data')
                else:
                    self.stdout.write(
                        '**** Successfully all data but with %d ERRORS' %
                        total_errors)

            except Exception:
                import traceback
                traceback.print_exc()
                self.stdout.write('Failed')
예제 #8
0
def main(suffix=None, verbose=False):

    directory_name = os.path.dirname(__file__)

    def make_filename(name, add_suffix):
        if suffix and add_suffix:
            filename = name + "-" + suffix + ".csv"
        else:
            filename = name + ".csv"
        return os.path.join(directory_name, filename)

    def verbose_print(message):
        if verbose:
            print message

    print "Deleting and re-creating tournament..."
    m.Tournament.objects.filter(slug='test-australs2012').delete()
    t = m.Tournament(slug='test-australs2012')
    t.save()

    print "Adding rounds..."

    for i in range(1, NUM_ROUNDS + 1):
        if i == 1:
            rtype = m.Round.TYPE_RANDOM
        else:
            rtype = m.Round.TYPE_PRELIM

        m.Round(
            tournament=t,
            seq=i,
            name='Round %d' % i,
            type=rtype,
            feedback_weight=min((i - 1) * 0.1, 0.5),
        ).save()

    t.current_round = m.Round.objects.get(tournament=t, seq=1)
    t.save()

    print "Importing from files..."

    filename = make_filename("institutions", add_suffix=False)
    print(filename)
    reader = csv.reader(open(filename))
    for name, code in reader:
        i = m.Institution(code=code, name=name, tournament=t)
        i.save()

    filename = make_filename('debaters', add_suffix=True)
    print(filename)
    reader = csv.reader(open(filename))
    header_row = reader.next()  # skip the first row (headers)
    first_column = header_row.index("Name")
    for row in reader:
        # for some reason there are a bunch of stray cells at the end of each row
        # we only care about the non-blank cells, i.e. the first four
        name, ins_name, attendance, team_number = row[
            first_column:first_column + 4]
        verbose_print(ins_name)

        if attendance != "Debater":
            print("{0} is not a debater? ({1})".format(name, attendance))

        ins_name_full = ins_name
        # extract the institution name from Seb's longer institution name
        # These are in the format "Name of University 1 Member 1", where the
        # first number is the team number and the second number is the member number
        if ins_name[-1].isdigit():
            member_number = ins_name[-1]
            ins_name = ins_name[:-1].rstrip()
        else:
            member_number = None
            print("No member number in: {0}".format(ins_name_full))

        if ins_name.endswith("Member"):
            ins_name = ins_name[:-6].rstrip()
        else:
            print("No 'Member' in: {0}".format(ins_name_full))

        if ins_name[-1].isdigit():
            ins_name_team_number = ins_name[-1]
            ins_name = ins_name[:-1].rstrip()
        else:
            ins_name_team_number = None
            print("No team number in: {0}".format(ins_name_full))

        if ins_name_team_number is not None and ins_name_team_number != team_number:
            print("Team numbers don't match: {0}, {1}".format(
                ins_name_full, team_number))

        ins = m.Institution.objects.get(name=ins_name, tournament=t)
        team_name = ins.code + " " + team_number
        team, _ = m.Team.objects.get_or_create(institution=ins, name=team_name)
        m.Speaker(name=name, team=team).save()

    filename = make_filename('judges', add_suffix=True)
    print(filename)
    reader = csv.reader(open(filename))
    header_row = reader.next()  # skip the first row (headers)
    first_column = header_row.index("Name")
    for row in reader:
        name, ins_name, attendance = row[first_column:first_column + 3]

        verbose_print(ins_name)

        if attendance not in [
                "Judge", "Independent", "CA", "DCA", "Observer", "Org Comm"
        ]:
            print(
                "{0} is not a judge, independent, CA, DCA, observer or org comm? ({1})"
                .format(name, attendance))

        ins_name_full = ins_name

        if ins_name == "Adjudication Core":
            if attendance not in ["CA", "DCA"]:
                print("{0} is in the adjudication core, but not a CA or DCA".
                      format(name))

        elif ins_name == "Org Comm":
            # Do nothing, we don't care about org comms
            continue

        elif attendance == "Observer" and "Observer" in ins_name:
            # Do nothing, we don't care about observers
            continue

        else:
            # extract the institution name from Seb's longer institution name
            # These are in the format "Name of University 1 Judge 1", where the
            # first number is the team number and the second number is the member number
            if ins_name[-1].isdigit():
                member_number = ins_name[-1]
                ins_name = ins_name[:-1].rstrip()
            else:
                member_number = None
                print("No member number in: {0}".format(ins_name_full))

            if ins_name.endswith("Judge"):
                ins_name_attendance = ins_name[-5:]
                ins_name = ins_name[:-5].rstrip()
            elif ins_name.endswith("Independent"):
                ins_name_attendance = ins_name[-11:]
                ins_name = ins_name[:-11].rstrip()
            else:
                ins_name_attendance = None
                print("No 'Judge' or 'Independent' in: {0}".format(
                    ins_name_full))

            if ins_name[-1].isdigit():
                ins_name = ins_name[:-1].rstrip()
                print("Judge has team number in: {0}".format(ins_name_full))

            if ins_name_attendance is not None and ins_name_attendance != attendance:
                print("Attendances don't match: {0}, {1}".format(
                    ins_name_full, attendance))

        # Override institution name for independents, put in independents pseudo-institution instead
        if attendance == "Independent":
            ins = m.Institution.objects.get(name="Independent Adjudicators",
                                            tournament=t)
            adj = m.Adjudicator(name=name, institution=ins, test_score=0)
            adj.save()
            try:
                home_ins = m.Institution.objects.get(name=ins_name,
                                                     tournament=t)
            except m.Institution.DoesNotExist:
                print(
                    "No institution '{0}', institution conflict not added for independent {1}"
                    .format(ins_name, name))
            else:
                add_conflicts(adj, m.Team.objects.filter(institution=home_ins))

        else:
            ins = m.Institution.objects.get(name=ins_name, tournament=t)
            m.Adjudicator(name=name, institution=ins, test_score=0).save()

    # Add conflicts for own institutions
    for adj in m.Adjudicator.objects.all():
        add_conflicts(adj, m.Team.objects.filter(institution=adj.institution))

    # Add test scores
    filename = make_filename("adj_scores", add_suffix=False)
    print(filename)
    reader = csv.reader(open(filename))
    header_row = reader.next()
    first_column = header_row.index("Name")
    for row in reader:
        name, score = row[first_column:first_column + 2]
        verbose_print(name)
        try:
            adj = m.Adjudicator.objects.get(name=name,
                                            institution__tournament=t)
        except m.Adjudicator.DoesNotExist:
            print(
                "Could not find adjudicator {0}, can't add his/her test score".
                format(name))
            continue
        adj.test_score = float(score)
        adj.save()

    filename = make_filename("venues", add_suffix=False)
    print(filename)
    reader = csv.reader(open(filename))
    reader.next()  # skip the first row (headers)
    for row in reader:
        group, rooms = row[0:2]
        rooms = rooms.split("/")

        try:
            group = int(group)
        except ValueError:
            group = None

        for room in rooms:
            m.Venue(tournament=t,
                    group=group,
                    name=room,
                    priority=get_priority(room)).save()