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
|
||||
)
|
||||
|
||||
from rowers.tasks import handle_c2_import_stroke_data
|
||||
from rowers.tasks import handle_c2_import_stroke_data, handle_c2_sync
|
||||
import django_rq
|
||||
queue = django_rq.get_queue('default')
|
||||
queuelow = django_rq.get_queue('low')
|
||||
@@ -859,7 +859,7 @@ def default(o):
|
||||
raise TypeError
|
||||
|
||||
# Uploading workout
|
||||
def workout_c2_upload(user,w):
|
||||
def workout_c2_upload(user,w,async=False):
|
||||
message = 'trying C2 upload'
|
||||
try:
|
||||
if mytypes.c2mapping[w.workouttype] is None:
|
||||
@@ -888,25 +888,34 @@ def workout_c2_upload(user,w):
|
||||
'Content-Type': 'application/json'}
|
||||
import urllib
|
||||
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
|
||||
if not async:
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
|
||||
|
||||
if (response.status_code == 409 ):
|
||||
message = "Concept2 Duplicate error"
|
||||
w.uploadedtoc2 = -1
|
||||
c2id = -1
|
||||
w.save()
|
||||
elif (response.status_code == 201 or response.status_code == 200):
|
||||
# s= json.loads(response.text)
|
||||
s = response.json()
|
||||
c2id = s['data']['id']
|
||||
w.uploadedtoc2 = c2id
|
||||
w.save()
|
||||
message = "Upload to Concept2 was successful"
|
||||
|
||||
if (response.status_code == 409 ):
|
||||
message = "Concept2 Duplicate error"
|
||||
w.uploadedtoc2 = -1
|
||||
c2id = -1
|
||||
w.save()
|
||||
elif (response.status_code == 201 or response.status_code == 200):
|
||||
# s= json.loads(response.text)
|
||||
s = response.json()
|
||||
c2id = s['data']['id']
|
||||
w.uploadedtoc2 = c2id
|
||||
w.save()
|
||||
message = "Upload to Concept2 was successful"
|
||||
else:
|
||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||
c2id = 0
|
||||
else:
|
||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||
job = myqueue(queue,
|
||||
handle_c2_sync,
|
||||
w.id,
|
||||
url,
|
||||
headers,
|
||||
json.dumps(data,default=default))
|
||||
c2id = 0
|
||||
|
||||
|
||||
return message,c2id
|
||||
|
||||
# This is token refresh. Looks for tokens in our database, then refreshes
|
||||
|
||||
@@ -806,6 +806,20 @@ def paceformatsecs(values):
|
||||
|
||||
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):
|
||||
engine = create_engine(database_url, echo=False)
|
||||
columns = ', '.join(m.keys())
|
||||
@@ -1087,7 +1101,7 @@ def save_workout_database(f2, r, dosmooth=True, workouttype='rower',
|
||||
message = None
|
||||
if title is None:
|
||||
title = 'Workout'
|
||||
|
||||
|
||||
powerperc = 100 * np.array([r.pw_ut2,
|
||||
r.pw_ut1,
|
||||
r.pw_at,
|
||||
|
||||
@@ -741,6 +741,20 @@ def getsmallrowdata_db(columns,ids=[],debug=False):
|
||||
|
||||
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,
|
||||
doclean=False):
|
||||
# test if nan among values
|
||||
|
||||
@@ -75,7 +75,7 @@ def strip_tags(html):
|
||||
|
||||
from rowers.dataprepnodjango import (
|
||||
update_strokedata, new_workout_from_file,
|
||||
getsmallrowdata_db, updatecpdata_sql,
|
||||
getsmallrowdata_db, updatecpdata_sql,update_c2id_sql,
|
||||
update_agegroup_db,fitnessmetric_to_sql,
|
||||
add_c2_stroke_data_db,totaltime_sec_to_string,
|
||||
create_c2_stroke_data_db,update_empower,
|
||||
@@ -121,7 +121,18 @@ def add(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
|
||||
def handle_c2_import_stroke_data(c2token,
|
||||
|
||||
@@ -24,6 +24,7 @@ import argparse
|
||||
import yamllint
|
||||
from subprocess import call
|
||||
import re
|
||||
import sys
|
||||
|
||||
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)):
|
||||
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:
|
||||
id = 0
|
||||
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)):
|
||||
try:
|
||||
|
||||
@@ -4500,11 +4500,10 @@ def workout_upload_api(request):
|
||||
|
||||
w = Workout.objects.get(id=id)
|
||||
|
||||
uploads.do_sync(w,post_data,quick=True)
|
||||
|
||||
if make_plot:
|
||||
res, jobid = uploads.make_plot(r,w,f1,f2,plottype,t)
|
||||
|
||||
uploads.do_sync(w,post_data,quick=True)
|
||||
|
||||
else: # form invalid
|
||||
if fstr:
|
||||
@@ -4529,7 +4528,6 @@ def workout_upload_api(request):
|
||||
}
|
||||
)
|
||||
email_sent = send_confirm(r.user, t, link, '')
|
||||
|
||||
return JSONResponse(status=statuscode,data=message)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user