diff --git a/rowers/integrations/trainingpeaks.py b/rowers/integrations/trainingpeaks.py index 93a73089..cd3b6bb4 100644 --- a/rowers/integrations/trainingpeaks.py +++ b/rowers/integrations/trainingpeaks.py @@ -15,7 +15,7 @@ from rowingdata import rowingdata from rowers.rower_rules import is_workout_user import time from django_rq import job -from rowers.mytypes import tpmapping +from rowers.mytypes import tpmapping, tcxmapping from rowers.tasks import check_tp_workout_id, handle_workout_tp_upload @@ -68,7 +68,8 @@ class TPIntegration(SyncIntegration): newnotes = 'from '+w.workoutsource+' via rowsandall.com' try: - row.exporttotcx(tcxfilename, notes=newnotes, sport=tpmapping[w.workouttype]) + #row.exporttotcx(tcxfilename, notes=newnotes, sport=tcxmapping[w.workouttype]) + row.exporttotcx(tcxfilename, notes=newnotes, sport=None) except KeyError: row.exporttotcx(tcxfilename, notes=newnotes, sport='other') @@ -83,7 +84,8 @@ class TPIntegration(SyncIntegration): handle_workout_tp_upload, workout, thetoken, - tcxfilename + tcxfilename, + tpmapping[workout.workouttype] ) return job.id diff --git a/rowers/tasks.py b/rowers/tasks.py index 5903e38b..ebb7a07f 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -461,9 +461,10 @@ def handle_add_workouts_team(ws, t, debug=False, **kwargs): # pragma: no cover return 1 def uploadactivity(access_token, filename, description='', - name='Rowsandall.com workout'): # pragma: no cover + name='Rowsandall.com workout', workouttype='rowing'): # pragma: no cover data_gz = BytesIO() + try: with open(filename, 'rb') as inF: s = inF.read() @@ -478,12 +479,13 @@ def uploadactivity(access_token, filename, description='', 'Authorization': 'Bearer %s' % access_token } + # Data field is base64 encoded file read from filename data = { "UploadClient": "rowsandall", "Filename": filename, "SetWorkoutPublic": True, "Title": name, - "Type": "rowing", + "Type": workouttype, "Comment": description, "Data": base64.b64encode(data_gz.getvalue()).decode("ascii") } @@ -495,7 +497,7 @@ def uploadactivity(access_token, filename, description='', if resp.status_code not in (200, 202): # pragma: no cover dologging('tp_export.log',resp.status_code) dologging('tp_export.log',resp.reason) - dologging('tp_export.log',json.dumps(data)) + return 0, resp.reason, resp.status_code, headers else: return 1, "ok", 200, resp.headers @@ -564,26 +566,34 @@ def check_tp_workout_id(workout, location, attempts=5, debug=False, **kwargs): # workout.uploadedtotp = tpid record = create_or_update_syncrecord(workout.user, workout, tpid=tpid) workout.save() + else: + dologging('tp_export.log','failed to get workout id from trainingpeaks') + dologging('tp_export.log',response.text) + dologging('tp_export.log',status) + return 0 + else: + dologging('tp_export.log','failed to get workout id from trainingpeaks') + dologging('tp_export.log',response.text) + return 0 return 1 @app.task -def handle_workout_tp_upload(w, thetoken, tcxfilename, debug=False, **kwargs): # pragma: no cover +def handle_workout_tp_upload(w, thetoken, tcxfilename, workouttype, debug=False, **kwargs): # pragma: no cover tpid = 0 r = w.user dologging('tp_export.log','uploading workout {workoutid} to trainingpeaks for user {id}'.format(id=r.id,workoutid=w.id)) - if not tcxfilename: return 0 res, reason, status_code, headers = uploadactivity( thetoken, tcxfilename, - name=w.name + name=w.name, workouttype=workouttype, ) if res == 0: - w.tpid = -1 + w.uploadedtotcx = -1 try: os.remove(tcxfilename) except: diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index ec075d30..a22c712f 100644 Binary files a/rowers/tests/testdata/testdata.tcx.gz and b/rowers/tests/testdata/testdata.tcx.gz differ