From d50ba322b8bb4f452c0dc3cf86aa3eb7a9025efa Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 10 May 2021 20:47:10 +0200 Subject: [PATCH] saving intermediate state --- rowers/utils.py | 50 ++++++++++++++++++++++++++++++++++++ rowers/views/workoutviews.py | 40 +++++++++++++++++------------ 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/rowers/utils.py b/rowers/utils.py index c3cfbeff..4330767b 100644 --- a/rowers/utils.py +++ b/rowers/utils.py @@ -1164,3 +1164,53 @@ def request_is_ajax(request): # is_ajax = True return is_ajax + +def intervals_to_string(vals, units, typ): + if vals is None or units is None or typ is None: + return '' + if len(vals) != len(units) or len(vals) != len(typ): + return '' + + s = '' + previous = 'rest' + for i in range(len(vals)): + if typ[i] == 'rest' and previous == 'rest': + if units[i] == 'min': + val = int(vals[i])*60 + unit = 'sec' + else: + val = int(vals[i]) + if units[i] == 'meters': + unit = 'm' + if units[i] == 'seconds': + unit = 'sec' + s += '+0min/{val}{unit}'.format(val=val,unit=unit) + elif typ[i] == 'rest': + if units[i] == 'min': + val = int(vals[i])*60 + unit = 'sec' + else: + val = int(vals[i]) + if units[i] == 'meters': + unit = 'm' + if units[i] == 'seconds': + unit = 'sec' + s += '/{val}{unit}'.format(val=val,unit=unit) + previous = 'rest' + else: # work interval + if units[i] == 'min': + val = int(vals[i])*60 + unit = 'sec' + else: + val = int(vals[i]) + if units[i] == 'meters': + unit = 'm' + if units[i] == 'seconds': + unit = 'sec' + s += '+{val}{unit}'.format(val=val,unit=unit) + previous = 'work' + + if s[0] == '+': + s = s[1:] + + return s diff --git a/rowers/views/workoutviews.py b/rowers/views/workoutviews.py index a273601f..1227ff68 100644 --- a/rowers/views/workoutviews.py +++ b/rowers/views/workoutviews.py @@ -13,6 +13,7 @@ import numpy from rowers.mailprocessing import send_confirm import rowers.uploads as uploads import rowers.utils as utils +from rowers.utils import intervals_to_string from urllib.parse import urlparse, parse_qs from json.decoder import JSONDecodeError @@ -2297,7 +2298,7 @@ def workout_view(request,id=0,raceresult=0,sessionresult=0,nocourseraceresult=0) intervaldata['itime'] = itime intervaldata['itype'] = itype - rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', debug=False,smoothwindow=15., activewindow = [startsecond,endsecond]) summary = rowdata.allstats() @@ -2316,7 +2317,7 @@ def workout_view(request,id=0,raceresult=0,sessionresult=0,nocourseraceresult=0) intervaldata['itime'] = itime intervaldata['itype'] = itype - rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', debug=False,smoothwindow=15., activewindow = [startsecond,endsecond]) summary = rowdata.allstats() @@ -2336,7 +2337,7 @@ def workout_view(request,id=0,raceresult=0,sessionresult=0,nocourseraceresult=0) intervaldata['itime'] = itime intervaldata['itype'] = itype - rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',0.1,mode='larger', debug=False,smoothwindow=15., activewindow = [startsecond,endsecond]) summary = rowdata.allstats() @@ -6240,7 +6241,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" if powerorpace == 'power' and power is not None: try: - rowdata.updateinterval_metric( + vals, units, typ = rowdata.updateinterval_metric( ' Power (watts)',power,mode='larger', debug=False,smoothwindow=15., activewindow=[activesecondsmin,activesecondsmax], @@ -6250,7 +6251,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" elif powerorpace == 'pace': # pragma: no cover try: velo = 500./pace_secs - rowdata.updateinterval_metric( + vals, units, typ = rowdata.updateinterval_metric( ' AverageBoatSpeed (m/s)',velo,mode='larger', debug=False,smoothwindow=15., activewindow=[activesecondsmin,activesecondsmax], @@ -6259,7 +6260,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" messages.error(request,'Error updating pace') elif powerorpace == 'work': # pragma: no cover try: - rowdata.updateinterval_metric( + vals, units, typ = rowdata.updateinterval_metric( 'driveenergy',work,mode='larger', debug=False,smoothwindow=15., activewindow=[activesecondsmin,activesecondsmax], @@ -6268,13 +6269,17 @@ def workout_summary_edit_view(request,id,message="",successmessage="" messages.error(request,'Error updating Work per Stroke') elif powerorpace == 'spm': # pragma: no cover try: - rowdata.updateinterval_metric( + vals, units, typ = rowdata.updateinterval_metric( ' Cadence (stokes/min)',spm,mode='larger', debug=False,smoothwindow=2., activewindow=[activesecondsmin,activesecondsmax],) except: messages.error(request,'Error updating SPM') + intervalString = '' + if vals is not None: + intervalString = intervals_to_string(vals, units, typ) + intervalstats = rowdata.allstats() itime,idist,itype = rowdata.intervalstats_values() nrintervals = len(idist) @@ -6296,7 +6301,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" 'activeminutesmin': activeminutesmin, 'activeminutesmax': activeminutesmax, } - form = SummaryStringForm() + form = SummaryStringForm(initial={'intervalstring':intervalString}) powerupdateform = PowerIntervalUpdateForm(initial=data) savebutton = 'savepowerpaceform' formvalues = { @@ -6368,7 +6373,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" pace_secs = 120. if powerorpace == 'power' and power is not None: - rowdata.updateinterval_metric(' Power (watts)',power,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' Power (watts)',power,mode='larger', debug=False,smoothwindow=15, activewindow=[activesecondsmin,activesecondsmax], ) @@ -6376,7 +6381,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" elif powerorpace == 'pace': # pragma: no cover try: velo = 500./pace_secs - rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',velo,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' AverageBoatSpeed (m/s)',velo,mode='larger', debug=False,smoothwindow=15, activewindow=[activesecondsmin,activesecondsmax], ) @@ -6384,7 +6389,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" messages.error(request,'Error updating pace') elif powerorpace == 'work': # pragma: no cover try: - rowdata.updateinterval_metric( + vals, units, typ = rowdata.updateinterval_metric( 'driveenergy',work,mode='larger', debug=False,smoothwindow=15., activewindow=[activesecondsmin,activesecondsmax],) @@ -6392,7 +6397,7 @@ def workout_summary_edit_view(request,id,message="",successmessage="" messages.error(request,'Error updating Work per Stroke') elif powerorpace == 'spm': # pragma: no cover try: - rowdata.updateinterval_metric(' Cadence (stokes/min)',spm,mode='larger', + vals, units, typ = rowdata.updateinterval_metric(' Cadence (stokes/min)',spm,mode='larger', debug=False,smoothwindow=2., activewindow=[activesecondsmin,activesecondsmax], ) @@ -6400,6 +6405,11 @@ def workout_summary_edit_view(request,id,message="",successmessage="" messages.error(request,'Error updating SPM') + intervalString = '' + if vals is not None: + intervalString = intervals_to_string(vals, units, typ) + + intervalstats = rowdata.allstats() itime,idist,itype = rowdata.intervalstats_values() nrintervals = len(idist) @@ -6414,11 +6424,10 @@ def workout_summary_edit_view(request,id,message="",successmessage="" 'activeminutesmax': activeminutesmax, } powerupdateform = PowerIntervalUpdateForm(initial=cd) - form = SummaryStringForm() + form = SummaryStringForm(initial={'intervalstring':intervalString}) + - form = SummaryStringForm() - # we are saving the results obtained from the detailed form elif request.method == 'POST' and "savedetailform" in request.POST: # pragma: no cover savebutton = 'savedetailform' @@ -6562,7 +6571,6 @@ def workout_summary_edit_view(request,id,message="",successmessage="" div = '' # render page - return render(request, 'summary_edit.html', {'form':form, 'activeminutesmax':activeminutesmax,