From e55682edd0428052c1ca193ad9de86e6152488e3 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 31 Dec 2020 06:56:50 +0100 Subject: [PATCH] refactoring and adding email confirmation / trimp calc to api v2 --- apilog.log | 22 ++++++++++ rowers/dataprep.py | 90 +++++++++++++++++++++------------------- rowers/views/apiviews.py | 19 ++++++++- 3 files changed, 88 insertions(+), 43 deletions(-) create mode 100644 apilog.log diff --git a/apilog.log b/apilog.log new file mode 100644 index 00000000..06d4998a --- /dev/null +++ b/apilog.log @@ -0,0 +1,22 @@ +2020-12-31 05:38:33.044672+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:39:20.796493+00:00: sander (strokedatajson_v2 POST) +No data in request +2020-12-31 05:39:35.245274+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:40:51.057825+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:40:52.842430+00:00: sander (strokedatajson_v2 POST completed successfully) +2020-12-31 05:50:10.246269+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:50:54.083948+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:51:50.273905+00:00: sander (strokedatajson_v2 POST) +No data in request +1595667900.0: sander(strokedatajson_v2 POST - data parsed) +2020-12-31 05:51:52.185284+00:00: sander (strokedatajson_v2 POST completed successfully) diff --git a/rowers/dataprep.py b/rowers/dataprep.py index 3d7ae8fb..d72c1517 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -1430,6 +1430,53 @@ def create_row_df(r,distance,duration,startdatetime,workouttype='rower', from rowers.utils import totaltime_sec_to_string +def checkbreakthrough(w, r): + isbreakthrough = False + ishard = False + workouttype = w.workouttype + if workouttype in rowtypes: + cpdf,delta,cpvalues = setcp(w) + if not cpdf.empty: + if workouttype in otwtypes: + res, btvalues, res2 = utils.isbreakthrough( + delta, cpvalues, r.p0, r.p1, r.p2, r.p3, r.cpratio) + success = update_rolling_cp(r,otwtypes,'water') + + elif workouttype in otetypes: + res, btvalues, res2 = utils.isbreakthrough( + delta, cpvalues, r.ep0, r.ep1, r.ep2, r.ep3, r.ecpratio) + success = update_rolling_cp(r,otetypes,'erg') + else: + res = 0 + res2 = 0 + if res: + isbreakthrough = True + if res2 and not isbreakthrough: + ishard = True + + # submit email task to send email about breakthrough workout + if isbreakthrough: + if r.getemailnotifications and not r.emailbounced: + job = myqueue(queuehigh,handle_sendemail_breakthrough, + w.id, + r.user.email, + r.user.first_name, + r.user.last_name, + btvalues=btvalues.to_json()) + + # submit email task to send email about breakthrough workout + if ishard: + if r.getemailnotifications and not r.emailbounced: + job = myqueue(queuehigh,handle_sendemail_hard, + w.id, + r.user.email, + r.user.first_name, + r.user.last_name, + btvalues=btvalues.to_json()) + + return isbreakthrough, ishard + + # Processes painsled CSV file to database def save_workout_database(f2, r, dosmooth=True, workouttype='rower', boattype='1x', @@ -1701,48 +1748,7 @@ def save_workout_database(f2, r, dosmooth=True, workouttype='rower', job = myqueue(queuehigh,handle_calctrimp,w.id,f2,r.ftp,r.sex,r.hrftp,r.max,r.rest) - - isbreakthrough = False - ishard = False - if workouttype in rowtypes: - cpdf,delta,cpvalues = setcp(w) - if not cpdf.empty: - if workouttype in otwtypes: - res, btvalues, res2 = utils.isbreakthrough( - delta, cpvalues, r.p0, r.p1, r.p2, r.p3, r.cpratio) - success = update_rolling_cp(r,otwtypes,'water') - - elif workouttype in otetypes: - res, btvalues, res2 = utils.isbreakthrough( - delta, cpvalues, r.ep0, r.ep1, r.ep2, r.ep3, r.ecpratio) - success = update_rolling_cp(r,otetypes,'erg') - else: - res = 0 - res2 = 0 - if res: - isbreakthrough = True - if res2 and not isbreakthrough: - ishard = True - - # submit email task to send email about breakthrough workout - if isbreakthrough: - if r.getemailnotifications and not r.emailbounced: - job = myqueue(queuehigh,handle_sendemail_breakthrough, - w.id, - r.user.email, - r.user.first_name, - r.user.last_name, - btvalues=btvalues.to_json()) - - # submit email task to send email about breakthrough workout - if ishard: - if r.getemailnotifications and not r.emailbounced: - job = myqueue(queuehigh,handle_sendemail_hard, - w.id, - r.user.email, - r.user.first_name, - r.user.last_name, - btvalues=btvalues.to_json()) + isbreakthrough, ishard = checkbreakthrough(w, r) return (w.id, message) diff --git a/rowers/views/apiviews.py b/rowers/views/apiviews.py index 8e446f2b..b8ceaf67 100644 --- a/rowers/views/apiviews.py +++ b/rowers/views/apiviews.py @@ -4,7 +4,8 @@ from __future__ import print_function from __future__ import unicode_literals from rowers.views.statements import * - +from rowers.tasks import handle_calctrimp +from rowers.mailprocessing import send_confirm # Stroke data form to test API upload @login_required() @@ -310,6 +311,22 @@ def strokedatajson_v2(request,id): datadf = dataprep.dataprep(rowdata,id=row.id,bands=True,barchart=True,otwpower=True,empower=True) + job = myqueue(queuehigh, handle_calctrimp, row.id, row.csvfilename, r.ftp,r.sex,r.hrftp, r.max, r.rest) + + isbreakthrough, ishard = dataprep.checkbreakthrough(row, r) + + if r.getemailnotifications and not r.emailbounced: + link = settings.SITE_URL+reverse( + r.defaultlandingpage, + kwargs = { + 'id':encoder.encode_hex(row.id), + } + ) + email_sent = send_confirm(r.user, row.name, link, '') + + result = uploads.do_sync(row,{},quick=True) + + with open('apilog.log','a') as logfile: logfile.write(str(timezone.now())+": ") logfile.write(request.user.username+" (strokedatajson_v2 POST completed successfully) \n")