imports no stroke data by creating synthetic workout
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
import oauth2 as oauth
|
import oauth2 as oauth
|
||||||
import cgi
|
import cgi
|
||||||
import requests
|
import requests
|
||||||
|
import arrow
|
||||||
import requests.auth
|
import requests.auth
|
||||||
import json
|
import json
|
||||||
import iso8601
|
import iso8601
|
||||||
@@ -33,9 +34,15 @@ import sys
|
|||||||
import urllib
|
import urllib
|
||||||
from requests import Request, Session
|
from requests import Request, Session
|
||||||
|
|
||||||
|
from utils import myqueue
|
||||||
|
|
||||||
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
|
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
|
||||||
|
import django_rq
|
||||||
|
queue = django_rq.get_queue('default')
|
||||||
|
queuelow = django_rq.get_queue('low')
|
||||||
|
queuehigh = django_rq.get_queue('low')
|
||||||
|
|
||||||
# Custom error class - to raise a NoTokenError
|
# Custom error class - to raise a NoTokenError
|
||||||
class C2NoTokenError(Exception):
|
class C2NoTokenError(Exception):
|
||||||
@@ -95,84 +102,76 @@ def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename):
|
|||||||
return custom_exception_handler(401,s)
|
return custom_exception_handler(401,s)
|
||||||
else:
|
else:
|
||||||
# ready to fetch. Hurray
|
# ready to fetch. Hurray
|
||||||
|
starttimeunix = arrow.get(startdatetime).timestamp
|
||||||
|
|
||||||
res = handle_c2_import_stroke_data(r.c2token,
|
job = myqueue(queue,
|
||||||
c2id,
|
handle_c2_import_stroke_data,
|
||||||
workoutid,
|
r.c2token,
|
||||||
startdatetime,
|
c2id,
|
||||||
csvfilename)
|
workoutid,
|
||||||
|
starttimeunix,
|
||||||
|
csvfilename)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
# get workout metrics, then relay stroke data to an asynchronous task
|
# get workout metrics, then relay stroke data to an asynchronous task
|
||||||
def create_async_workout(user,c2id):
|
def create_async_workout(alldata,user,c2id):
|
||||||
|
data = alldata[c2id]
|
||||||
|
splitdata = None
|
||||||
|
|
||||||
res = get_c2_workout(user,c2id)
|
distance = data['distance']
|
||||||
if (res.status_code == 200):
|
c2id = data['id']
|
||||||
data = res.json()['data']
|
workouttype = data['type']
|
||||||
splitdata = None
|
verified = data['verified']
|
||||||
if 'workout' in data:
|
startdatetime = iso8601.parse_date(data['date'])
|
||||||
if 'splits' in data['workout']:
|
weightclass = data['weight_class']
|
||||||
splitdata = data['workout']['splits']
|
weightcategory = 'hwt'
|
||||||
if 'intervals' in data['workout']:
|
if weightclass == "L":
|
||||||
splitdata = data['workout']['intervals']
|
weightcategory = 'lwt'
|
||||||
|
|
||||||
distance = data['distance']
|
# Create CSV file name and save data to CSV file
|
||||||
c2id = data['id']
|
csvfilename ='media/Import_'+str(c2id)+'.csv.gz'
|
||||||
workouttype = data['type']
|
|
||||||
verified = data['verified']
|
|
||||||
startdatetime = iso8601.parse_date(data['date'])
|
|
||||||
weightclass = data['weight_class']
|
|
||||||
weightcategory = 'hwt'
|
|
||||||
if weightclass == "L":
|
|
||||||
weightcategory = 'lwt'
|
|
||||||
|
|
||||||
# Create CSV file name and save data to CSV file
|
totaltime = data['time']/10.
|
||||||
csvfilename ='media/Import_'+str(c2id)+'.csv.gz'
|
duration = dataprep.totaltime_sec_to_string(totaltime)
|
||||||
|
|
||||||
totaltime = data['time']/10.
|
try:
|
||||||
duration = dataprep.totaltime_sec_to_string(totaltime)
|
timezone_str = tz(data['timezone'])
|
||||||
|
except:
|
||||||
|
timezone_str = 'UTC'
|
||||||
|
|
||||||
try:
|
workoutdate = startdatetime.astimezone(
|
||||||
timezone_str = tz(data['timezone'])
|
pytz.timezone(timezone_str)
|
||||||
except:
|
).strftime('%Y-%m-%d')
|
||||||
timezone_str = 'UTC'
|
starttime = startdatetime.astimezone(
|
||||||
|
pytz.timezone(timezone_str)
|
||||||
|
).strftime('%H:%M:%S')
|
||||||
|
|
||||||
workoutdate = startdatetime.astimezone(
|
r = Rower.objects.get(user=user)
|
||||||
pytz.timezone(timezone_str)
|
|
||||||
).strftime('%Y-%m-%d')
|
|
||||||
starttime = startdatetime.astimezone(
|
|
||||||
pytz.timezone(timezone_str)
|
|
||||||
).strftime('%H:%M:%S')
|
|
||||||
|
|
||||||
r = Rower.objects.get(user=user)
|
|
||||||
|
|
||||||
|
|
||||||
w = Workout(
|
w = Workout(
|
||||||
user=r,
|
user=r,
|
||||||
workouttype = workouttype,
|
workouttype = workouttype,
|
||||||
name = 'Imported workout',
|
name = 'Imported workout',
|
||||||
date = workoutdate,
|
date = workoutdate,
|
||||||
starttime = starttime,
|
starttime = starttime,
|
||||||
startdatetime = startdatetime,
|
startdatetime = startdatetime,
|
||||||
timezone = timezone_str,
|
timezone = timezone_str,
|
||||||
duration = duration,
|
duration = duration,
|
||||||
distance=distance,
|
distance=distance,
|
||||||
weightcategory = weightcategory,
|
weightcategory = weightcategory,
|
||||||
uploadedtoc2 = c2id,
|
uploadedtoc2 = c2id,
|
||||||
csvfilename = csvfilename,
|
csvfilename = csvfilename,
|
||||||
notes = 'imported from Concept2 log'
|
notes = 'imported from Concept2 log'
|
||||||
)
|
)
|
||||||
|
|
||||||
w.save()
|
w.save()
|
||||||
|
|
||||||
# Check if workout has stroke data, and get the stroke data
|
# Check if workout has stroke data, and get the stroke data
|
||||||
if data['stroke_data']:
|
|
||||||
result = add_stroke_data(user,c2id,w.id,startdatetime,csvfilename)
|
result = add_stroke_data(user,c2id,w.id,startdatetime,csvfilename)
|
||||||
else:
|
|
||||||
# create synthetic stroke data
|
|
||||||
pass
|
|
||||||
|
|
||||||
return w.id
|
return w.id
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ def add(x, y):
|
|||||||
@app.task
|
@app.task
|
||||||
def handle_c2_import_stroke_data(c2token,
|
def handle_c2_import_stroke_data(c2token,
|
||||||
c2id,workoutid,
|
c2id,workoutid,
|
||||||
startdatetime,
|
starttimeunix,
|
||||||
csvfilename,debug=True):
|
csvfilename,debug=True):
|
||||||
authorizationstring = str('Bearer ' + c2token)
|
authorizationstring = str('Bearer ' + c2token)
|
||||||
headers = {'Authorization': authorizationstring,
|
headers = {'Authorization': authorizationstring,
|
||||||
@@ -66,7 +66,6 @@ def handle_c2_import_stroke_data(c2token,
|
|||||||
s = requests.get(url,headers=headers)
|
s = requests.get(url,headers=headers)
|
||||||
if s.status_code == 200:
|
if s.status_code == 200:
|
||||||
strokedata = pd.DataFrame.from_dict(s.json()['data'])
|
strokedata = pd.DataFrame.from_dict(s.json()['data'])
|
||||||
starttimeunix = arrow.get(startdatetime).timestamp
|
|
||||||
result = add_c2_stroke_data_db(
|
result = add_c2_stroke_data_db(
|
||||||
strokedata,workoutid,starttimeunix,
|
strokedata,workoutid,starttimeunix,
|
||||||
csvfilename,debug=debug,
|
csvfilename,debug=debug,
|
||||||
|
|||||||
@@ -46,11 +46,7 @@
|
|||||||
{% for workout in workouts %}
|
{% for workout in workouts %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
{% if workout|lookup:'source' != 'Web' %}
|
|
||||||
<a href="/rowers/workout/c2import/{{ workout|lookup:'id' }}/">Import</a></td>
|
<a href="/rowers/workout/c2import/{{ workout|lookup:'id' }}/">Import</a></td>
|
||||||
{% else %}
|
|
||||||
|
|
||||||
{% endif %}
|
|
||||||
<td>{{ workout|lookup:'starttime' }}</td>
|
<td>{{ workout|lookup:'starttime' }}</td>
|
||||||
<td>{{ workout|lookup:'duration' }}</td>
|
<td>{{ workout|lookup:'duration' }}</td>
|
||||||
<td>{{ workout|lookup:'distance' }}</td>
|
<td>{{ workout|lookup:'distance' }}</td>
|
||||||
@@ -58,11 +54,8 @@
|
|||||||
<td>{{ workout|lookup:'source' }}</td>
|
<td>{{ workout|lookup:'source' }}</td>
|
||||||
<td>{{ workout|lookup:'comment' }}</td>
|
<td>{{ workout|lookup:'comment' }}</td>
|
||||||
<td>
|
<td>
|
||||||
{% if workout|lookup:'source' != 'Web' %}
|
|
||||||
{{ workout|lookup:'new' }}
|
{{ workout|lookup:'new' }}
|
||||||
{% else %}
|
</td>
|
||||||
|
|
||||||
{% endif %}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|||||||
@@ -3190,7 +3190,6 @@ def addmanual_view(request):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
print duration,'aap'
|
|
||||||
id,message = dataprep.create_row_df(r,
|
id,message = dataprep.create_row_df(r,
|
||||||
distance,
|
distance,
|
||||||
duration,startdatetime,
|
duration,startdatetime,
|
||||||
@@ -9110,12 +9109,18 @@ def workout_getc2workout_all(request,page=1,message=""):
|
|||||||
else:
|
else:
|
||||||
r = getrower(request.user)
|
r = getrower(request.user)
|
||||||
c2ids = [item['id'] for item in res.json()['data'] if item['source'] != 'Web']
|
c2ids = [item['id'] for item in res.json()['data'] if item['source'] != 'Web']
|
||||||
|
alldata = {}
|
||||||
|
for item in res.json()['data']:
|
||||||
|
alldata[item['id']] = item
|
||||||
|
|
||||||
knownc2ids = uniqify([
|
knownc2ids = uniqify([
|
||||||
w.uploadedtoc2 for w in Workout.objects.filter(user=r)
|
w.uploadedtoc2 for w in Workout.objects.filter(user=r)
|
||||||
])
|
])
|
||||||
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
|
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
|
||||||
|
|
||||||
for c2id in newids:
|
for c2id in newids:
|
||||||
workoutid = c2stuff.create_async_workout(request.user,c2id)
|
workoutid = c2stuff.create_async_workout(alldata,
|
||||||
|
request.user,c2id)
|
||||||
|
|
||||||
url = reverse(workouts_view)
|
url = reverse(workouts_view)
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
@@ -9366,9 +9371,51 @@ def workout_getc2workout_view(request,c2id):
|
|||||||
if data['stroke_data']:
|
if data['stroke_data']:
|
||||||
res2 = c2stuff.get_c2_workout_strokes(request.user,c2id)
|
res2 = c2stuff.get_c2_workout_strokes(request.user,c2id)
|
||||||
else:
|
else:
|
||||||
message = "This workout does not have any stroke data associated with it"
|
distance = data['distance']
|
||||||
messages.error(request,message)
|
c2id = data['id']
|
||||||
url = reverse(workout_c2import_view)
|
workouttype = data['type']
|
||||||
|
verified = data['verified']
|
||||||
|
startdatetime = iso8601.parse_date(data['date'])
|
||||||
|
weightclass = data['weight_class']
|
||||||
|
weightcategory = 'hwt'
|
||||||
|
if weightclass == "L":
|
||||||
|
weightcategory = 'lwt'
|
||||||
|
totaltime = data['time']/10.
|
||||||
|
duration = dataprep.totaltime_sec_to_string(totaltime)
|
||||||
|
duration = datetime.datetime.strptime(duration,'%H:%M:%S.%f').time()
|
||||||
|
|
||||||
|
try:
|
||||||
|
timezone_str = tz(data['timezone'])
|
||||||
|
except:
|
||||||
|
timezone_str = 'UTC'
|
||||||
|
|
||||||
|
workoutdate = startdatetime.astimezone(
|
||||||
|
pytz.timezone(timezone_str)
|
||||||
|
).strftime('%Y-%m-%d')
|
||||||
|
starttime = startdatetime.astimezone(
|
||||||
|
pytz.timezone(timezone_str)
|
||||||
|
).strftime('%H:%M:%S')
|
||||||
|
|
||||||
|
r = getrower(request.user)
|
||||||
|
|
||||||
|
id, message = dataprep.create_row_df(r,
|
||||||
|
distance,
|
||||||
|
duration,
|
||||||
|
startdatetime,
|
||||||
|
title = 'Imported from C2',
|
||||||
|
workouttype=workouttype)
|
||||||
|
|
||||||
|
w = Workout.objects.get(id=id)
|
||||||
|
w.uploadedtoc2 = c2id
|
||||||
|
w.save()
|
||||||
|
|
||||||
|
message = "This workout does not have any stroke data associated with it. We created synthetic stroke data."
|
||||||
|
messages.info(request,message)
|
||||||
|
url = reverse(r.defaultlandingpage,
|
||||||
|
kwargs = {
|
||||||
|
'id':int(id),
|
||||||
|
})
|
||||||
|
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
# We have stroke data
|
# We have stroke data
|
||||||
|
|||||||
Reference in New Issue
Block a user