From 5deaa194f06879d196452c3a9be5581730554f3e Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 15 Feb 2017 16:16:02 +0100 Subject: [PATCH] making upload options sticky and adding make private --- rowers/dataprep.py | 22 ++++++++++++++++------ rowers/forms.py | 7 ++++--- rowers/views.py | 42 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/rowers/dataprep.py b/rowers/dataprep.py index 386f8a88..0b16e99f 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -228,7 +228,8 @@ def timedeltaconv(x): # Processes painsled CSV file to database def save_workout_database(f2,r,dosmooth=True,workouttype='rower', dosummary=True,title='Workout', - notes='',totaldist=0,totaltime=0): + notes='',totaldist=0,totaltime=0, + makeprivate=False): message = None powerperc = 100*np.array([r.pw_ut2, r.pw_ut1, @@ -313,12 +314,18 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower', workoutstarttime = row.rowdatetime.strftime('%H:%M:%S') workoutstartdatetime = thetimezone.localize(row.rowdatetime).astimezone(utc) + if makeprivate: + privacy = 'private' + else: + privacy = 'visible' + # check for duplicate start times ws = Workout.objects.filter(starttime=workoutstarttime, user=r) if (len(ws) != 0): message = "Warning: This workout probably already exists in the database" + w = Workout(user=r,name=title,date=workoutdate, workouttype=workouttype, duration=duration,distance=totaldist, @@ -326,15 +333,16 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower', starttime=workoutstarttime, csvfilename=f2,notes=notes,summary=summary, maxhr=maxhr,averagehr=averagehr, - startdatetime=workoutstartdatetime) + startdatetime=workoutstartdatetime, + privacy=privacy) w.save() - ts = Team.objects.filter(rower=r) - - for t in ts: - w.team.add(t) + if privacy == 'visible': + ts = Team.objects.filter(rower=r) + for t in ts: + w.team.add(t) # put stroke data in database res = dataprep(row.df,id=w.id,bands=True, @@ -422,6 +430,7 @@ def handle_nonpainsled(f2,fileformat,summary=''): def new_workout_from_file(r,f2, workouttype='rower', title='Workout', + makeprivate=False, notes=''): message = None fileformat = get_file_type(f2) @@ -470,6 +479,7 @@ def new_workout_from_file(r,f2, dosummary = (fileformat != 'fit') id,message = save_workout_database(f2,r, workouttype=workouttype, + makeprivate=makeprivate, dosummary=dosummary, title=title) diff --git a/rowers/forms.py b/rowers/forms.py index 0644ff5b..0b7c53b7 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -76,14 +76,15 @@ class UploadOptionsForm(forms.Form): ('distanceplot','Distance Plot'), ('pieplot','Pie Chart'), ) - make_plot = forms.BooleanField(initial=False) + make_plot = forms.BooleanField(initial=False,required=False) plottype = forms.ChoiceField(required=False, choices=plotchoices, initial='timeplot') - upload_to_C2 = forms.BooleanField(initial=False) + upload_to_C2 = forms.BooleanField(initial=False,required=False) + makeprivate = forms.BooleanField(initial=False,required=False) class Meta: - fields = ['make_plot','plottype','upload_toc2'] + fields = ['make_plot','plottype','upload_toc2','makeprivate'] # This form is used on the Analysis page to add a custom distance/time # trial and predict the pace diff --git a/rowers/views.py b/rowers/views.py index 137f4b77..b18410b2 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -4181,7 +4181,26 @@ def workout_getc2workout_view(request,c2id): # This is the main view for processing uploaded files @login_required() -def workout_upload_view(request,message=""): +def workout_upload_view(request,message="", + uploadoptions={ + 'makeprivate':False, + 'make_plot':False, + 'upload_to_C2':False, + 'plottype':'timeplot', + }): + + if 'uploadoptions' in request.session: + uploadoptions = request.session['uploadoptions'] + else: + request.session['uploadoptions'] = uploadoptions + + + + makeprivate = uploadoptions['makeprivate'] + make_plot = uploadoptions['make_plot'] + plottype = uploadoptions['plottype'] + upload_toc2 = uploadoptions['upload_to_C2'] + r = Rower.objects.get(user=request.user) if request.method == 'POST': form = DocumentsForm(request.POST,request.FILES) @@ -4193,10 +4212,22 @@ def workout_upload_view(request,message=""): workouttype = form.cleaned_data['workouttype'] notes = form.cleaned_data['notes'] - make_plot = request.POST.getlist('make_plot') - plottype = request.POST['plottype'] - upload_to_c2 = request.POST.getlist('upload_to_C2') + if optionsform.is_valid(): + make_plot = optionsform.cleaned_data['make_plot'] + plottype = optionsform.cleaned_data['plottype'] + upload_to_c2 = optionsform.cleaned_data['upload_to_C2'] + makeprivate = optionsform.cleaned_data['makeprivate'] + + uploadoptions = { + 'makeprivate':makeprivate, + 'make_plot':make_plot, + 'plottype':plottype, + 'upload_to_C2':upload_to_c2, + } + + + request.session['uploadoptions'] = uploadoptions f1 = res[0] # file name f2 = res[1] # file name incl media directory @@ -4204,6 +4235,7 @@ def workout_upload_view(request,message=""): id,message = dataprep.new_workout_from_file(r,f2, workouttype=workouttype, + makeprivate=makeprivate, title = t, notes='') if not id: @@ -4344,7 +4376,7 @@ def workout_upload_view(request,message=""): return response else: form = DocumentsForm() - optionsform = UploadOptionsForm() + optionsform = UploadOptionsForm(initial=uploadoptions) return render(request, 'document_form.html', {'form':form, 'optionsform': optionsform,