Private
Public Access
1
0

imports no stroke data by creating synthetic workout

This commit is contained in:
Sander Roosendaal
2018-01-19 16:54:55 +01:00
parent 0d1be41e6e
commit 45703f4946
4 changed files with 115 additions and 77 deletions

View File

@@ -7,6 +7,7 @@
import oauth2 as oauth
import cgi
import requests
import arrow
import requests.auth
import json
import iso8601
@@ -33,9 +34,15 @@ import sys
import urllib
from requests import Request, Session
from utils import myqueue
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
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
class C2NoTokenError(Exception):
@@ -95,84 +102,76 @@ def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename):
return custom_exception_handler(401,s)
else:
# ready to fetch. Hurray
starttimeunix = arrow.get(startdatetime).timestamp
res = handle_c2_import_stroke_data(r.c2token,
c2id,
workoutid,
startdatetime,
csvfilename)
job = myqueue(queue,
handle_c2_import_stroke_data,
r.c2token,
c2id,
workoutid,
starttimeunix,
csvfilename)
return 1
# 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)
if (res.status_code == 200):
data = res.json()['data']
splitdata = None
if 'workout' in data:
if 'splits' in data['workout']:
splitdata = data['workout']['splits']
if 'intervals' in data['workout']:
splitdata = data['workout']['intervals']
distance = data['distance']
c2id = data['id']
workouttype = data['type']
verified = data['verified']
startdatetime = iso8601.parse_date(data['date'])
weightclass = data['weight_class']
weightcategory = 'hwt'
if weightclass == "L":
weightcategory = 'lwt'
distance = data['distance']
c2id = data['id']
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
csvfilename ='media/Import_'+str(c2id)+'.csv.gz'
# Create CSV file name and save data to CSV file
csvfilename ='media/Import_'+str(c2id)+'.csv.gz'
totaltime = data['time']/10.
duration = dataprep.totaltime_sec_to_string(totaltime)
totaltime = data['time']/10.
duration = dataprep.totaltime_sec_to_string(totaltime)
try:
timezone_str = tz(data['timezone'])
except:
timezone_str = 'UTC'
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')
workoutdate = startdatetime.astimezone(
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)
r = Rower.objects.get(user=user)
w = Workout(
user=r,
workouttype = workouttype,
name = 'Imported workout',
date = workoutdate,
starttime = starttime,
startdatetime = startdatetime,
timezone = timezone_str,
duration = duration,
distance=distance,
weightcategory = weightcategory,
uploadedtoc2 = c2id,
csvfilename = csvfilename,
notes = 'imported from Concept2 log'
)
w = Workout(
user=r,
workouttype = workouttype,
name = 'Imported workout',
date = workoutdate,
starttime = starttime,
startdatetime = startdatetime,
timezone = timezone_str,
duration = duration,
distance=distance,
weightcategory = weightcategory,
uploadedtoc2 = c2id,
csvfilename = csvfilename,
notes = 'imported from Concept2 log'
)
w.save()
w.save()
# 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)
else:
# create synthetic stroke data
pass
# Check if workout has stroke data, and get the stroke data
result = add_stroke_data(user,c2id,w.id,startdatetime,csvfilename)
return w.id

View File

@@ -56,7 +56,7 @@ def add(x, y):
@app.task
def handle_c2_import_stroke_data(c2token,
c2id,workoutid,
startdatetime,
starttimeunix,
csvfilename,debug=True):
authorizationstring = str('Bearer ' + c2token)
headers = {'Authorization': authorizationstring,
@@ -66,7 +66,6 @@ def handle_c2_import_stroke_data(c2token,
s = requests.get(url,headers=headers)
if s.status_code == 200:
strokedata = pd.DataFrame.from_dict(s.json()['data'])
starttimeunix = arrow.get(startdatetime).timestamp
result = add_c2_stroke_data_db(
strokedata,workoutid,starttimeunix,
csvfilename,debug=debug,

View File

@@ -46,11 +46,7 @@
{% for workout in workouts %}
<tr>
<td>
{% if workout|lookup:'source' != 'Web' %}
<a href="/rowers/workout/c2import/{{ workout|lookup:'id' }}/">Import</a></td>
{% else %}
&nbsp;
{% endif %}
<td>{{ workout|lookup:'starttime' }}</td>
<td>{{ workout|lookup:'duration' }}</td>
<td>{{ workout|lookup:'distance' }}</td>
@@ -58,11 +54,8 @@
<td>{{ workout|lookup:'source' }}</td>
<td>{{ workout|lookup:'comment' }}</td>
<td>
{% if workout|lookup:'source' != 'Web' %}
{{ workout|lookup:'new' }}
{% else %}
&nbsp;
{% endif %}</td>
</td>
</tr>
{% endfor %}

View File

@@ -3190,7 +3190,6 @@ def addmanual_view(request):
)
print duration,'aap'
id,message = dataprep.create_row_df(r,
distance,
duration,startdatetime,
@@ -9110,12 +9109,18 @@ def workout_getc2workout_all(request,page=1,message=""):
else:
r = getrower(request.user)
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([
w.uploadedtoc2 for w in Workout.objects.filter(user=r)
])
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
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)
return HttpResponseRedirect(url)
@@ -9366,9 +9371,51 @@ def workout_getc2workout_view(request,c2id):
if data['stroke_data']:
res2 = c2stuff.get_c2_workout_strokes(request.user,c2id)
else:
message = "This workout does not have any stroke data associated with it"
messages.error(request,message)
url = reverse(workout_c2import_view)
distance = data['distance']
c2id = data['id']
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)
# We have stroke data