diff --git a/rowers/dataprep.py b/rowers/dataprep.py index f54cf9d7..9eb8f65c 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -809,34 +809,7 @@ def create_row_df(r,distance,duration,startdatetime, return (id, message) - -def totaltime_sec_to_string(totaltime): - hours = int(totaltime / 3600.) - if hours > 23: - message = 'Warning: The workout duration was longer than 23 hours. ' - hours = 23 - - minutes = int((totaltime - 3600. * hours) / 60.) - if minutes > 59: - minutes = 59 - if not message: - message = 'Warning: there is something wrong with the workout duration' - - seconds = int(totaltime - 3600. * hours - 60. * minutes) - if seconds > 59: - seconds = 59 - if not message: - message = 'Warning: there is something wrong with the workout duration' - - tenths = int(10 * (totaltime - 3600. * hours - 60. * minutes - seconds)) - if tenths > 9: - tenths = 9 - if not message: - message = 'Warning: there is something wrong with the workout duration' - - duration = "%s:%s:%s.%s" % (hours, minutes, seconds, tenths) - - return duration +from utils import totaltime_sec_to_string # Processes painsled CSV file to database def save_workout_database(f2, r, dosmooth=True, workouttype='rower', diff --git a/rowers/forms.py b/rowers/forms.py index 1e7a7032..19665e58 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -695,6 +695,19 @@ class WorkoutSessionSelectForm(forms.Form): widget = forms.CheckboxSelectMultiple, ) +class WorkoutRaceSelectForm(forms.Form): + + def __init__(self, workoutdata, *args, **kwargs): + + super(WorkoutRaceSelectForm, self).__init__(*args, **kwargs) + + self.fields['workouts'] = forms.ChoiceField( + label='Workouts', + choices = workoutdata['choices'], + initial=workoutdata['initial'], + widget=forms.RadioSelect, + ) + class PlannedSessionTeamForm(forms.Form): team = forms.ModelMultipleChoiceField( queryset=Team.objects.all(), diff --git a/rowers/models.py b/rowers/models.py index 20b86584..722e087c 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -1335,12 +1335,14 @@ class VirtualRaceResult(models.Model): default='1x', verbose_name = 'Boat Type' ) - + coursecompleted = models.BooleanField(default=False) sex = models.CharField(default="not specified", max_length=30, choices=sexcategories, verbose_name='Gender') + age = models.IntegerField(null=True) + from rowers.metrics import rowingmetrics diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 7aa5f311..ce3ba8d5 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -8,7 +8,7 @@ from django.db import IntegrityError import uuid from django.conf import settings import pytz -from utils import myqueue +from utils import myqueue,calculate_age,totaltime_sec_to_string import django_rq queue = django_rq.get_queue('default') @@ -18,7 +18,7 @@ queuehigh = django_rq.get_queue('low') from rowers.models import ( Rower, Workout,Team, GeoCourse, TrainingMicroCycle,TrainingMesoCycle,TrainingMacroCycle, - TrainingPlan,PlannedSession, + TrainingPlan,PlannedSession,VirtualRaceResult ) import metrics @@ -596,3 +596,95 @@ def remove_rower_race(r,race): race.rower.remove(r) return 1 + +# Low Level functions - to be called by higher level methods +def add_workout_race(ws,race,r): + 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 + + if len(ws)>1 and race.sessiontype == 'test': + errors.append('For tests, you can only attach one workout') + return result,comments,errors + + + + wold = Workout.objects.filter(plannedsession=race,user=r) + ids = [w.id for w in wold] + [w.id for w in ws] + ids = list(set(ids)) + + if len(ids)>1 and race.sessiontype in ['test','coursetest']: + errors.append('For tests, you can only attach one workout') + return result,comments,errors + + # start adding sessions + for w in ws: + if w.startdatetime>=startdatetime and w.startdatetime<=enddatetime: + w.plannedsession = race + w.save() + result += 1 + + comments.append('Your result has been submitted') + else: + errors.append('Workout %i did not match the race window' % w.id) + + if result>0: + username = r.user.first_name+' '+r.user.last_name + if r.birthdate: + age = calculate_age(r.birthdate) + else: + age = None + ( + coursetime, + coursemeters, + coursecompleted + ) = courses.get_time_course(ws,race.course) + if not coursecompleted: + errors.append('Your trajectory did not match the race course') + + duration = totaltime_sec_to_string(coursetime) + + record = VirtualRaceResult( + user=r, + username=username, + workout = ws[0], + race = race, + coursecompleted=coursecompleted, + duration = duration, + boattype = ws[0].boattype, + sex = r.sex, + age = age, + ) + + record.save() + + + + return result,comments,errors + +def delete_race_result(workout,race): + results = VirtualRaceResult.objects.filter(workout=workout,race=race) + for r in results: + r.delete() + + diff --git a/rowers/templates/race_submit.html b/rowers/templates/race_submit.html new file mode 100644 index 00000000..e8678c04 --- /dev/null +++ b/rowers/templates/race_submit.html @@ -0,0 +1,59 @@ +{% extends "base.html" %} +{% load staticfiles %} +{% load rowerfilters %} + +{% block title %}Submit Race Result{% endblock %} + +{% block meta %} + + + +{% endblock %} + + +{% block content %} +