Strava, ST, RK done
This commit is contained in:
@@ -29,7 +29,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, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET, SPORTTRACKS_CLIENT_SECRET, SPORTTRACKS_CLIENT_ID, SPORTTRACKS_REDIRECT_URI
|
||||
|
||||
@@ -61,6 +61,21 @@ def custom_exception_handler(exc,message):
|
||||
|
||||
return res
|
||||
|
||||
# Checks if user has SportTracks token, renews them if they are expired
|
||||
def sporttracks_open(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.sporttrackstoken == '') or (r.sporttrackstoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise SportTracksNoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.sporttrackstokenexpirydate):
|
||||
thetoken = sporttracksstuff.rower_sporttracks_token_refresh(user)
|
||||
else:
|
||||
thetoken = r.sporttrackstoken
|
||||
|
||||
return thetoken
|
||||
|
||||
|
||||
# Refresh ST token using refresh token
|
||||
def do_refresh_token(refreshtoken):
|
||||
client_auth = requests.auth.HTTPBasicAuth(SPORTTRACKS_CLIENT_ID, SPORTTRACKS_CLIENT_SECRET)
|
||||
@@ -303,3 +318,51 @@ def getidfromresponse(response):
|
||||
return int(id)
|
||||
|
||||
|
||||
def workout_sporttracks_upload(user,w):
|
||||
message = ""
|
||||
stid = 0
|
||||
# ready to upload. Hurray
|
||||
r = w.user
|
||||
|
||||
thetoken = sporttracks_open(user)
|
||||
|
||||
if (checkworkoutuser(user,w)):
|
||||
data = createsporttracksworkoutdata(w)
|
||||
if not data:
|
||||
message = "Data error"
|
||||
stid = 0
|
||||
return message,stid
|
||||
|
||||
authorizationstring = str('Bearer ' + thetoken)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json'}
|
||||
|
||||
url = "https://api.sporttracks.mobi/api/v2/fitnessActivities.json"
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data))
|
||||
|
||||
# check for duplicate error first
|
||||
if (response.status_code == 409 ):
|
||||
message = "Duplicate error"
|
||||
w.uploadedtosporttracks = -1
|
||||
stid = -1
|
||||
w.save()
|
||||
return message, stid
|
||||
elif (response.status_code == 201 or response.status_code==200):
|
||||
s= json.loads(response.text)
|
||||
stid = getidfromresponse(response)
|
||||
w.uploadedtosporttracks = stid
|
||||
w.save()
|
||||
return '',stid
|
||||
else:
|
||||
s = response
|
||||
message = "Something went wrong in workout_sporttracks_upload_view: %s" % s.reason
|
||||
stid = 0
|
||||
return message,stid
|
||||
|
||||
else:
|
||||
message = "You are not authorized to upload this workout"
|
||||
stid = 0
|
||||
return message,stid
|
||||
|
||||
return message,stid
|
||||
|
||||
Reference in New Issue
Block a user