From 7dfff7e3df449496ceae8b5a281ccd2ac0c1b748 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 8 Dec 2016 11:50:58 +0100 Subject: [PATCH] added reststrokes to favoritechart settings --- rowers/dataprep.py | 12 ++++++-- rowers/models.py | 8 +++-- rowers/templates/flexchart3.html | 5 ++++ rowers/templates/flexchart3otw.html | 5 ++++ rowers/views.py | 46 ++++++++++++++++++++++------- 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/rowers/dataprep.py b/rowers/dataprep.py index 38fafec0..dca076d2 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -162,8 +162,13 @@ def prepmultipledata(ids,verbose=False): res = list(itertools.chain.from_iterable(res.fetchall())) conn.close() engine.dispose() - - res = list(set(ids)-set(res)) + + try: + ids2 = [int(id) for id in ids] + except ValueError: + ids2 = ids + + res = list(set(ids2)-set(res)) for id in res: rowdata,row = getrowdata(id=id) if verbose: @@ -196,7 +201,8 @@ def read_cols_df_sql(ids,columns): columns = cls, ids = tuple(ids), )) - + + connection = engine.raw_connection() df = pd.read_sql_query(query,engine) engine.dispose() return df diff --git a/rowers/models.py b/rowers/models.py index d25a36b4..bce7c2c4 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -157,12 +157,15 @@ class FavoriteChart(models.Model): workouttype = models.CharField(max_length=50,choices=workouttypechoices, default='both', verbose_name='Workout Type') + reststrokes = models.BooleanField(default=True,verbose_name="Incl. Rest") user = models.ForeignKey(Rower) + class FavoriteForm(ModelForm): class Meta: model = FavoriteChart - fields = ['xparam','yparam1','yparam2','plottype','workouttype'] + fields = ['xparam','yparam1','yparam2', + 'plottype','workouttype','reststrokes'] class BaseFavoriteFormSet(BaseFormSet): def clean(self): @@ -175,7 +178,8 @@ class BaseFavoriteFormSet(BaseFormSet): yparam1 = form.cleaned_data['yparam1'] yparam2 = form.cleaned_data['yparam2'] plottype = form.cleaned_data['plottype'] - + reststrokes = form.cleaned_data['reststrokes'] + if not xparam: raise forms.ValidationError( 'Must have x parameter.', diff --git a/rowers/templates/flexchart3.html b/rowers/templates/flexchart3.html index 3f6a2dd8..ecb92d10 100644 --- a/rowers/templates/flexchart3.html +++ b/rowers/templates/flexchart3.html @@ -183,6 +183,11 @@
{% csrf_token %} + {% if workstrokesonly %} + + {% else %} + + {% endif %}
diff --git a/rowers/templates/flexchart3otw.html b/rowers/templates/flexchart3otw.html index 37525b3a..62938673 100644 --- a/rowers/templates/flexchart3otw.html +++ b/rowers/templates/flexchart3otw.html @@ -218,6 +218,11 @@
{% csrf_token %} + {% if workstrokesonly %} + + {% else %} + + {% endif %}
diff --git a/rowers/views.py b/rowers/views.py index cc2e29b6..c89e8e61 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -2577,11 +2577,6 @@ def workout_comparison_view2(request,id1=0,id2=0,xparam='distance', def workout_flexchart3_view(request,*args,**kwargs): -# xparam='distance',yparam1='pace', -# yparam2='hr',plottype='line', -# promember=0): - - try: id = kwargs['id'] @@ -2620,7 +2615,14 @@ def workout_flexchart3_view(request,*args,**kwargs): favorites = FavoriteChart.objects.filter(user=r, workouttype__in=[workouttype,'both']).order_by("id") maxfav = len(favorites)-1 - + + # check if favoritenr is not out of range + if favorites: + try: + t = favorites[favoritenr].xparam + except IndexError: + favoritenr=0 + if 'xparam' in kwargs: xparam = kwargs['xparam'] else: @@ -2657,10 +2659,21 @@ def workout_flexchart3_view(request,*args,**kwargs): else: plottype = 'line' + if 'workstrokesonly' in kwargs: + workstrokesonly = kwargs['workstrokesonly'] + else: + if favorites: + workstrokesonly = not favorites[favoritenr].reststrokes + else: + workstrokesonly = False + if request.method == 'POST' and 'savefavorite' in request.POST: + workstrokesonly = request.POST['workstrokesonlysave'] + reststrokes = not workstrokesonly f = FavoriteChart(user=r,xparam=xparam, yparam1=yparam1,yparam2=yparam2, - plottype=plottype,workouttype=workouttype) + plottype=plottype,workouttype=workouttype, + reststrokes=reststrokes) f.save() if request.method == 'POST' and 'workstrokesonly' in request.POST: workstrokesonly = request.POST['workstrokesonly'] @@ -2668,8 +2681,6 @@ def workout_flexchart3_view(request,*args,**kwargs): workstrokesonly = True else: workstrokesonly = False - else: - workstrokesonly = False # create interactive plot res = interactive_flex_chart2(id,xparam=xparam,yparam1=yparam1, @@ -4508,13 +4519,18 @@ def rower_favoritecharts_view(request): successmessage = '' r = Rower.objects.get(user=request.user) favorites = FavoriteChart.objects.filter(user=r).order_by('id') + aantal = len(favorites) favorites_data = [{'yparam1':f.yparam1, 'yparam2':f.yparam2, 'xparam':f.xparam, 'plottype':f.plottype, - 'workouttype':f.workouttype} + 'workouttype':f.workouttype, + 'reststrokes':f.reststrokes} for f in favorites] FavoriteChartFormSet = formset_factory(FavoriteForm,formset=BaseFavoriteFormSet,extra=0) + if aantal==0: + FavoriteChartFormSet = formset_factory(FavoriteForm,formset=BaseFavoriteFormSet,extra=1) + if request.method == 'POST': favorites_formset = FavoriteChartFormSet(request.POST) @@ -4527,18 +4543,26 @@ def rower_favoritecharts_view(request): xparam = favorites_form.cleaned_data.get('xparam') plottype = favorites_form.cleaned_data.get('plottype') workouttype = favorites_form.cleaned_data.get('workouttype') + reststrokes = favorites_form.cleaned_data.get('reststrokes') new_instances.append(FavoriteChart(user=r, yparam1=yparam1, yparam2=yparam2, xparam=xparam, plottype=plottype, - workouttype=workouttype)) + workouttype=workouttype, + reststrokes=reststrokes)) try: with transaction.atomic(): FavoriteChart.objects.filter(user=r).delete() FavoriteChart.objects.bulk_create(new_instances) successmessage = "You have updated your favorites" + FavoriteChartFormSet=formset_factory(FavoriteForm,formset=BaseFavoriteFormSet) + print new_instances + print "aap",len(new_instances) + if len(new_instances)==0: + FavoriteChartFormSet=formset_factory(FavoriteForm,formset=BaseFavoriteFormSet,extra=1) + favorites_formset = FavoriteChartFormSet() except IntegrityError: message = "something went wrong"