def import_institutions(self): for line in self.load_table('institutions'): id, code, name, c, u = line.split('\t') i = m.Institution(code=code.strip(), name=name.strip(), tournament=self.tournament) i.save() self.institutions[int(id)] = i
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()
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()
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')
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])
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()