imports no stroke data by creating synthetic workout
This commit is contained in:
@@ -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):
|
||||
|
||||
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']
|
||||
def create_async_workout(alldata,user,c2id):
|
||||
data = alldata[c2id]
|
||||
splitdata = None
|
||||
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user