c2 to async
This commit is contained in:
@@ -25,7 +25,7 @@ from rowsandall_app.settings import (
|
|||||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
|
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
|
||||||
)
|
)
|
||||||
|
|
||||||
from rowers.tasks import handle_c2_import_stroke_data
|
from rowers.tasks import handle_c2_import_stroke_data, handle_c2_sync
|
||||||
import django_rq
|
import django_rq
|
||||||
queue = django_rq.get_queue('default')
|
queue = django_rq.get_queue('default')
|
||||||
queuelow = django_rq.get_queue('low')
|
queuelow = django_rq.get_queue('low')
|
||||||
@@ -859,7 +859,7 @@ def default(o):
|
|||||||
raise TypeError
|
raise TypeError
|
||||||
|
|
||||||
# Uploading workout
|
# Uploading workout
|
||||||
def workout_c2_upload(user,w):
|
def workout_c2_upload(user,w,async=False):
|
||||||
message = 'trying C2 upload'
|
message = 'trying C2 upload'
|
||||||
try:
|
try:
|
||||||
if mytypes.c2mapping[w.workouttype] is None:
|
if mytypes.c2mapping[w.workouttype] is None:
|
||||||
@@ -888,8 +888,10 @@ def workout_c2_upload(user,w):
|
|||||||
'Content-Type': 'application/json'}
|
'Content-Type': 'application/json'}
|
||||||
import urllib
|
import urllib
|
||||||
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
|
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
|
||||||
|
if not async:
|
||||||
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
|
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
|
||||||
|
|
||||||
|
|
||||||
if (response.status_code == 409 ):
|
if (response.status_code == 409 ):
|
||||||
message = "Concept2 Duplicate error"
|
message = "Concept2 Duplicate error"
|
||||||
w.uploadedtoc2 = -1
|
w.uploadedtoc2 = -1
|
||||||
@@ -905,7 +907,14 @@ def workout_c2_upload(user,w):
|
|||||||
else:
|
else:
|
||||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||||
c2id = 0
|
c2id = 0
|
||||||
|
else:
|
||||||
|
job = myqueue(queue,
|
||||||
|
handle_c2_sync,
|
||||||
|
w.id,
|
||||||
|
url,
|
||||||
|
headers,
|
||||||
|
json.dumps(data,default=default))
|
||||||
|
c2id = 0
|
||||||
|
|
||||||
return message,c2id
|
return message,c2id
|
||||||
|
|
||||||
|
|||||||
@@ -806,6 +806,20 @@ def paceformatsecs(values):
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def update_c2id_sql(id,c2id):
|
||||||
|
engine = create_engine(database_url, echo=False)
|
||||||
|
table = 'rowers_workout'
|
||||||
|
|
||||||
|
query = "UPDATE %s SET uploadedtoc2 = %s WHERE `id` = %s;" % (table,c2id,id)
|
||||||
|
|
||||||
|
with engine.connect() as conn, conn.begin():
|
||||||
|
result = conn.execute(query)
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
def fitnessmetric_to_sql(m,table='powertimefitnessmetric',debug=False):
|
def fitnessmetric_to_sql(m,table='powertimefitnessmetric',debug=False):
|
||||||
engine = create_engine(database_url, echo=False)
|
engine = create_engine(database_url, echo=False)
|
||||||
columns = ', '.join(m.keys())
|
columns = ', '.join(m.keys())
|
||||||
|
|||||||
@@ -741,6 +741,20 @@ def getsmallrowdata_db(columns,ids=[],debug=False):
|
|||||||
|
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
def update_c2id_sql(id,c2id):
|
||||||
|
engine = create_engine(database_url, echo=False)
|
||||||
|
table = 'rowers_workout'
|
||||||
|
|
||||||
|
query = "UPDATE %s SET uploadedtoc2 = %s WHERE `id` = %s;" % (table,c2id,id)
|
||||||
|
|
||||||
|
with engine.connect() as conn, conn.begin():
|
||||||
|
result = conn.execute(query)
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
def fitnessmetric_to_sql(m,table='powertimefitnessmetric',debug=False,
|
def fitnessmetric_to_sql(m,table='powertimefitnessmetric',debug=False,
|
||||||
doclean=False):
|
doclean=False):
|
||||||
# test if nan among values
|
# test if nan among values
|
||||||
|
|||||||
@@ -75,7 +75,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,
|
getsmallrowdata_db, updatecpdata_sql,update_c2id_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,
|
||||||
@@ -121,7 +121,18 @@ def add(x, y):
|
|||||||
return x + y
|
return x + y
|
||||||
|
|
||||||
|
|
||||||
|
@app.task
|
||||||
|
def handle_c2_sync(workoutid,url,headers,data,debug=True,**kwargs):
|
||||||
|
response = requests.post(url,headers=headers,data=data)
|
||||||
|
if response.status_code not in [200,201]:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
s = response.json()
|
||||||
|
c2id = s['data']['id']
|
||||||
|
|
||||||
|
res = update_c2id_sql(workoutid,c2id)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
def handle_c2_import_stroke_data(c2token,
|
def handle_c2_import_stroke_data(c2token,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import argparse
|
|||||||
import yamllint
|
import yamllint
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
from verbalexpressions import VerEx
|
from verbalexpressions import VerEx
|
||||||
|
|
||||||
@@ -524,10 +525,12 @@ def do_sync(w,options, quick=False):
|
|||||||
|
|
||||||
if ('upload_to_C2' in options and options['upload_to_C2']) or (w.user.c2_auto_export and ispromember(w.user.user)):
|
if ('upload_to_C2' in options and options['upload_to_C2']) or (w.user.c2_auto_export and ispromember(w.user.user)):
|
||||||
try:
|
try:
|
||||||
message,id = c2stuff.workout_c2_upload(w.user.user,w)
|
message,id = c2stuff.workout_c2_upload(w.user.user,w,async=True)
|
||||||
except NoTokenError:
|
except NoTokenError:
|
||||||
id = 0
|
id = 0
|
||||||
message = "Something went wrong with the Concept2 sync"
|
message = "Something went wrong with the Concept2 sync"
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
if ('upload_to_Strava' in options and upload_to_strava) or (w.user.strava_auto_export and ispromember(w.user.user)):
|
if ('upload_to_Strava' in options and upload_to_strava) or (w.user.strava_auto_export and ispromember(w.user.user)):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -4500,11 +4500,10 @@ def workout_upload_api(request):
|
|||||||
|
|
||||||
w = Workout.objects.get(id=id)
|
w = Workout.objects.get(id=id)
|
||||||
|
|
||||||
uploads.do_sync(w,post_data,quick=True)
|
|
||||||
|
|
||||||
if make_plot:
|
if make_plot:
|
||||||
res, jobid = uploads.make_plot(r,w,f1,f2,plottype,t)
|
res, jobid = uploads.make_plot(r,w,f1,f2,plottype,t)
|
||||||
|
|
||||||
|
uploads.do_sync(w,post_data,quick=True)
|
||||||
|
|
||||||
else: # form invalid
|
else: # form invalid
|
||||||
if fstr:
|
if fstr:
|
||||||
@@ -4529,7 +4528,6 @@ def workout_upload_api(request):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
email_sent = send_confirm(r.user, t, link, '')
|
email_sent = send_confirm(r.user, t, link, '')
|
||||||
|
|
||||||
return JSONResponse(status=statuscode,data=message)
|
return JSONResponse(status=statuscode,data=message)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user