Merge branch 'feature/asyncsync' of bitbucket.org:sanderroosendaal/rowsandall into feature/asyncsync
This commit is contained in:
@@ -741,6 +741,25 @@ def getsmallrowdata_db(columns,ids=[],debug=False):
|
|||||||
|
|
||||||
return df
|
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):
|
def update_c2id_sql(id,c2id):
|
||||||
engine = create_engine(database_url, echo=False)
|
engine = create_engine(database_url, echo=False)
|
||||||
table = 'rowers_workout'
|
table = 'rowers_workout'
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ from rowers.utils import myqueue
|
|||||||
import rowers.mytypes as mytypes
|
import rowers.mytypes as mytypes
|
||||||
import gzip
|
import gzip
|
||||||
|
|
||||||
|
from rowers.tasks import handle_strava_sync
|
||||||
|
|
||||||
from rowsandall_app.settings import (
|
from rowsandall_app.settings import (
|
||||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
||||||
STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_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
|
# Upload the TCX file to Strava and set the workout activity type
|
||||||
# to rowing on Strava
|
# to rowing on Strava
|
||||||
def handle_stravaexport(f2,workoutname,stravatoken,description='',
|
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)
|
# w = Workout.objects.get(id=workoutid)
|
||||||
client = stravalib.Client(access_token=stravatoken)
|
client = stravalib.Client(access_token=stravatoken)
|
||||||
|
|
||||||
@@ -628,7 +630,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
|||||||
|
|
||||||
return id,message
|
return id,message
|
||||||
|
|
||||||
def workout_strava_upload(user,w, quick=False):
|
def workout_strava_upload(user,w, quick=False,asynchron=True):
|
||||||
try:
|
try:
|
||||||
thetoken = strava_open(user)
|
thetoken = strava_open(user)
|
||||||
except NoTokenError:
|
except NoTokenError:
|
||||||
@@ -641,55 +643,67 @@ def workout_strava_upload(user,w, quick=False):
|
|||||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
raise NoTokenError("Your hovercraft is full of eels")
|
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()
|
w.save()
|
||||||
try:
|
try:
|
||||||
os.remove(tcxfile)
|
os.remove(tcxfile)
|
||||||
except WindowsError:
|
except WindowsError:
|
||||||
pass
|
pass
|
||||||
message = mes
|
|
||||||
stravaid = res
|
|
||||||
return message,stravaid
|
return message,stravaid
|
||||||
else:
|
|
||||||
message = "Strava TCX data error "+tcxmesg
|
|
||||||
w.uploadedtostrava = -1
|
|
||||||
stravaid = -1
|
|
||||||
w.save()
|
|
||||||
return message, stravaid
|
|
||||||
|
|
||||||
except ActivityUploadFailed as e:
|
w.uploadedtostrava = res
|
||||||
message = "Strava Upload error: %s" % e
|
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
|
w.uploadedtostrava = -1
|
||||||
stravaid = -1
|
stravaid = -1
|
||||||
w.save()
|
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
|
return message,stravaid
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handle_strava_import_stroke_data(title,
|
def handle_strava_import_stroke_data(title,
|
||||||
useremail,
|
useremail,
|
||||||
stravatoken,
|
stravatoken,
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ def strip_tags(html):
|
|||||||
from rowers.dataprepnodjango import (
|
from rowers.dataprepnodjango import (
|
||||||
update_strokedata, new_workout_from_file,
|
update_strokedata, new_workout_from_file,
|
||||||
getsmallrowdata_db, updatecpdata_sql,update_c2id_sql,
|
getsmallrowdata_db, updatecpdata_sql,update_c2id_sql,
|
||||||
|
update_workout_field_sql,
|
||||||
update_agegroup_db,fitnessmetric_to_sql,
|
update_agegroup_db,fitnessmetric_to_sql,
|
||||||
add_c2_stroke_data_db,totaltime_sec_to_string,
|
add_c2_stroke_data_db,totaltime_sec_to_string,
|
||||||
create_c2_stroke_data_db,update_empower,
|
create_c2_stroke_data_db,update_empower,
|
||||||
@@ -100,6 +101,7 @@ import rowers.utils as utils
|
|||||||
import requests
|
import requests
|
||||||
import rowers.longtask as longtask
|
import rowers.longtask as longtask
|
||||||
import arrow
|
import arrow
|
||||||
|
import stravalib
|
||||||
|
|
||||||
from rowers.utils import get_strava_stream
|
from rowers.utils import get_strava_stream
|
||||||
|
|
||||||
@@ -122,7 +124,7 @@ def add(x, y):
|
|||||||
|
|
||||||
|
|
||||||
@app.task
|
@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)
|
response = requests.post(url,headers=headers,data=data)
|
||||||
if response.status_code not in [200,201]:
|
if response.status_code not in [200,201]:
|
||||||
return 0
|
return 0
|
||||||
@@ -130,10 +132,48 @@ def handle_c2_sync(workoutid,url,headers,data,debug=True,**kwargs):
|
|||||||
s = response.json()
|
s = response.json()
|
||||||
c2id = s['data']['id']
|
c2id = s['data']['id']
|
||||||
|
|
||||||
res = update_c2id_sql(workoutid,c2id)
|
res = update_workout_field_sql(workoutid,'uploadedtoc2',c2id,debug=debug)
|
||||||
|
|
||||||
return res
|
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
|
@app.task
|
||||||
def handle_c2_import_stroke_data(c2token,
|
def handle_c2_import_stroke_data(c2token,
|
||||||
c2id,workoutid,
|
c2id,workoutid,
|
||||||
|
|||||||
@@ -305,7 +305,6 @@ def myqueue(queue,function,*args,**kwargs):
|
|||||||
return MockJob()
|
return MockJob()
|
||||||
elif settings.CELERY:
|
elif settings.CELERY:
|
||||||
kwargs['debug'] = True
|
kwargs['debug'] = True
|
||||||
|
|
||||||
job = function.delay(*args,**kwargs)
|
job = function.delay(*args,**kwargs)
|
||||||
else:
|
else:
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|||||||
@@ -4822,7 +4822,7 @@ def workout_upload_view(request,
|
|||||||
if (upload_to_strava):
|
if (upload_to_strava):
|
||||||
try:
|
try:
|
||||||
message,id = stravastuff.workout_strava_upload(
|
message,id = stravastuff.workout_strava_upload(
|
||||||
request.user,w
|
request.user,w,
|
||||||
)
|
)
|
||||||
except NoTokenError:
|
except NoTokenError:
|
||||||
id = 0
|
id = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user