coverage stravastuff stuff
This commit is contained in:
@@ -41,7 +41,7 @@ from rowsandall_app.settings import (
|
||||
|
||||
try:
|
||||
from json.decoder import JSONDecodeError
|
||||
except ImportError:
|
||||
except ImportError: # pragma: no cover
|
||||
JSONDecodeError = ValueError
|
||||
|
||||
from rowers.imports import *
|
||||
@@ -91,7 +91,7 @@ def strava_open(user):
|
||||
f.write(json.dumps(oauth_data))
|
||||
f.write('\n')
|
||||
token = imports_open(user, oauth_data)
|
||||
if user.rower.strava_owner_id == 0:
|
||||
if user.rower.strava_owner_id == 0: # pragma: no cover
|
||||
strava_owner_id = set_strava_athlete_id(user)
|
||||
return token
|
||||
|
||||
@@ -114,10 +114,10 @@ def rower_strava_token_refresh(user):
|
||||
return r.stravatoken
|
||||
|
||||
# Make authorization URL including random string
|
||||
def make_authorization_url(request):
|
||||
def make_authorization_url(request): # pragma: no cover
|
||||
return imports_make_authorization_url(oauth_data)
|
||||
|
||||
def strava_establish_push():
|
||||
def strava_establish_push(): # pragma: no cover
|
||||
url = "https://www.strava.com/api/v3/push_subscriptions"
|
||||
post_data = {
|
||||
'client_id': STRAVA_CLIENT_ID,
|
||||
@@ -133,7 +133,7 @@ def strava_establish_push():
|
||||
|
||||
return response.status_code
|
||||
|
||||
def strava_list_push():
|
||||
def strava_list_push(): # pragma: no cover
|
||||
url = "https://www.strava.com/api/v3/push_subscriptions"
|
||||
params = {
|
||||
'client_id': STRAVA_CLIENT_ID,
|
||||
@@ -147,7 +147,7 @@ def strava_list_push():
|
||||
return [w['id'] for w in data]
|
||||
return []
|
||||
|
||||
def strava_push_delete(id):
|
||||
def strava_push_delete(id): # pragma: no cover
|
||||
url = "https://www.strava.com/api/v3/push_subscriptions/{id}".format(id=id)
|
||||
params = {
|
||||
'client_id': STRAVA_CLIENT_ID,
|
||||
@@ -159,7 +159,7 @@ def strava_push_delete(id):
|
||||
|
||||
def set_strava_athlete_id(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate):
|
||||
@@ -173,7 +173,7 @@ def set_strava_athlete_id(user):
|
||||
|
||||
response = requests.get(url,headers=headers,params={})
|
||||
|
||||
if response.status_code == 200:
|
||||
if response.status_code == 200: # pragma: no cover
|
||||
r.strava_owner_id = response.json()['id']
|
||||
r.save()
|
||||
return response.json()['id']
|
||||
@@ -185,10 +185,10 @@ def set_strava_athlete_id(user):
|
||||
def get_strava_workout_list(user,limit_n=0):
|
||||
r = Rower.objects.get(user=user)
|
||||
|
||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate):
|
||||
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate): # pragma: no cover
|
||||
s = "Token expired. Needs to refresh."
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
@@ -202,7 +202,7 @@ def get_strava_workout_list(user,limit_n=0):
|
||||
|
||||
if limit_n==0:
|
||||
params = {}
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
params = {'per_page':limit_n}
|
||||
|
||||
s = requests.get(url,headers=headers,params=params)
|
||||
@@ -212,7 +212,7 @@ def get_strava_workout_list(user,limit_n=0):
|
||||
|
||||
|
||||
# gets all new Strava workouts for a rower
|
||||
def get_strava_workouts(rower):
|
||||
def get_strava_workouts(rower): # pragma: no cover
|
||||
try:
|
||||
thetoken = strava_open(rower.user)
|
||||
except NoTokenError:
|
||||
@@ -279,13 +279,13 @@ def create_async_workout(alldata,user,stravaid,debug=False):
|
||||
stravaid = data['id']
|
||||
try:
|
||||
workouttype = mytypes.stravamappinginv[data['type']]
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
workouttype = 'other'
|
||||
|
||||
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'
|
||||
|
||||
if workouttype.lower() == 'rowing':
|
||||
if workouttype.lower() == 'rowing': # pragma: no cover
|
||||
workouttype = 'rower'
|
||||
if 'summary_polyline' in data['map']:
|
||||
workouttype = 'water'
|
||||
@@ -305,18 +305,18 @@ def create_async_workout(alldata,user,stravaid,debug=False):
|
||||
rowdatetime = iso8601.parse_date(data['date_utc'])
|
||||
except KeyError:
|
||||
rowdatetime = iso8601.parse_date(data['start_date'])
|
||||
except ParseError:
|
||||
except ParseError: # pragma: no cover
|
||||
rowdatetime = iso8601.parse_date(data['date'])
|
||||
|
||||
try:
|
||||
c2intervaltype = data['workout_type']
|
||||
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
c2intervaltype = ''
|
||||
|
||||
try:
|
||||
title = data['name']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
title = ""
|
||||
try:
|
||||
t = data['comments'].split('\n', 1)[0]
|
||||
@@ -367,7 +367,7 @@ from rowers.utils import get_strava_stream
|
||||
def async_get_workout(user,stravaid):
|
||||
try:
|
||||
token = strava_open(user)
|
||||
except NoTokenError:
|
||||
except NoTokenError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
csvfilename = 'media/{code}_{stravaid}.csv'.format(code=uuid4().hex[:16],stravaid=stravaid)
|
||||
@@ -385,15 +385,15 @@ def async_get_workout(user,stravaid):
|
||||
def get_workout(user,stravaid,do_async=False):
|
||||
try:
|
||||
thetoken = strava_open(user)
|
||||
except NoTokenError:
|
||||
except NoTokenError: # pragma: no cover
|
||||
s = "Token error"
|
||||
return custom_exception_handler(401,s)
|
||||
|
||||
r = Rower.objects.get(user=user)
|
||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
return custom_exception_handler(401,s)
|
||||
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate):
|
||||
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate): # pragma: no cover
|
||||
s = "Token expired. Needs to refresh."
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
@@ -411,7 +411,7 @@ def get_workout(user,stravaid,do_async=False):
|
||||
workoutsummary['timezone'] = "Etc/UTC"
|
||||
try:
|
||||
startdatetime = workoutsummary['start_date']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
startdatetime = timezone.now()
|
||||
|
||||
spm = get_strava_stream(r,'cadence',stravaid)
|
||||
@@ -424,29 +424,29 @@ def get_workout(user,stravaid,do_async=False):
|
||||
|
||||
if t is not None:
|
||||
nr_rows = len(t)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
duration = int(workoutsummary['elapsed_time'])
|
||||
t = pd.Series(range(duration+1))
|
||||
|
||||
nr_rows = len(t)
|
||||
|
||||
|
||||
if nr_rows == 0:
|
||||
if nr_rows == 0: # pragma: no cover
|
||||
return (0,"Error: Time data had zero length")
|
||||
|
||||
if d is None:
|
||||
if d is None: # pragma: no cover
|
||||
d = 0*t
|
||||
|
||||
if spm is None:
|
||||
if spm is None: # pragma: no cover
|
||||
spm = np.zeros(nr_rows)
|
||||
|
||||
if power is None:
|
||||
if power is None: # pragma: no cover
|
||||
power = np.zeros(nr_rows)
|
||||
|
||||
if hr is None:
|
||||
if hr is None: # pragma: no cover
|
||||
hr = np.zeros(nr_rows)
|
||||
|
||||
if velo is None:
|
||||
if velo is None: # pragma: no cover
|
||||
velo = np.zeros(nr_rows)
|
||||
|
||||
dt = np.diff(t).mean()
|
||||
@@ -458,10 +458,10 @@ def get_workout(user,stravaid,do_async=False):
|
||||
try:
|
||||
lat = coords[:,0]
|
||||
lon = coords[:,1]
|
||||
except IndexError:
|
||||
except IndexError: # pragma: no cover
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
|
||||
@@ -497,7 +497,7 @@ def createstravaworkoutdata(w,dozip=True):
|
||||
filename = w.csvfilename
|
||||
try:
|
||||
row = rowingdata(csvfile=filename)
|
||||
except IOError:
|
||||
except IOError: # pragma: no cover
|
||||
data = dataprep.read_df_sql(w.id)
|
||||
try:
|
||||
datalength = len(data)
|
||||
@@ -532,12 +532,12 @@ def createstravaworkoutdata(w,dozip=True):
|
||||
|
||||
try:
|
||||
os.remove(tcxfilename)
|
||||
except WindowError:
|
||||
except WindowError: # pragma: no cover
|
||||
pass
|
||||
|
||||
return gzfilename,""
|
||||
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
return tcxfilename,""
|
||||
|
||||
|
||||
@@ -551,13 +551,13 @@ def handle_stravaexport(f2,workoutname,stravatoken,description='',
|
||||
act = client.upload_activity(f2,'tcx.gz',name=workoutname)
|
||||
|
||||
try:
|
||||
if quick:
|
||||
if quick: # pragma: no cover
|
||||
res = act.wait(poll_interval=1.0, timeout=10)
|
||||
message = 'Workout successfully synchronized to Strava'
|
||||
else:
|
||||
res = act.wait(poll_interval=5.0,timeout=30)
|
||||
message = 'Workout successfully synchronized to Strava'
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
res = 0
|
||||
message = 'Strava upload timed out'
|
||||
|
||||
@@ -566,9 +566,9 @@ def handle_stravaexport(f2,workoutname,stravatoken,description='',
|
||||
if res:
|
||||
try:
|
||||
act = client.update_activity(res.id,activity_type=activity_type,description=description,device_name='Rowsandall.com')
|
||||
except TypeError:
|
||||
except TypeError: # pragma: no cover
|
||||
act = client.update_activity(res.id,activity_type=activity_type,description=description)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
message = 'Strava activity update timed out.'
|
||||
return (0,message)
|
||||
|
||||
@@ -581,16 +581,16 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
workoutsource='strava'):
|
||||
try:
|
||||
workouttype = mytypes.stravamappinginv[data['type']]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
workouttype = 'other'
|
||||
|
||||
if workouttype.lower() == 'rowing':
|
||||
if workouttype.lower() == 'rowing': # pragma: no cover
|
||||
workouttype = 'rower'
|
||||
|
||||
if 'summary_polyline' in data['map'] and workouttype=='rower':
|
||||
if 'summary_polyline' in data['map'] and workouttype=='rower': # pragma: no cover
|
||||
workouttype = 'water'
|
||||
|
||||
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']
|
||||
@@ -607,7 +607,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
rowdatetime = iso8601.parse_date(data['date_utc'])
|
||||
except KeyError:
|
||||
rowdatetime = iso8601.parse_date(data['start_date'])
|
||||
except ParseError:
|
||||
except ParseError: # pragma: no cover
|
||||
rowdatetime = iso8601.parse_date(data['date'])
|
||||
|
||||
|
||||
@@ -619,7 +619,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
try:
|
||||
title = data['name']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
title = ""
|
||||
try:
|
||||
t = data['comments'].split('\n', 1)[0]
|
||||
@@ -641,9 +641,9 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
try:
|
||||
latcoord = strokedata.loc[:,'lat']
|
||||
loncoord = strokedata.loc[:,'lon']
|
||||
if latcoord.std() == 0 and loncoord.std() == 0 and workouttype == 'water':
|
||||
if latcoord.std() == 0 and loncoord.std() == 0 and workouttype == 'water': # pragma: no cover
|
||||
workouttype = 'rower'
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
latcoord = np.zeros(nr_rows)
|
||||
loncoord = np.zeros(nr_rows)
|
||||
if workouttype == 'water':
|
||||
@@ -653,19 +653,19 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
try:
|
||||
strokelength = strokedata.loc[:,'strokelength']
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
strokelength = np.zeros(nr_rows)
|
||||
|
||||
dist2 = 0.1*strokedata.loc[:,'d']
|
||||
|
||||
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)
|
||||
@@ -675,7 +675,7 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
|
||||
try:
|
||||
power = strokedata.loc[:,'power']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
power = 2.8*velo**3
|
||||
|
||||
#if power.std() == 0 and power.mean() == 0:
|
||||
@@ -734,27 +734,27 @@ def add_workout_from_data(user,importid,data,strokedata,
|
||||
def workout_strava_upload(user,w, quick=False,asynchron=True):
|
||||
try:
|
||||
thetoken = strava_open(user)
|
||||
except NoTokenError:
|
||||
except NoTokenError: # pragma: no cover
|
||||
return "Please connect to Strava first",0
|
||||
|
||||
message = "Uploading to Strava"
|
||||
stravaid=-1
|
||||
r = Rower.objects.get(user=user)
|
||||
res = -1
|
||||
if (r.stravatoken == '') or (r.stravatoken is None):
|
||||
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise NoTokenError("Your hovercraft is full of eels")
|
||||
|
||||
if (is_workout_user(user,w)):
|
||||
if asynchron:
|
||||
tcxfile, tcxmesg = createstravaworkoutdata(w)
|
||||
if not tcxfile:
|
||||
if not tcxfile: # pragma: no cover
|
||||
return "Failed to create workout data",0
|
||||
activity_type = r.stravaexportas
|
||||
if r.stravaexportas == 'match':
|
||||
try:
|
||||
activity_type = mytypes.stravamapping[w.workouttype]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
activity_type = 'Rowing'
|
||||
job = myqueue(queue,
|
||||
handle_strava_sync,
|
||||
@@ -771,16 +771,20 @@ def workout_strava_upload(user,w, quick=False,asynchron=True):
|
||||
if r.stravaexportas == 'match':
|
||||
try:
|
||||
activity_type = mytypes.stravamapping[w.workouttype]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
activity_type = 'Rowing'
|
||||
|
||||
with open(tcxfile,'rb') as f:
|
||||
try:
|
||||
description = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
|
||||
except TypeError:
|
||||
description = ' via rowsandall.com'
|
||||
res,mes = handle_stravaexport(
|
||||
f,w.name,
|
||||
r.stravatoken,
|
||||
description=w.notes+'\n from '+w.workoutsource+' via rowsandall.com',
|
||||
description=description,
|
||||
activity_type=activity_type,quick=quick,asynchron=asynchron)
|
||||
if res==0:
|
||||
if res==0: # pragma: no cover
|
||||
message = mes
|
||||
w.uploadedtostrava = -1
|
||||
stravaid = -1
|
||||
@@ -795,19 +799,19 @@ def workout_strava_upload(user,w, quick=False,asynchron=True):
|
||||
w.save()
|
||||
try:
|
||||
os.remove(tcxfile)
|
||||
except WindowsError:
|
||||
except WindowsError: # pragma: no cover
|
||||
pass
|
||||
message = mes
|
||||
stravaid = res
|
||||
return message,stravaid
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
message = "Strava TCX data error "+tcxmesg
|
||||
w.uploadedtostrava = -1
|
||||
stravaid = -1
|
||||
w.save()
|
||||
return message, stravaid
|
||||
|
||||
except ActivityUploadFailed as e:
|
||||
except ActivityUploadFailed as e: # pragma: no cover
|
||||
message = "Strava Upload error: %s" % e
|
||||
w.uploadedtostrava = -1
|
||||
stravaid = -1
|
||||
@@ -849,75 +853,75 @@ def handle_strava_import_stroke_data(title,
|
||||
|
||||
try:
|
||||
hr = get_strava_stream(r,'heartrate',stravaid)
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
hr = 0*spm
|
||||
|
||||
try:
|
||||
velo = get_strava_stream(r,'velocity_smooth',stravaid)
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
velo = 0*t
|
||||
|
||||
try:
|
||||
d = get_strava_stream(r,'distance',stravaid)
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
d = 0*t
|
||||
|
||||
try:
|
||||
coords = get_strava_stream(r,'latlng',stravaid)
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
coords = 0*t
|
||||
try:
|
||||
power = get_strava_stream(r,'watts',stravaid)
|
||||
except JSONDecodeError:
|
||||
except JSONDecodeError: # pragma: no cover
|
||||
power = 0*t
|
||||
|
||||
if t is not None:
|
||||
nr_rows = len(t)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
return 0
|
||||
|
||||
if nr_rows == 0:
|
||||
if nr_rows == 0: # pragma: no cover
|
||||
return 0
|
||||
|
||||
if d is None:
|
||||
if d is None: # pragma: no cover
|
||||
d = 0*t
|
||||
|
||||
if spm is None:
|
||||
if spm is None: # pragma: no cover
|
||||
spm = np.zeros(nr_rows)
|
||||
|
||||
if power is None:
|
||||
if power is None: # pragma: no cover
|
||||
power = np.zeros(nr_rows)
|
||||
|
||||
if hr is None:
|
||||
if hr is None: # pragma: no cover
|
||||
hr = np.zeros(nr_rows)
|
||||
|
||||
if velo is None:
|
||||
if velo is None: # pragma: no cover
|
||||
velo = np.zeros(nr_rows)
|
||||
|
||||
|
||||
f = np.diff(t).mean()
|
||||
if f != 0:
|
||||
windowsize = 2*(int(10./(f)))+1
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
windowsize = 1
|
||||
|
||||
if windowsize > 3 and windowsize < len(velo):
|
||||
velo2 = savgol_filter(velo,windowsize,3)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
velo2 = velo
|
||||
|
||||
if coords is not None:
|
||||
try:
|
||||
lat = coords[:,0]
|
||||
lon = coords[:,1]
|
||||
if lat.std() == 0 and lon.std() == 0 and workouttype == 'water':
|
||||
if lat.std() == 0 and lon.std() == 0 and workouttype == 'water': # pragma: no cover
|
||||
workouttype = 'rower'
|
||||
except IndexError:
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
if workouttype == 'water':
|
||||
workouttype = 'rower'
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
lat = np.zeros(len(t))
|
||||
lon = np.zeros(len(t))
|
||||
if workouttype == 'water':
|
||||
@@ -933,7 +937,7 @@ def handle_strava_import_stroke_data(title,
|
||||
|
||||
strokedistance = 60.*velo2/spm
|
||||
|
||||
if workouttype == 'rower' and pd.Series(power).mean() == 0:
|
||||
if workouttype == 'rower' and pd.Series(power).mean() == 0: # pragma: no cover
|
||||
power = 2.8*(velo2**3)
|
||||
|
||||
nr_strokes = len(t)
|
||||
|
||||
Reference in New Issue
Block a user