Private
Public Access
1
0

Merge branch 'release/v11.29'

This commit is contained in:
Sander Roosendaal
2020-03-10 21:08:42 +01:00
5 changed files with 113 additions and 41 deletions

View File

@@ -741,6 +741,25 @@ def getsmallrowdata_db(columns,ids=[],debug=False):
return df
def update_workout_field_sql(workoutid,fieldname,value,debug=False):
if debug:
engine = create_engine(database_url_debug, echo=False)
else:
engine = create_engine(database_url, echo=False)
table = 'rowers_workout'
query = "UPDATE %s SET %s = %s WHERE `id` = %s;" % (table,fieldname,value,workoutid)
with engine.connect() as conn, conn.begin():
result = conn.execute(query)
conn.close()
engine.dispose()
return 1
def update_c2id_sql(id,c2id):
engine = create_engine(database_url, echo=False)
table = 'rowers_workout'

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,

View File

@@ -76,6 +76,7 @@ def strip_tags(html):
from rowers.dataprepnodjango import (
update_strokedata, new_workout_from_file,
getsmallrowdata_db, updatecpdata_sql,update_c2id_sql,
update_workout_field_sql,
update_agegroup_db,fitnessmetric_to_sql,
add_c2_stroke_data_db,totaltime_sec_to_string,
create_c2_stroke_data_db,update_empower,
@@ -100,6 +101,7 @@ import rowers.utils as utils
import requests
import rowers.longtask as longtask
import arrow
import stravalib
from rowers.utils import get_strava_stream
@@ -122,7 +124,7 @@ def add(x, y):
@app.task
def handle_c2_sync(workoutid,url,headers,data,debug=True,**kwargs):
def handle_c2_sync(workoutid,url,headers,data,debug=False,**kwargs):
response = requests.post(url,headers=headers,data=data)
if response.status_code not in [200,201]:
return 0
@@ -130,10 +132,48 @@ def handle_c2_sync(workoutid,url,headers,data,debug=True,**kwargs):
s = response.json()
c2id = s['data']['id']
res = update_c2id_sql(workoutid,c2id)
res = update_workout_field_sql(workoutid,'uploadedtoc2',c2id,debug=debug)
return res
@app.task
def handle_sporttracks_sync(workoutid,url,headers,data,debug=False,**kwargs):
response = requests.post(url,headers=headers,data=data)
if response.status_code not in [200,201]:
return 0
t = response.json()
uri = t['uris'][0]
regex = '.*?sporttracks\.mobi\/api\/v2\/fitnessActivities/(\d+)\.json$'
m = re.compile(regex).match(uri).group(1)
id = int(m)
res = update_workout_field_sql(workoutid,'uploadedtosporttracks',id,debug=debug)
@app.task
def handle_strava_sync(stravatoken,workoutid,filename,name,activity_type,description,debug=False,**kwargs):
client = stravalib.Client(access_token=stravatoken)
with open(filename,'rb') as f:
act = client.upload_activity(f,'tcx.gz',name=name)
res = act.wait(poll_interval=5.0, timeout=60)
try:
act = client.update_activity(res.id,activity_type=activity_type,
description=description,device_name='Rowsandall.com')
except TypeError:
act = client.update_activity(res.id,activity_type=activity_type,
description=description)
result = update_workout_field_sql(workoutid,'uploadedtostrava',res.id,debug=debug)
try:
os.remove(filename)
except WindowsError:
pass
return 1
@app.task
def handle_c2_import_stroke_data(c2token,
c2id,workoutid,

View File

@@ -305,7 +305,6 @@ def myqueue(queue,function,*args,**kwargs):
return MockJob()
elif settings.CELERY:
kwargs['debug'] = True
job = function.delay(*args,**kwargs)
else:
if settings.DEBUG:

View File

@@ -4822,7 +4822,7 @@ def workout_upload_view(request,
if (upload_to_strava):
try:
message,id = stravastuff.workout_strava_upload(
request.user,w
request.user,w,
)
except NoTokenError:
id = 0