TP done
This commit is contained in:
@@ -31,7 +31,7 @@ from django.contrib.auth.decorators import login_required
|
||||
# from .models import Profile
|
||||
from rowingdata import rowingdata
|
||||
import pandas as pd
|
||||
from rowers.models import Rower,Workout
|
||||
from rowers.models import Rower,Workout,checkworkoutuser
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
||||
@@ -93,6 +93,29 @@ def custom_exception_handler(exc,message):
|
||||
|
||||
return res
|
||||
|
||||
# Checks if user has UnderArmour token, renews them if they are expired
|
||||
def tp_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.tptoken == '') or (r.tptoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise TPNoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.tptokenexpirydate):
|
||||
res = tpstuff.do_refresh_token(r.tprefreshtoken)
|
||||
if res[0] != 0:
|
||||
r.tptoken = res[0]
|
||||
r.tprefreshtoken = res[2]
|
||||
expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1])
|
||||
r.tptokenexpirydate = expirydatetime
|
||||
r.save()
|
||||
thetoken = r.tptoken
|
||||
else:
|
||||
raise TPNoTokenError("Refresh token invalid")
|
||||
else:
|
||||
thetoken = r.tptoken
|
||||
|
||||
return thetoken
|
||||
|
||||
# Refresh ST token using refresh token
|
||||
def do_refresh_token(refreshtoken):
|
||||
client_auth = requests.auth.HTTPBasicAuth(TP_CLIENT_KEY, TP_CLIENT_SECRET)
|
||||
@@ -270,3 +293,48 @@ def uploadactivity(access_token,filename,description='',
|
||||
return 0
|
||||
|
||||
|
||||
def workout_tp_upload(user,w):
|
||||
message = ""
|
||||
tpid = 0
|
||||
r = w.user
|
||||
|
||||
thetoken = tp_open(r.user)
|
||||
|
||||
if (checkworkoutuser(user,w)):
|
||||
tcxfile = createtpworkoutdata(w)
|
||||
if tcxfile:
|
||||
res,reason,status_code,headers = 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.tpid = -1
|
||||
w.save()
|
||||
try:
|
||||
os.remove(tcxfile)
|
||||
except WindowsError:
|
||||
pass
|
||||
|
||||
return message,tpid
|
||||
|
||||
else: # res != 0
|
||||
w.uploadedtotp = res
|
||||
tpid = res
|
||||
w.save()
|
||||
os.remove(tcxfile)
|
||||
return '',tpid
|
||||
|
||||
else: # no tcxfile
|
||||
message = "Upload to TrainingPeaks failed"
|
||||
w.uploadedtotp = -1
|
||||
tpid = -1
|
||||
w.save()
|
||||
return message,tpid
|
||||
else: # not allowed to upload
|
||||
message = "You are not allowed to export this workout to TP"
|
||||
tpid = 0
|
||||
return message,tpid
|
||||
|
||||
return message,tpid
|
||||
|
||||
@@ -54,7 +54,7 @@ import c2stuff
|
||||
from c2stuff import C2NoTokenError,c2_open
|
||||
from runkeeperstuff import RunKeeperNoTokenError,runkeeper_open
|
||||
from sporttracksstuff import SportTracksNoTokenError,sporttracks_open
|
||||
from tpstuff import TPNoTokenError
|
||||
from tpstuff import TPNoTokenError,tp_open
|
||||
from iso8601 import ParseError
|
||||
import stravastuff
|
||||
from stravastuff import StravaNoTokenError
|
||||
@@ -1037,29 +1037,6 @@ def add_workout_from_underarmourdata(user,importid,data):
|
||||
|
||||
|
||||
|
||||
# Checks if user has UnderArmour token, renews them if they are expired
|
||||
def tp_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.tptoken == '') or (r.tptoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise TPNoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.tptokenexpirydate):
|
||||
res = tpstuff.do_refresh_token(r.tprefreshtoken)
|
||||
if res[0] != 0:
|
||||
r.tptoken = res[0]
|
||||
r.tprefreshtoken = res[2]
|
||||
expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1])
|
||||
r.tptokenexpirydate = expirydatetime
|
||||
r.save()
|
||||
thetoken = r.tptoken
|
||||
else:
|
||||
raise TPNoTokenError("Refresh token invalid")
|
||||
else:
|
||||
thetoken = r.tptoken
|
||||
|
||||
return thetoken
|
||||
|
||||
|
||||
# Export workout to TCX and send to user's email address
|
||||
@login_required()
|
||||
@@ -5891,6 +5868,14 @@ def workout_upload_view(request,message="",
|
||||
)
|
||||
except UnderArmourNoTokenError:
|
||||
pass
|
||||
|
||||
if (upload_to_tp):
|
||||
try:
|
||||
tpmessage,tpid = tpstuff.workout_tp_upload(
|
||||
request.user,w
|
||||
)
|
||||
except TPNoTokenError:
|
||||
pass
|
||||
|
||||
if message:
|
||||
url = reverse(workout_edit_view,
|
||||
|
||||
Reference in New Issue
Block a user