From add55993ba33350ebc37b3fe75a624cf8b5f2bfb Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Sun, 21 Jun 2020 14:49:09 +0200 Subject: [PATCH] form initial now has better default initial --- rowers/forms.py | 12 +++++- rowers/models.py | 4 +- rowers/plannedsessions.py | 76 +++++++++++++++++++++++++++++++++++--- rowers/views/racesviews.py | 40 ++++++++++++++++---- 4 files changed, 116 insertions(+), 16 deletions(-) diff --git a/rowers/forms.py b/rowers/forms.py index 42987cb4..694962b5 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -418,7 +418,17 @@ class UploadOptionsForm(forms.Form): choices2 = [(r.id,str(r)) for r in registrations2] choices3 = [(0,'---')] - choices = choices3+choices1+choices2 + noregistrations = [] + for ra in VirtualRace.objects.filter(registration_closure__gt=timezone.now(),sessiontype='race'): + rs = VirtualRaceResult.objects.filter(race = ra,userid=r.id) + if rs.count()==0: + noregistrations.append((-ra.id,ra.name)) + for ra in VirtualRace.objects.filter(registration_closure__gt=timezone.now(),sessiontype='indoorrace'): + rs = IndoorVirtualRaceResult.objects.filter(race = ra,userid=r.id) + if rs.count()==0: + noregistrations.append((-ra.id,ra.name)) + + choices = choices3+choices1+choices2+noregistrations if int(raceid) in [r.id for r in races]: therace = VirtualRace.objects.get(id=raceid) diff --git a/rowers/models.py b/rowers/models.py index 4a8adafb..8e9ed3f8 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -2648,7 +2648,7 @@ class VirtualRaceForm(ModelForm): except KeyError: registration_closure = enddatetime+datetime.timedelta(days=1) cd['registration_closure'] = registration_closure - + registration_form = cd['registration_form'] try: @@ -3124,6 +3124,8 @@ class CourseTestResult(models.Model): distance = models.IntegerField(default=0) coursecompleted = models.BooleanField(default=False) + + class IndoorVirtualRaceResultForm(ModelForm): class Meta: model = IndoorVirtualRaceResult diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 674b5fe8..93206cbe 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -45,7 +45,7 @@ from rowers.models import ( GeoCourse, TrainingMicroCycle,TrainingMesoCycle,TrainingMacroCycle, TrainingPlan,PlannedSession,VirtualRaceResult,CourseTestResult, get_course_timezone, IndoorVirtualRaceResult,VirtualRace,createmacrofillers, - createmesofillers,createmicrofillers, + createmesofillers,createmicrofillers,CourseStandard, ) from rowers.courses import get_time_course @@ -1341,9 +1341,70 @@ def remove_rower_race(r,race,recordid=None): return 1 +def default_class(r,w,race): + if r.birthdate: + age = calculate_age(r.birthdate) + else: + age = 25 + + sex = r.sex + if sex=='not specified': + sex='male' + + if w is not None: + boatclass = w.workouttype + boattype = w.boattype + + adaptiveclass = w.adaptiveclass + weightclass = w.weightcategory + else: + if race.sessiontype == 'race': + boatclass = 'water' + else: + boatclass = 'rower' + boattype = '1x' + adaptiveclass = 'None' + weightclass = 'hwt' + + if race.coursestandards: + standards = CourseStandard.objects.filter( + agemin__lt=age,agemax__gt=age, + boatclass=boatclass, + adaptiveclass=adaptiveclass, + boattype=boattype, + weightclass=weightclass + ).order_by("agemax","-agemin","boattype") + + + if standards.count()==0: + # omit weight + standards = CourseStandard.objects.filter(agemin__lt=age,agemax__gt=age, + boatclass=boatclass, + adaptiveclass=adaptiveclass, + boattype=boattype, + ) + if standards.count()==0: + standards = CourseStandard.objects.filter(agemin__lt=age,agemax__gt=age, + boattype=boattype) + if standards.count()==0: + standards = CourseStandard.objects.filter(agemin__lt=age,agemax__gt=age) + + if standards.count()==0: + # boolean, boattype, boatclass, adaptiveclass, weightclass, sex, coursestandard, + return False,'1x','water',None,'hwt','male',None + + if standards.count()>0: + # find optimum standard + s = standards[0] + return True,s.boattype,s.boatclass,s.adaptiveclass,s.weightclass,s.sex,s + + # No Course Standard + return True,boattype,boatclass,adaptiveclass,weightclass,sex,None + + + # Low Level functions - to be called by higher level methods -def add_workout_indoorrace(ws,race,r,recordid=0): - print('aap') +def add_workout_indoorrace(ws,race,r,recordid=0,doregister=False): result = 0 comments = [] errors = [] @@ -1403,11 +1464,14 @@ def add_workout_indoorrace(ws,race,r,recordid=0): workoutid = ws[0].id ) - print(record,records) - if not record: + + if not record and not doregister: errors.append("Couldn't find this entry") return result,comments,errors,0 + elif not record: + pass + if race.sessionmode == 'distance': if ws[0].distance != race.sessionvalue: @@ -1532,7 +1596,7 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0,doregister=False): workoutid = ws[0].id ) - if not record: + if not record and not doregister: errors.append("Couldn't find this entry") return result,comments,errors,0 diff --git a/rowers/views/racesviews.py b/rowers/views/racesviews.py index c0369e00..c2148ac0 100644 --- a/rowers/views/racesviews.py +++ b/rowers/views/racesviews.py @@ -1499,6 +1499,7 @@ def virtualevent_addboat_view(request,id=0): raise Http404("Virtual Challenge does not exist") categories = None + hasinitial,boattype,boatclass,adaptiveclass,weightclass,sex,initialcategory = default_class(r,None,race) if race.coursestandards is not None: categories = CourseStandard.objects.filter( standardcollection=race.coursestandards).order_by("name") @@ -1700,11 +1701,22 @@ def virtualevent_addboat_view(request,id=0): return HttpResponseRedirect(url) else: - initial = { - 'age': calculate_age(r.birthdate), - 'weightcategory': r.weightcategory, - 'adaptiveclass': r.adaptiveclass, + if hasinitial: + initial = { + 'age': calculate_age(r.birthdate), + 'boattype':boattype, + 'boatclass':boatclass, + 'adaptiveclass':adaptiveclass, + 'weightclass':weightclass, + 'sex':sex, + 'entrycategory':initialcategory, } + else: + initial = { + 'age': calculate_age(r.birthdate), + 'weightcategory': r.weightcategory, + 'adaptiveclass': r.adaptiveclass, + } categories = None if race.coursestandards is not None: @@ -1774,6 +1786,7 @@ def virtualevent_register_view(request,id=0): raise Http404("Virtual Challenge does not exist") categories = None + hasinitial,boattype,boatclass,adaptiveclass,weightclass,sex,initialcategory = default_class(r,None,race) if race.coursestandards is not None: categories = CourseStandard.objects.filter( standardcollection=race.coursestandards).order_by("name") @@ -1937,11 +1950,22 @@ def virtualevent_register_view(request,id=0): return HttpResponseRedirect(url) else: - initial = { - 'age': calculate_age(r.birthdate), - 'weightcategory': r.weightcategory, - 'adaptiveclass': r.adaptiveclass, + if hasinitial: + initial = { + 'age': calculate_age(r.birthdate), + 'boattype':boattype, + 'boatclass':boatclass, + 'adaptiveclass':adaptiveclass, + 'weightclass':weightclass, + 'sex':sex, + 'entrycategory':initialcategory, } + else: + initial = { + 'age': calculate_age(r.birthdate), + 'weightcategory': r.weightcategory, + 'adaptiveclass': r.adaptiveclass, + } categories = None if race.coursestandards is not None: