coverage stravastuff stuff
This commit is contained in:
@@ -19,6 +19,7 @@ from iso8601 import ParseError
|
||||
|
||||
import numpy
|
||||
import json
|
||||
from scipy import optimize
|
||||
from json.decoder import JSONDecodeError
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
@@ -41,7 +42,7 @@ from django.core.exceptions import PermissionDenied
|
||||
def getagegrouprecord(age,sex='male',weightcategory='hwt',
|
||||
distance=2000,duration=None,indf=pd.DataFrame()):
|
||||
|
||||
if not indf.empty:
|
||||
if not indf.empty: # pragma: no cover
|
||||
if not duration:
|
||||
df = indf[indf['distance'] == distance]
|
||||
else:
|
||||
@@ -83,7 +84,7 @@ def getagegrouprecord(age,sex='male',weightcategory='hwt',
|
||||
try:
|
||||
p1, success = optimize.leastsq(errfunc,p0[:],
|
||||
args = (ages,powers))
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
p1 = p0
|
||||
success = 0
|
||||
|
||||
@@ -93,7 +94,7 @@ def getagegrouprecord(age,sex='male',weightcategory='hwt',
|
||||
#power = np.polyval(poly_coefficients,age)
|
||||
|
||||
power = 0.5*(np.abs(power)+power)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
power = 0
|
||||
else:
|
||||
power = 0
|
||||
@@ -126,48 +127,26 @@ def c2_open(user):
|
||||
else:
|
||||
if (timezone.now()>r.tokenexpirydate):
|
||||
res = rower_c2_token_refresh(user)
|
||||
if res == None:
|
||||
if res == None: # pragma: no cover
|
||||
raise NoTokenError("User has no token")
|
||||
if res[0] != None:
|
||||
thetoken = res[0]
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
raise NoTokenError("User has no token")
|
||||
else:
|
||||
thetoken = r.c2token
|
||||
|
||||
return thetoken
|
||||
|
||||
def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename,
|
||||
workouttype='rower'):
|
||||
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:
|
||||
starttimeunix = arrow.get(startdatetime).timestamp()
|
||||
|
||||
job = myqueue(queue,
|
||||
handle_c2_import_stroke_data,
|
||||
r.c2token,
|
||||
c2id,
|
||||
workoutid,
|
||||
starttimeunix,
|
||||
csvfilename,workouttype=workouttype)
|
||||
|
||||
return 1
|
||||
|
||||
def get_c2_workouts(rower,do_async=True):
|
||||
try:
|
||||
thetoken = c2_open(rower.user)
|
||||
except NoTokenError:
|
||||
except NoTokenError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
res = get_c2_workout_list(rower.user,page=1)
|
||||
|
||||
if (res.status_code != 200):
|
||||
if (res.status_code != 200): # pragma: no cover
|
||||
return 0
|
||||
else:
|
||||
c2ids = [item['id'] for item in res.json()['data']]
|
||||
@@ -189,13 +168,15 @@ def get_c2_workouts(rower,do_async=True):
|
||||
with open('c2blocked.json','r') as c2blocked:
|
||||
jsondata = json.load(c2blocked)
|
||||
parkedids = jsondata['ids']
|
||||
except FileNotFoundError:
|
||||
except FileNotFoundError: # pragma: no cover
|
||||
pass
|
||||
|
||||
|
||||
knownc2ids = uniqify(knownc2ids+tombstones+parkedids)
|
||||
|
||||
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
|
||||
if settings.TESTING:
|
||||
newids = c2ids
|
||||
|
||||
newparkedids = uniqify(newids+parkedids)
|
||||
|
||||
@@ -205,7 +186,7 @@ def get_c2_workouts(rower,do_async=True):
|
||||
|
||||
counter = 0
|
||||
for c2id in newids:
|
||||
if do_async:
|
||||
if do_async: # pragma: no cover
|
||||
res = myqueue(queuehigh,
|
||||
handle_c2_async_workout,
|
||||
alldata,
|
||||
@@ -285,10 +266,10 @@ def create_async_workout(alldata,user,c2id):
|
||||
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
|
||||
try:
|
||||
s = requests.get(url,headers=headers)
|
||||
except ConnectionError:
|
||||
except ConnectionError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
if s.status_code != 200:
|
||||
if s.status_code != 200: # pragma: no cover
|
||||
return 0
|
||||
|
||||
strokedata = pd.DataFrame.from_dict(s.json()['data'])
|
||||
@@ -306,7 +287,7 @@ def create_async_workout(alldata,user,c2id):
|
||||
|
||||
nr_rows = len(unixtime)
|
||||
|
||||
try:
|
||||
try: # pragma: no cover
|
||||
latcoord = strokedata.loc[:,'lat']
|
||||
loncoord = strokedata.loc[:,'lon']
|
||||
except:
|
||||
@@ -323,12 +304,12 @@ def create_async_workout(alldata,user,c2id):
|
||||
|
||||
try:
|
||||
spm = strokedata.loc[:,'spm']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
spm = 0*dist2
|
||||
|
||||
try:
|
||||
hr = strokedata.loc[:,'hr']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
hr = 0*spm
|
||||
|
||||
pace = strokedata.loc[:,'p']/10.
|
||||
@@ -337,7 +318,7 @@ def create_async_workout(alldata,user,c2id):
|
||||
|
||||
velo = 500./pace
|
||||
power = 2.8*velo**3
|
||||
if workouttype == 'bike':
|
||||
if workouttype == 'bike': # pragma: no cover
|
||||
velo = 1000./pace
|
||||
|
||||
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
|
||||
@@ -385,12 +366,12 @@ def create_async_workout(alldata,user,c2id):
|
||||
|
||||
response = session.post(UPLOAD_SERVICE_URL,json=uploadoptions)
|
||||
|
||||
if response.status_code != 200:
|
||||
if response.status_code != 200: # pragma: no cover
|
||||
return 0
|
||||
|
||||
try:
|
||||
workoutid = response.json()['id']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
workoutid = 1
|
||||
|
||||
newc2id = Workout.objects.get(id=workoutid).uploadedtoc2
|
||||
@@ -407,7 +388,7 @@ def create_async_workout(alldata,user,c2id):
|
||||
json.dump(data,c2blocked)
|
||||
|
||||
# summary
|
||||
if 'workout' in data:
|
||||
if 'workout' in data: # pragma: no cover
|
||||
if 'splits' in data['workout']:
|
||||
splitdata = data['workout']['splits']
|
||||
elif 'intervals' in data['workout']:
|
||||
@@ -417,7 +398,7 @@ def create_async_workout(alldata,user,c2id):
|
||||
else:
|
||||
splitdata = False
|
||||
|
||||
if splitdata:
|
||||
if splitdata: # pragma: no cover
|
||||
summary,sa,results = c2stuff.summaryfromsplitdata(splitdata,data,csvfilename,workouttype=workouttype)
|
||||
w = Workout.objects.get(id=workoutid)
|
||||
w.summary = summary
|
||||
@@ -448,7 +429,7 @@ def makeseconds(t):
|
||||
|
||||
# convert our weight class code to Concept2 weight class code
|
||||
def c2wc(weightclass):
|
||||
if (weightclass=="lwt"):
|
||||
if (weightclass=="lwt"): # pragma: no cover
|
||||
res = "L"
|
||||
else:
|
||||
res = "H"
|
||||
@@ -465,38 +446,38 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
totaltime = data['time']/10.
|
||||
try:
|
||||
spm = data['stroke_rate']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
spm = 0
|
||||
try:
|
||||
resttime = data['rest_time']/10.
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
resttime = 0
|
||||
try:
|
||||
restdistance = data['rest_distance']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
restdistance = 0
|
||||
try:
|
||||
avghr = data['heart_rate']['average']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
avghr = 0
|
||||
try:
|
||||
maxhr = data['heart_rate']['max']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
maxhr = 0
|
||||
|
||||
try:
|
||||
avgpace = 500.*totaltime/totaldist
|
||||
except (ZeroDivisionError,OverflowError):
|
||||
except (ZeroDivisionError,OverflowError): # pragma: no cover
|
||||
avgpace = 0.
|
||||
|
||||
try:
|
||||
restpace = 500.*resttime/restdistance
|
||||
except (ZeroDivisionError,OverflowError):
|
||||
except (ZeroDivisionError,OverflowError): # pragma: no cover
|
||||
restpace = 0.
|
||||
|
||||
velo = totaldist/totaltime
|
||||
avgpower = 2.8*velo**(3.0)
|
||||
if workouttype in ['bike','bikeerg']:
|
||||
if workouttype in ['bike','bikeerg']: # pragma: no cover
|
||||
velo = velo/2.
|
||||
avgpower = 2.8*velo**(3.0)
|
||||
velo = velo*2
|
||||
@@ -504,18 +485,18 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
|
||||
try:
|
||||
restvelo = restdistance/resttime
|
||||
except (ZeroDivisionError,OverflowError):
|
||||
except (ZeroDivisionError,OverflowError): # pragma: no cover
|
||||
restvelo = 0
|
||||
|
||||
restpower = 2.8*restvelo**(3.0)
|
||||
if workouttype in ['bike','bikeerg']:
|
||||
if workouttype in ['bike','bikeerg']: # pragma: no cover
|
||||
restvelo = restvelo/2.
|
||||
restpower = 2.8*restvelo**(3.0)
|
||||
restvelo = restvelo*2
|
||||
|
||||
try:
|
||||
avgdps = totaldist/data['stroke_count']
|
||||
except (ZeroDivisionError,OverflowError,KeyError):
|
||||
except (ZeroDivisionError,OverflowError,KeyError): # pragma: no cover
|
||||
avgdps = 0
|
||||
|
||||
from rowingdata import summarystring,workstring,interval_string
|
||||
@@ -543,45 +524,45 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
|
||||
try:
|
||||
timebased = data['workout_type'] in ['FixedTimeSplits','FixedTimeInterval']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
timebased = False
|
||||
|
||||
for interval in splitdata:
|
||||
try:
|
||||
idist = interval['distance']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
idist = 0
|
||||
|
||||
try:
|
||||
itime = interval['time']/10.
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
itime = 0
|
||||
try:
|
||||
ipace = 500.*itime/idist
|
||||
except (ZeroDivisionError,OverflowError):
|
||||
except (ZeroDivisionError,OverflowError): # pragma: no cover
|
||||
ipace = 180.
|
||||
|
||||
try:
|
||||
ispm = interval['stroke_rate']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
ispm = 0
|
||||
try:
|
||||
irest_time = interval['rest_time']/10.
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
irest_time = 0
|
||||
try:
|
||||
iavghr = interval['heart_rate']['average']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
iavghr = 0
|
||||
try:
|
||||
imaxhr = interval['heart_rate']['average']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
imaxhr = 0
|
||||
|
||||
# create interval values
|
||||
iarr = [idist,'meters','work']
|
||||
resarr = [itime]
|
||||
if timebased:
|
||||
if timebased: # pragma: no cover
|
||||
iarr = [itime,'seconds','work']
|
||||
resarr = [idist]
|
||||
|
||||
@@ -589,7 +570,7 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
iarr += [irest_time,'seconds','rest']
|
||||
try:
|
||||
resarr += [interval['rest_distance']]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
resarr += [np.nan]
|
||||
|
||||
sa += iarr
|
||||
@@ -598,9 +579,9 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
if itime != 0:
|
||||
ivelo = idist/itime
|
||||
ipower = 2.8*ivelo**(3.0)
|
||||
if workouttype in ['bike','bikeerg']:
|
||||
if workouttype in ['bike','bikeerg']: # pragma: no cover
|
||||
ipower = 2.8*(ivelo/2.)**(3.0)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
ivelo = 0
|
||||
ipower = 0
|
||||
|
||||
@@ -610,70 +591,6 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
|
||||
|
||||
return sums,sa,results
|
||||
|
||||
# Not used now. Could be used to add workout split data to Concept2
|
||||
# logbook but needs to be reviewed.
|
||||
def createc2workoutdata_as_splits(w):
|
||||
filename = w.csvfilename
|
||||
row = rowingdata(csvfile=filename)
|
||||
|
||||
# resize per minute
|
||||
df = row.df.groupby(lambda x:x/60).mean()
|
||||
|
||||
averagehr = int(df[' HRCur (bpm)'].mean())
|
||||
maxhr = int(df[' HRCur (bpm)'].max())
|
||||
|
||||
# adding diff, trying to see if this is valid
|
||||
t = 10*df.loc[:,' ElapsedTime (sec)'].diff().values
|
||||
t[0] = t[1]
|
||||
d = df.loc[:,' Horizontal (meters)'].diff().values
|
||||
d[0] = d[1]
|
||||
p = 10*df.loc[:,' Stroke500mPace (sec/500m)'].values
|
||||
t = t.astype(int)
|
||||
d = d.astype(int)
|
||||
p = p.astype(int)
|
||||
spm = df[' Cadence (stokes/min)'].astype(int)
|
||||
spm[0] = spm[1]
|
||||
hr = df[' HRCur (bpm)'].astype(int)
|
||||
split_data = []
|
||||
for i in range(len(t)):
|
||||
thisrecord = {"time":t[i],"distance":d[i],"stroke_rate":spm[i],
|
||||
"heart_rate":{
|
||||
"average:":hr[i]
|
||||
}
|
||||
}
|
||||
split_data.append(thisrecord)
|
||||
|
||||
try:
|
||||
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S.%f")
|
||||
except ValueError:
|
||||
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
|
||||
|
||||
try:
|
||||
newnotes = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
|
||||
except TypeError:
|
||||
newnotes = 'from '+w.workoutsource+' via rowsandall.com'
|
||||
|
||||
wtype = w.workouttype
|
||||
if wtype in otwtypes:
|
||||
wtype = 'water'
|
||||
|
||||
data = {
|
||||
"type": wtype,
|
||||
"date": w.startdatetime.isoformat(),
|
||||
"distance": int(w.distance),
|
||||
"time": int(10*makeseconds(durationstr)),
|
||||
"timezone": w.timezone,
|
||||
"weight_class": c2wc(w.weightcategory),
|
||||
"comments": newnotes,
|
||||
"heart_rate": {
|
||||
"average": averagehr,
|
||||
"max": maxhr,
|
||||
},
|
||||
"splits": split_data,
|
||||
}
|
||||
|
||||
|
||||
return data
|
||||
|
||||
# Create the Data object for the stroke data to be sent to Concept2 logbook
|
||||
# API
|
||||
@@ -681,13 +598,13 @@ def createc2workoutdata(w):
|
||||
filename = w.csvfilename
|
||||
try:
|
||||
row = rowingdata(csvfile=filename)
|
||||
except IOError:
|
||||
except IOError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
try:
|
||||
averagehr = int(row.df[' HRCur (bpm)'].mean())
|
||||
maxhr = int(row.df[' HRCur (bpm)'].max())
|
||||
except ValueError:
|
||||
except ValueError: # pragma: no cover
|
||||
averagehr = 0
|
||||
maxhr = 0
|
||||
|
||||
@@ -721,18 +638,18 @@ def createc2workoutdata(w):
|
||||
t = 10*row.df.loc[:,'TimeStamp (sec)'].values-10*row.df.loc[:,'TimeStamp (sec)'].iloc[0]
|
||||
try:
|
||||
t[0] = t[1]
|
||||
except IndexError:
|
||||
except IndexError: # pragma: no cover
|
||||
pass
|
||||
|
||||
d = 10*row.df.loc[:,' Horizontal (meters)'].values
|
||||
try:
|
||||
d[0] = d[1]
|
||||
except IndexError:
|
||||
except IndexError: # pragma: no cover
|
||||
pass
|
||||
|
||||
p = abs(10*row.df.loc[:,' Stroke500mPace (sec/500m)'].values)
|
||||
p = np.clip(p,0,3600)
|
||||
if w.workouttype == 'bike':
|
||||
if w.workouttype == 'bike': # pragma: no cover
|
||||
p = 2.0*p
|
||||
|
||||
t = t.astype(int)
|
||||
@@ -742,11 +659,11 @@ def createc2workoutdata(w):
|
||||
|
||||
try:
|
||||
spm[0] = spm[1]
|
||||
except (KeyError,IndexError):
|
||||
except (KeyError,IndexError): # pragma: no cover
|
||||
spm = 0*t
|
||||
try:
|
||||
hr = row.df[' HRCur (bpm)'].astype(int)
|
||||
except ValueError:
|
||||
except ValueError: # pragma: no cover
|
||||
hr = 0*d
|
||||
stroke_data = []
|
||||
|
||||
@@ -822,14 +739,14 @@ def do_refresh_token(refreshtoken):
|
||||
|
||||
try:
|
||||
token_json = response.json()
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
return [None,None,None]
|
||||
|
||||
try:
|
||||
thetoken = token_json['access_token']
|
||||
expires_in = token_json['expires_in']
|
||||
refresh_token = token_json['refresh_token']
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
with open("media/c2errors.log","a") as errorlog:
|
||||
errorstring = str(sys.exc_info()[0])
|
||||
timestr = time.strftime("%Y%m%d-%H%M%S")
|
||||
@@ -868,25 +785,25 @@ def get_token(code):
|
||||
try:
|
||||
status_code = response.status_code
|
||||
# status_code = token_json['status_code']
|
||||
except AttributeError:
|
||||
except AttributeError: # pragma: no cover
|
||||
# except KeyError:
|
||||
return (0,response.text)
|
||||
try:
|
||||
status_code = token_json.status_code
|
||||
except AttributeError:
|
||||
except AttributeError: # pragma: no cover
|
||||
return (0,'Attribute Error on c2_get_token')
|
||||
|
||||
if status_code == 200:
|
||||
thetoken = token_json['access_token']
|
||||
expires_in = token_json['expires_in']
|
||||
refresh_token = token_json['refresh_token']
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
return (0,token_json['message'])
|
||||
|
||||
return (thetoken,expires_in,refresh_token,messg)
|
||||
|
||||
# Make URL for authorization and load it
|
||||
def make_authorization_url(request):
|
||||
def make_authorization_url(request): # pragma: no cover
|
||||
# Generate a random string for the state parameter
|
||||
# Save it for use later to prevent xsrf attacks
|
||||
from uuid import uuid4
|
||||
@@ -904,7 +821,7 @@ def make_authorization_url(request):
|
||||
# Get workout from C2 ID
|
||||
def get_workout(user,c2id,do_async=False):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.c2token == '') or (r.c2token is None):
|
||||
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s) ,0
|
||||
elif (timezone.now()>r.tokenexpirydate):
|
||||
@@ -919,7 +836,7 @@ def get_workout(user,c2id,do_async=False):
|
||||
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)
|
||||
s = requests.get(url,headers=headers)
|
||||
|
||||
if s.status_code != 200:
|
||||
if s.status_code != 200: # pragma: no cover
|
||||
if s.status_code == 404:
|
||||
raise PermissionDenied("You have no access to this resource")
|
||||
else:
|
||||
@@ -930,11 +847,11 @@ def get_workout(user,c2id,do_async=False):
|
||||
splitdata = None
|
||||
|
||||
if 'workout' in data:
|
||||
if 'splits' in data['workout']:
|
||||
if 'splits' in data['workout']: # pragma: no cover
|
||||
splitdata = data['workout']['splits']
|
||||
elif 'intervals' in data['workout']:
|
||||
elif 'intervals' in data['workout']: # pragma: no cover
|
||||
splitdata = data['workout']['intervals']
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
splitdata = None
|
||||
|
||||
# Check if workout has stroke data, and get the stroke data
|
||||
@@ -943,9 +860,9 @@ def get_workout(user,c2id,do_async=False):
|
||||
res2 = get_c2_workout_strokes(user,c2id)
|
||||
if res2.status_code == 200:
|
||||
strokedata = pd.DataFrame.from_dict(res2.json()['data'])
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
strokedata = pd.DataFrame()
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
strokedata = pd.DataFrame()
|
||||
|
||||
return data,strokedata
|
||||
@@ -953,10 +870,10 @@ def get_workout(user,c2id,do_async=False):
|
||||
# Get stroke data belonging to C2 ID
|
||||
def get_c2_workout_strokes(user,c2id):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.c2token == '') or (r.c2token is None):
|
||||
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
|
||||
return custom_exception_handler(401,s)
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
elif (timezone.now()>r.tokenexpirydate):
|
||||
elif (timezone.now()>r.tokenexpirydate): # pragma: no cover
|
||||
s = "Token expired. Needs to refresh."
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
@@ -974,10 +891,10 @@ def get_c2_workout_strokes(user,c2id):
|
||||
# assuming that users don't want to import their old workouts
|
||||
def get_c2_workout_list(user,page=1):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.c2token == '') or (r.c2token is None):
|
||||
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (timezone.now()>r.tokenexpirydate):
|
||||
elif (timezone.now()>r.tokenexpirydate): # pragma: no cover
|
||||
s = "Token expired. Needs to refresh."
|
||||
|
||||
return custom_exception_handler(401,s)
|
||||
@@ -997,7 +914,7 @@ def get_c2_workout_list(user,page=1):
|
||||
|
||||
# Get username, having access token.
|
||||
# Handy for checking if the API access is working
|
||||
def get_username(access_token):
|
||||
def get_username(access_token): # pragma: no cover
|
||||
authorizationstring = str('Bearer ' + access_token)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
@@ -1028,23 +945,23 @@ def get_userid(access_token):
|
||||
url = "https://log.concept2.com/api/users/me"
|
||||
try:
|
||||
response = requests.get(url,headers=headers)
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
return 0
|
||||
|
||||
|
||||
try:
|
||||
me_json = response.json()
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
return 0
|
||||
try:
|
||||
res = me_json['data']['id']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
return res
|
||||
|
||||
# For debugging purposes
|
||||
def process_callback(request):
|
||||
def process_callback(request): # pragma: no cover
|
||||
# need error handling
|
||||
|
||||
code = request.GET['code']
|
||||
@@ -1055,7 +972,7 @@ def process_callback(request):
|
||||
|
||||
return HttpResponse("got a user name: %s" % username)
|
||||
|
||||
def default(o):
|
||||
def default(o): # pragma: no cover
|
||||
if isinstance(o, numpy.int64): return int(o)
|
||||
raise TypeError
|
||||
|
||||
@@ -1063,9 +980,9 @@ def default(o):
|
||||
def workout_c2_upload(user,w,asynchron=False):
|
||||
message = 'trying C2 upload'
|
||||
try:
|
||||
if mytypes.c2mapping[w.workouttype] is None:
|
||||
if mytypes.c2mapping[w.workouttype] is None: # pragma: no cover
|
||||
return "This workout type cannot be uploaded to Concept2",0
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
return "This workout type cannot be uploaded to Concept2",0
|
||||
|
||||
thetoken = c2_open(user)
|
||||
@@ -1075,12 +992,12 @@ def workout_c2_upload(user,w,asynchron=False):
|
||||
# ready to upload. Hurray
|
||||
if (is_workout_user(user,w)):
|
||||
c2userid = get_userid(r.c2token)
|
||||
if not c2userid:
|
||||
if not c2userid: # pragma: no cover
|
||||
raise NoTokenError("User has no token")
|
||||
|
||||
data = createc2workoutdata(w)
|
||||
|
||||
if data == 0:
|
||||
if data == 0: # pragma: no cover
|
||||
return "Error: No data file. Contact info@rowsandall.com if the problem persists",0
|
||||
|
||||
authorizationstring = str('Bearer ' + r.c2token)
|
||||
@@ -1093,7 +1010,7 @@ def workout_c2_upload(user,w,asynchron=False):
|
||||
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
|
||||
|
||||
|
||||
if (response.status_code == 409 ):
|
||||
if (response.status_code == 409 ): # pragma: no cover
|
||||
message = "Concept2 Duplicate error"
|
||||
w.uploadedtoc2 = -1
|
||||
c2id = -1
|
||||
@@ -1105,10 +1022,10 @@ def workout_c2_upload(user,w,asynchron=False):
|
||||
w.uploadedtoc2 = c2id
|
||||
w.save()
|
||||
message = "Upload to Concept2 was successful"
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||
c2id = 0
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
job = myqueue(queue,
|
||||
handle_c2_sync,
|
||||
w.id,
|
||||
@@ -1136,7 +1053,7 @@ def rower_c2_token_refresh(user):
|
||||
|
||||
r.save()
|
||||
return r.c2token
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
return None
|
||||
|
||||
# Create workout data from Strava or Concept2
|
||||
@@ -1146,14 +1063,14 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
workoutsource='concept2'):
|
||||
try:
|
||||
workouttype = mytypes.c2mappinginv[data['type']]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
workouttype = 'rower'
|
||||
|
||||
if workouttype not in [x[0] for x in Workout.workouttypes]:
|
||||
if workouttype not in [x[0] for x in Workout.workouttypes]: # pragma: no cover
|
||||
workouttype = 'other'
|
||||
try:
|
||||
comments = data['comments']
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
comments = ' '
|
||||
|
||||
try:
|
||||
@@ -1165,9 +1082,9 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
try:
|
||||
rowdatetime = iso8601.parse_date(data['date_utc'])
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
rowdatetime = iso8601.parse_date(data['start_date'])
|
||||
except ParseError:
|
||||
except ParseError: # pragma: no cover
|
||||
rowdatetime = iso8601.parse_date(data['date'])
|
||||
|
||||
|
||||
@@ -1175,7 +1092,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
try:
|
||||
c2intervaltype = data['workout_type']
|
||||
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
c2intervaltype = ''
|
||||
|
||||
try:
|
||||
@@ -1185,12 +1102,12 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
try:
|
||||
t = data['comments'].split('\n', 1)[0]
|
||||
title += t[:40]
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
title = ''
|
||||
|
||||
try:
|
||||
comments = data['comments']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
comments = ''
|
||||
|
||||
starttimeunix = arrow.get(rowdatetime).timestamp()
|
||||
@@ -1207,7 +1124,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
nr_rows = len(unixtime)
|
||||
|
||||
try:
|
||||
try: # pragma: no cover
|
||||
latcoord = strokedata.loc[:,'lat']
|
||||
loncoord = strokedata.loc[:,'lon']
|
||||
except:
|
||||
@@ -1224,12 +1141,12 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
try:
|
||||
spm = strokedata.loc[:,'spm']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
spm = 0*dist2
|
||||
|
||||
try:
|
||||
hr = strokedata.loc[:,'hr']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
hr = 0*spm
|
||||
pace = strokedata.loc[:,'p']/10.
|
||||
pace = np.clip(pace,0,1e4)
|
||||
@@ -1237,7 +1154,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
velo = 500./pace
|
||||
power = 2.8*velo**3
|
||||
if workouttype in ['bike','bikeerg']:
|
||||
if workouttype in ['bike','bikeerg']: # pragma: no cover
|
||||
velo = 1000./pace
|
||||
pace = 500./velo
|
||||
|
||||
@@ -1285,10 +1202,10 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
try:
|
||||
totaldist = data['distance']
|
||||
totaltime = data['time']/10.
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
totaldist = 0
|
||||
totaltime = 0
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
totaldist = 0
|
||||
totaltime = 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user