Private
Public Access
1
0

async strava sync

This commit is contained in:
Sander Roosendaal
2020-03-10 15:53:18 +01:00
parent f2635651aa
commit 718453de96
5 changed files with 76 additions and 41 deletions

View File

@@ -29,6 +29,8 @@ from rowers.utils import myqueue
import rowers.mytypes as mytypes
import gzip
from rowers.tasks import handle_strava_sync
from rowsandall_app.settings import (
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET
@@ -442,7 +444,7 @@ def createstravaworkoutdata(w,dozip=True):
# Upload the TCX file to Strava and set the workout activity type
# to rowing on Strava
def handle_stravaexport(f2,workoutname,stravatoken,description='',
activity_type='Rowing',quick=False):
activity_type='Rowing',quick=False,asynchron=False):
# w = Workout.objects.get(id=workoutid)
client = stravalib.Client(access_token=stravatoken)
@@ -628,7 +630,7 @@ def add_workout_from_data(user,importid,data,strokedata,
return id,message
def workout_strava_upload(user,w, quick=False):
def workout_strava_upload(user,w, quick=False,asynchron=True):
try:
thetoken = strava_open(user)
except NoTokenError:
@@ -641,55 +643,67 @@ def workout_strava_upload(user,w, quick=False):
if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize"
raise NoTokenError("Your hovercraft is full of eels")
else:
if (is_workout_user(user,w)):
try:
tcxfile,tcxmesg = 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',
activity_type=r.stravaexportas,quick=quick)
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
if (is_workout_user(user,w)):
if asynchron:
tcxfile, tcxmesg = createstravaworkoutdata(w)
if not tcxfile:
return "Failed to create workout data",0
job = myqueue(queue,
handle_strava_sync,
r.stravatoken,
w.id,
tcxfile,w.name,r.stravaexportas,
w.notes
)
return "Asynchronous sync",-1
try:
tcxfile,tcxmesg = 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',
activity_type=r.stravaexportas,quick=quick,asynchron=asynchron)
if res==0:
message = mes
w.uploadedtostrava = -1
stravaid = -1
w.save()
try:
os.remove(tcxfile)
except WindowsError:
pass
message = mes
stravaid = res
return message,stravaid
else:
message = "Strava TCX data error "+tcxmesg
w.uploadedtostrava = -1
stravaid = -1
w.save()
return message, stravaid
except ActivityUploadFailed as e:
message = "Strava Upload error: %s" % e
w.uploadedtostrava = res
w.save()
try:
os.remove(tcxfile)
except WindowsError:
pass
message = mes
stravaid = res
return message,stravaid
else:
message = "Strava TCX data error "+tcxmesg
w.uploadedtostrava = -1
stravaid = -1
w.save()
os.remove(tcxfile)
return message,stravaid
return message,stravaid
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
def handle_strava_import_stroke_data(title,
useremail,
stravatoken,