From d4e819491d21169327043c47343d919c568cc97e Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 10 Nov 2020 11:06:19 +0100 Subject: [PATCH] basic submit, does not calculate points yet --- rowers/plannedsessions.py | 148 +++++++++++++++++++++++++++++++++++++ rowers/views/racesviews.py | 7 +- 2 files changed, 153 insertions(+), 2 deletions(-) diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 03e34e0a..fb16db02 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -1528,6 +1528,154 @@ def default_class(r,w,race): # No Course Standard return True,boattype,boatclass,adaptiveclass,weightclass,sex,5.0,None +def add_workout_fastestrace(ws, race, r, recordid=0, doregister=False): + result = 0 + comments = [] + errors = [] + + start_time = race.start_time + start_date = race.startdate + startdatetime = datetime.combine(start_date,start_time) + startdatetime = pytz.timezone(race.timezone).localize( + startdatetime + ) + + end_time = race.end_time + end_date = race.enddate + enddatetime = datetime.combine(end_date,end_time) + enddatetime = pytz.timezone(race.timezone).localize( + enddatetime + ) + + # check if all sessions have same date + dates = [w.date for w in ws] + if (not all(d == dates[0] for d in dates)) and race.sessiontype not in ['challenge','cycletarget']: + errors.append('For tests and training sessions, selected workouts must all be done on the same date') + return result,comments,errors,0 + + + if len(ws)>1 and race.sessiontype == 'test': + errors.append('For tests, you can only attach one workout') + return result,comments,errors,0 + + + + ids = [w.id for w in ws] + ids = list(set(ids)) + + if len(ids)>1 and race.sessiontype in ['test','coursetest','race','indoorrace','fastest_time','fastest_distance']: + errors.append('For tests, you can only attach one workout') + return result,comments,errors,0 + + username = r.user.first_name+' '+r.user.last_name + if r.birthdate: + age = calculate_age(r.birthdate) + else: + age = None + + try: + record = IndoorVirtualRaceResult.objects.get( + userid=r.id, + race=race, + id=recordid + ) + except IndoorVirtualRaceResult.DoesNotExist: + if doregister: + hasinitial,boattype,boatclass,adaptiveclass,weightclass,sex,referencespeed,initialcategory = default_class(r,ws[0],race) + if hasinitial: + record = IndoorVirtualRaceResult( + userid = r.id, + username = r.user.first_name+' '+r.user.last_name, + weightcategory=weightclass, + adaptiveclass=adaptiveclass, + race=race, + boatclass=boatclass, + sex=sex, + age = age, + referencespeed=referencespeed, + entrycategory=initialcategory, + ) + record.save() + else: + errors.append("Unable to find a suitable start category") + return result,comments,errors,0 + else: + errors.append("Couldn't find this entry") + return result,comments,errors,0 + + records = IndoorVirtualRaceResult.objects.filter( + userid=r.id, + race=race, + workoutid = ws[0].id + ) + + if ws[0].workouttype != record.boatclass: + errors.append('Your workout boat class is different than on your race registration') + return 0,comments,errors,0 + + if ws[0].workouttype not in mytypes.otwtypes: + errors.append('You must submit a on-the-water rowing workout') + return 0,comments, errors, 0 + + if record.weightcategory == 'lwt' and ws[0].weightcategory != record.weightcategory: + errors.append('Your workout weight category did not match the weight category you registered') + return 0,comments, errors,0 + + if ws[0].adaptiveclass != record.adaptiveclass: + errors.append('Your adaptive classification did not match the registration') + return 0,comments, errors, 0 + + # start adding sessions + if ws[0].startdatetime>=startdatetime and ws[0].startdatetime<=enddatetime: + ws[0].plannedsession = race + ws[0].save() + result += 1 + + else: + errors.append('Workout %i did not match the race window' % ws[0].id) + return result,comments,errors,0 + + if result>0: + for otherrecord in records: + otherrecord.workoutid = None + otherrecord.coursecompleted = False + otherrecord.save() + + result, comment, errors = add_workouts_plannedsession(ws,race,r) + if result: + record.coursecompleted = True + record.workoutid = ws[0].id + if race.sessiontype == 'fastest_distance': + df = dataprep.getsmallrowdata_db(['time','cumdist'],ids=[ws[0].id]) + fastest_milliseconds = datautils.getfastest(df,race.sessionvalue,mode='distance') + + if fastest_milliseconds > 0: + duration = to_time(1000.*fastest_milliseconds) + record.coursecompleted = True + record.duration = duration + record.distance = race.sessionvalue + record.save() + if race.sessiontype == 'fastest_time': + df = dataprep.getsmallrowdata_db(['time','cumdist'],ids=[w.id]) + fastest_meters = datautils.getfastest(df,ps.sessionvalue,mode='time') + + if fastest_meters > 0: + duration = dt.time(0,ps.sessionvalue) + record.duration = duration + record.distance = fastest_meters + record.coursecompleted = True + record.save() + + + + if ws[0].privacy == 'private': + ws[0].privacy = 'visible' + ws[0].save() + comments.append('Workouts submitted to virtual events have to be public. We have changed the workout to a public workout.') + + record.save() + + return result, comments, errors, 0 # Low Level functions - to be called by higher level methods diff --git a/rowers/views/racesviews.py b/rowers/views/racesviews.py index 57934fe1..a2867ee3 100644 --- a/rowers/views/racesviews.py +++ b/rowers/views/racesviews.py @@ -3319,13 +3319,16 @@ def virtualevent_submit_result_view(request,id=0,workoutid=0): if selectedworkout is not None: - - workouts = Workout.objects.filter(id=selectedworkout) + if race.sessiontype == 'race': result,comments,errors,jobid = add_workout_race( workouts,race,r, splitsecond=splitsecond,recordid=recordid) + elif race.sessiontype in ['fastest_time','fastest_distance']: + result, comments, errors, jobid = add_workout_fastestrace( + workouts,race,r,recordid=recordid + ) else: result,comments,errors,jobid = add_workout_indoorrace( workouts,race,r,recordid=recordid)