From f03892f1c734ef51b0b83d23150d7e1631d18e13 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 11 May 2017 20:49:57 +0200 Subject: [PATCH] sporttracks callback --- rowers/sporttracksstuff.py | 15 +++++++++++---- rowers/tpstuff.py | 4 +--- rowers/views.py | 8 ++++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/rowers/sporttracksstuff.py b/rowers/sporttracksstuff.py index 629f166e..2319cf77 100644 --- a/rowers/sporttracksstuff.py +++ b/rowers/sporttracksstuff.py @@ -122,11 +122,18 @@ def get_token(code): response = requests.post(url, data=json.dumps(post_data), headers=headers) - token_json = response.json() - thetoken = token_json['access_token'] - expires_in = token_json['expires_in'] - refresh_token = token_json['refresh_token'] + if response.status_code == 200 or response.status_code == 201: + token_json = response.json() + thetoken = token_json['access_token'] + expires_in = token_json['expires_in'] + try: + refresh_token = token_json['refresh_token'] + except KeyError: + refresh_token = refreshtoken + else: + return [0,0,0] + return [thetoken,expires_in,refresh_token] # Make authorization URL including random string diff --git a/rowers/tpstuff.py b/rowers/tpstuff.py index efff14bb..f0b0ef67 100644 --- a/rowers/tpstuff.py +++ b/rowers/tpstuff.py @@ -145,7 +145,7 @@ def do_refresh_token(refreshtoken): data=post_data, headers=headers) - if response.status_code == 200: + if response.status_code == 200 or response.status_code == 201: token_json = response.json() thetoken = token_json['access_token'] expires_in = token_json['expires_in'] @@ -316,9 +316,7 @@ def workout_tp_upload(user,w): ) if res == 0: message = "Upload to TrainingPeaks failed with status code "+str(status_code)+": "+reason - w.uploadedtotp = -1 w.tpid = -1 - w.save() try: os.remove(tcxfile) except WindowsError: diff --git a/rowers/views.py b/rowers/views.py index eac39e2d..a1685e48 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -1170,12 +1170,12 @@ def workout_tp_upload_view(request,id=0): if (checkworkoutuser(request.user,w)): tcxfile = tpstuff.createtpworkoutdata(w) if tcxfile: - res,reason,status_code,headers = tpstuff.uploadactivity(r.tptoken,tcxfile, - name=w.name) + res,reason,status_code,headers = tpstuff.uploadactivity( + r.tptoken,tcxfile, + name=w.name + ) if res == 0: message = "Upload to TrainingPeaks failed with status code "+str(status_code)+": "+reason - w.uploadedtotp = -1 - w.save() try: os.remove(tcxfile) except WindowsError: