from rowers.models import StandardCollection,CourseStandard import pandas as pd import arrow import datetime def save_scoring(name,user,filename,id=0,notes=""): if id==0: collection = StandardCollection(name=name,manager=user,notes=notes) collection.save() standards = CourseStandard.objects.filter(standardcollection=collection) for standard in standards: standard.delete() else: try: collection = StandardCollection.objects.get(id=id) collection.name = name collection.notes = notes collection.save() except StandardCollection.DoesNotExist: return 0 try: df = pd.read_csv(filename) except: return 0 df = df.drop_duplicates(['Name']) for index, row in df.iterrows(): try: name = row['Name'] except KeyError: continue try: coursedistance = row['CourseDistance'] coursetime = row['CourseStandard'] t = datetime.datetime.strptime(coursetime,'%M:%S.%f') delta = datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second,microseconds=t.microsecond) seconds = delta.total_seconds() coursestandard = coursedistance/seconds except KeyError: continue try: agemin = row['MinAge'] agemax = row['MaxAge'] agemin = int(agemin) agemax = int(agemax) except KeyError: agemin = 0 agemax = 120 try: boatclass = row['BoatClass'] except KeyError: boatclass = 'water' try: boattype = row['BoatType'] except KeyError: boattype = '1x' try: sex = row['Gender'] except KeyError: sex = 'F' try: weightclass = row['WeightClass'] except KeyError: weightclass = 'hwt' try: adaptiveclass = row['AdaptiveClass'] except KeyError: adaptiveclass = None try: skillclass = row['SkillClass'] except KeyError: skillclass = 'Open' # some testing standard = CourseStandard( name=name, coursedistance=coursedistance, coursestandard=coursestandard, coursetime=coursetime, agemin=agemin, agemax=agemax, boatclass=boatclass, boattype=boattype, sex=sex, weightclass=weightclass, adaptiveclass=adaptiveclass, skillclass=skillclass, standardcollection = collection, ) standard.save() return collection.id