Private
Public Access
1
0

Strava, ST, RK done

This commit is contained in:
Sander Roosendaal
2017-05-05 11:54:37 +02:00
parent 25c67319f8
commit 4b80e30b74
11 changed files with 420 additions and 507 deletions

View File

@@ -29,8 +29,10 @@ from django.contrib.auth.decorators import login_required
from rowingdata import rowingdata
import pandas as pd
from rowers.models import Rower,Workout
from rowers.models import checkworkoutuser
import stravalib
from stravalib.exc import ActivityUploadFailed,TimeoutExceeded
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET
@@ -77,6 +79,15 @@ def custom_exception_handler(exc,message):
return res
# Custom error class - to raise a NoTokenError
class StravaNoTokenError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return repr(self.value)
# Exchange access code for long-lived access token
def get_token(code):
client_auth = requests.auth.HTTPBasicAuth(STRAVA_CLIENT_ID, STRAVA_CLIENT_SECRET)
@@ -277,3 +288,56 @@ def handle_stravaexport(f2,workoutname,stravatoken,description=''):
return (res.id,message)
def workout_strava_upload(user,w):
message = ""
stravaid=-1
r = Rower.objects.get(user=user)
res = -1
if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize"
raise StravaNoTokenError
else:
if (checkworkoutuser(user,w)):
try:
tcxfile = createstravaworkoutdata(w)
if tcxfile:
with open(tcxfile,'rb') as f:
res,mes = handle_stravaexport(f,w.name,
r.stravatoken,
description=w.notes+'\n from '+w.workoutsource+' via rowsandall.com')
if res==0:
message = mes
w.uploadedtostrava = -1
stravaid = -1
w.save()
try:
os.remove(tcxfile)
except WindowsError:
pass
return message,stravaid
w.uploadedtostrava = res
w.save()
try:
os.remove(tcxfile)
except WindowsError:
pass
message = ''
stravaid = res
return message,stravaid
else:
message = "Strava Upload error"
w.uploadedtostrava = -1
stravaid = -1
w.save()
return message, stravaid
except ActivityUploadFailed as e:
message = "Strava Upload error: %s" % e
w.uploadedtostrava = -1
stravaid = -1
w.save()
os.remove(tcxfile)
return message,stravaid
return message,stravaid
return message,stravaid