From cd3ee722e21a695bef9d0fcfb4ae0e74db16b6c9 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Fri, 8 Jun 2018 10:44:42 +0200 Subject: [PATCH] improved filter form --- rowers/forms.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++- rowers/models.py | 3 ++- rowers/views.py | 39 ++++++++++++++++++++++--------- 3 files changed, 90 insertions(+), 13 deletions(-) diff --git a/rowers/forms.py b/rowers/forms.py index 77b118a9..11c64220 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -752,7 +752,66 @@ class RaceResultFilterForm(forms.Form): label='Weight Category', initial=['hwt','lwt'], widget=forms.CheckboxSelectMultiple()) - + + def __init__(self, *args, **kwargs): + if 'records' in kwargs: + records = kwargs.pop('records',None) + + super(RaceResultFilterForm,self).__init__(*args,**kwargs) + + if records: + # sex + thesexes = [record.sex for record in records] + thesexes = list(set(thesexes)) + + if len(thesexes)<= 1: + del self.fields['sex'] + else: + sexchoices = [] + for choice in self.fields['sex'].choices: + if choice[0] in thesexes: + sexchoices.append(choice) + self.fields['sex'].choices = sexchoices + + # boatclass + theboatclasses = [record.boatclass for record in records] + theboatclasses = list(set(theboatclasses)) + + if len(theboatclasses)<= 1: + del self.fields['boatclass'] + else: + boatclasschoices = [] + for choice in self.fields['boatclass'].choices: + if choice[0] in theboatclasses: + boatclasschoices.append(choice) + self.fields['boatclass'].choices = boatclasschoices + + # boattype + theboattypees = [record.boattype for record in records] + theboattypees = list(set(theboattypees)) + + if len(theboattypees)<= 1: + del self.fields['boattype'] + else: + boattypechoices = [] + for choice in self.fields['boattype'].choices: + if choice[0] in theboattypees: + boattypechoices.append(choice) + self.fields['boattype'].choices = boattypechoices + + # weightcategory + theweightcategoryes = [record.weightcategory for record in records] + theweightcategoryes = list(set(theweightcategoryes)) + + if len(theweightcategoryes)<= 1: + del self.fields['weightcategory'] + else: + weightcategorychoices = [] + for choice in self.fields['weightcategory'].choices: + if choice[0] in theweightcategoryes: + weightcategorychoices.append(choice) + self.fields['weightcategory'].choices = weightcategorychoices + class WorkoutRaceSelectForm(forms.Form): # evaluate_after = forms.TimeField( # input_formats=['%H:%M:%S.%f', diff --git a/rowers/models.py b/rowers/models.py index 8f6ebcac..b2f26e18 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -1641,10 +1641,11 @@ class VirtualRaceResult(models.Model): u1 = rr.user.first_name, u2 = rr.user.last_name, ) - return u'Entry for {n} for "{r}" in {d}'.format( + return u'Entry for {n} for "{r}" in {c} {d}'.format( n = name, r = self.race, d = self.boattype, + c = self.boatclass, ) diff --git a/rowers/views.py b/rowers/views.py index ae26b810..143de466 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -13634,6 +13634,11 @@ def virtualevent_view(request,id=0): script,div = course_map(race.course) + + records = VirtualRaceResult.objects.filter( + race=race + ) + buttons = [] if not request.user.is_anonymous(): @@ -13656,15 +13661,31 @@ def virtualevent_view(request,id=0): buttons += ['editbutton'] if request.method == 'POST': - form = RaceResultFilterForm(request.POST) + form = RaceResultFilterForm(request.POST,records=records) if form.is_valid(): cd = form.cleaned_data - sex = cd['sex'] - boattype = cd['boattype'] - boatclass = cd['boatclass'] + try: + sex = cd['sex'] + except KeyError: + sex = ['female','male','mixed'] + + try: + boattype = cd['boattype'] + except KeyError: + boattype = types.waterboattype + + try: + boatclass = cd['boatclass'] + except KeyError: + boatclass = [t for t in types.otwtypes] + age_min = cd['age_min'] age_max = cd['age_max'] - weightcategory = cd['weightcategory'] + + try: + weightcategory = cd['weightcategory'] + except KeyError: + weightcategory = ['hwt','lwt'] results = VirtualRaceResult.objects.filter( race=race, @@ -13691,13 +13712,13 @@ def virtualevent_view(request,id=0): age__lte=age_max ) else: - form = RaceResultFilterForm() - results = VirtualRaceResult.objects.filter( race=race, workoutid__isnull=False, ).order_by("duration") + form = RaceResultFilterForm(records=records) + # to-do - add DNS dns = [] if timezone.now() > race.evaluation_closure: @@ -13708,10 +13729,6 @@ def virtualevent_view(request,id=0): - records = VirtualRaceResult.objects.filter( - race=race - ) - return render(request,'virtualevent.html', {