c2stuff.create_async_workout start
works but is not async yet
This commit is contained in:
@@ -9,6 +9,7 @@ import cgi
|
||||
import requests
|
||||
import requests.auth
|
||||
import json
|
||||
import iso8601
|
||||
from django.utils import timezone
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
@@ -21,8 +22,8 @@ from django.conf import settings
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
|
||||
import dataprep
|
||||
import pytz
|
||||
from rowingdata import rowingdata
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
@@ -34,6 +35,8 @@ from requests import Request, Session
|
||||
|
||||
from rowsandall_app.settings import C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET
|
||||
|
||||
from rowers.tasks import handle_c2_import_stroke_data
|
||||
|
||||
# Custom error class - to raise a NoTokenError
|
||||
class C2NoTokenError(Exception):
|
||||
def __init__(self,value):
|
||||
@@ -82,7 +85,96 @@ def c2_open(user):
|
||||
|
||||
return thetoken
|
||||
|
||||
def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.c2token == '') or (r.c2token is None):
|
||||
return custom_exception_handler(401,s)
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
elif (timezone.now()>r.tokenexpirydate):
|
||||
s = "Token expired. Needs to refresh."
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
# ready to fetch. Hurray
|
||||
|
||||
res = handle_c2_import_stroke_data(r.c2token,
|
||||
c2id,
|
||||
workoutid,
|
||||
startdatetime,
|
||||
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']
|
||||
|
||||
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'
|
||||
|
||||
totaltime = data['time']/10.
|
||||
duration = dataprep.totaltime_sec_to_string(totaltime)
|
||||
|
||||
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 = 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.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
|
||||
|
||||
return w.id
|
||||
|
||||
# convert datetime object to seconds
|
||||
def makeseconds(t):
|
||||
|
||||
Reference in New Issue
Block a user