Private
Public Access
1
0

passes checks in python3

This commit is contained in:
Sander Roosendaal
2019-02-24 15:57:26 +01:00
parent c7ec31344b
commit 866566172c
51 changed files with 4037 additions and 3999 deletions

View File

@@ -1,3 +1,4 @@
from __future__ import unicode_literals, absolute_import
# The interactions with the Concept2 logbook API
# All C2 related functions should be defined here
# (There is still some stuff defined directly in views.py. Need to
@@ -6,7 +7,7 @@
from rowers.imports import *
import datetime
from requests import Request, Session
import mytypes
import rowers.mytypes as mytypes
from rowers.mytypes import otwtypes
from iso8601 import ParseError
@@ -19,7 +20,7 @@ import django_rq
queue = django_rq.get_queue('default')
queuelow = django_rq.get_queue('low')
queuehigh = django_rq.get_queue('low')
from utils import myqueue
from rowers.utils import myqueue
oauth_data = {
'client_id': C2_CLIENT_ID,
@@ -41,19 +42,19 @@ oauth_data = {
def c2_open(user):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
s = "Token doesn't exist. Need to authorize"
raise NoTokenError("User has no token")
s = "Token doesn't exist. Need to authorize"
raise NoTokenError("User has no token")
else:
if (timezone.now()>r.tokenexpirydate):
res = rower_c2_token_refresh(user)
if (timezone.now()>r.tokenexpirydate):
res = rower_c2_token_refresh(user)
if res == None:
raise NoTokenError("User has no token")
if res[0] != None:
thetoken = res[0]
else:
raise NoTokenError("User has no token")
else:
thetoken = r.c2token
else:
thetoken = r.c2token
return thetoken
@@ -61,11 +62,11 @@ 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"
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)
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
starttimeunix = arrow.get(startdatetime).timestamp
@@ -179,9 +180,9 @@ def makeseconds(t):
# convert our weight class code to Concept2 weight class code
def c2wc(weightclass):
if (weightclass=="lwt"):
res = "L"
res = "L"
else:
res = "H"
res = "H"
return res
@@ -343,17 +344,17 @@ def createc2workoutdata_as_splits(w):
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)
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")
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S.%f")
except ValueError:
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
try:
newnotes = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
@@ -365,19 +366,19 @@ def createc2workoutdata_as_splits(w):
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,
}
"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
@@ -418,13 +419,13 @@ def createc2workoutdata(w):
hr = 0*d
stroke_data = []
for i in range(len(t)):
thisrecord = {"t":t[i],"d":d[i],"p":p[i],"spm":spm[i],"hr":hr[i]}
stroke_data.append(thisrecord)
thisrecord = {"t":t[i],"d":d[i],"p":p[i],"spm":spm[i],"hr":hr[i]}
stroke_data.append(thisrecord)
try:
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S.%f")
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S.%f")
except ValueError:
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
workouttype = w.workouttype
if workouttype in otwtypes:
@@ -436,19 +437,19 @@ def createc2workoutdata(w):
startdate = datetime.datetime.combine(w.date,datetime.time())
data = {
"type": mytypes.c2mapping[workouttype],
"date": w.startdatetime.isoformat(),
"timezone": w.timezone,
"distance": int(w.distance),
"time": int(10*makeseconds(durationstr)),
"weight_class": c2wc(w.weightcategory),
"comments": w.notes,
"heart_rate": {
"average": averagehr,
"max": maxhr,
},
"stroke_data": stroke_data,
}
"type": mytypes.c2mapping[workouttype],
"date": w.startdatetime.isoformat(),
"timezone": w.timezone,
"distance": int(w.distance),
"time": int(10*makeseconds(durationstr)),
"weight_class": c2wc(w.weightcategory),
"comments": w.notes,
"heart_rate": {
"average": averagehr,
"max": maxhr,
},
"stroke_data": stroke_data,
}
return data
@@ -458,10 +459,10 @@ def do_refresh_token(refreshtoken):
scope = "results:write,user:read"
client_auth = requests.auth.HTTPBasicAuth(C2_CLIENT_ID, C2_CLIENT_SECRET)
post_data = {"grant_type": "refresh_token",
"client_secret": C2_CLIENT_SECRET,
"client_id":C2_CLIENT_ID,
"refresh_token": refreshtoken,
}
"client_secret": C2_CLIENT_SECRET,
"client_id":C2_CLIENT_ID,
"refresh_token": refreshtoken,
}
headers = {'user-agent': 'sanderroosendaal'}
url = "https://log.concept2.com/oauth/access_token"
s = Session()
@@ -498,9 +499,9 @@ def get_token(code):
post_data = {"grant_type": "authorization_code",
"code": code,
"redirect_uri": C2_REDIRECT_URI,
"client_secret": C2_CLIENT_SECRET,
"client_id":C2_CLIENT_ID,
}
"client_secret": C2_CLIENT_SECRET,
"client_id":C2_CLIENT_ID,
}
headers = {'user-agent': 'sanderroosendaal'}
url = "https://log.concept2.com/oauth/access_token"
s = Session()
@@ -554,19 +555,19 @@ def make_authorization_url(request):
def get_workout(user,c2id):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s) ,0
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s) ,0
elif (timezone.now()>r.tokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s),0
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s),0
else:
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)
s = requests.get(url,headers=headers)
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)
s = requests.get(url,headers=headers)
data = s.json()['data']
@@ -583,9 +584,9 @@ def get_workout(user,c2id):
# Check if workout has stroke data, and get the stroke data
if data['stroke_data']:
res2 = get_c2_workout_strokes(user,c2id)
if res2.status_code == 200:
strokedata = pd.DataFrame.from_dict(res2.json()['data'])
res2 = get_c2_workout_strokes(user,c2id)
if res2.status_code == 200:
strokedata = pd.DataFrame.from_dict(res2.json()['data'])
else:
strokedata = pd.DataFrame()
else:
@@ -597,19 +598,19 @@ def get_workout(user,c2id):
def get_c2_workout_strokes(user,c2id):
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"
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)
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
s = requests.get(url,headers=headers)
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
s = requests.get(url,headers=headers)
return s
@@ -618,21 +619,21 @@ def get_c2_workout_strokes(user,c2id):
def get_c2_workout_list(user,page=1):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (timezone.now()>r.tokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results"
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
url = "https://log.concept2.com/api/users/me/results"
url += "?page={page}".format(page=page)
s = requests.get(url,headers=headers)
s = requests.get(url,headers=headers)
return s
@@ -642,8 +643,8 @@ def get_c2_workout_list(user,page=1):
def get_username(access_token):
authorizationstring = str('Bearer ' + access_token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
import urllib
url = "https://log.concept2.com/api/users/me"
response = requests.get(url,headers=headers)
@@ -664,8 +665,8 @@ def get_username(access_token):
def get_userid(access_token):
authorizationstring = str('Bearer ' + access_token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
import urllib
url = "https://log.concept2.com/api/users/me"
response = requests.get(url,headers=headers)
@@ -706,40 +707,40 @@ def workout_c2_upload(user,w):
# ready to upload. Hurray
if (checkworkoutuser(user,w)):
c2userid = get_userid(r.c2token)
c2userid = get_userid(r.c2token)
if not c2userid:
raise NoTokenError
data = createc2workoutdata(w)
data = createc2workoutdata(w)
if data == 0:
return "Error: No data file. Contact info@rowsandall.com if the problem persists",0
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
import urllib
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
response = requests.post(url,headers=headers,data=json.dumps(data))
authorizationstring = str('Bearer ' + r.c2token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json'}
import urllib
url = "https://log.concept2.com/api/users/%s/results" % (c2userid)
response = requests.post(url,headers=headers,data=json.dumps(data))
if (response.status_code == 409 ):
message = "Concept2 Duplicate error"
w.uploadedtoc2 = -1
message = "Concept2 Duplicate error"
w.uploadedtoc2 = -1
c2id = -1
w.save()
w.save()
elif (response.status_code == 201 or response.status_code == 200):
try:
# s= json.loads(response.text)
# s= json.loads(response.text)
s = response.json()
c2id = s['data']['id']
w.uploadedtoc2 = c2id
w.save()
c2id = s['data']['id']
w.uploadedtoc2 = c2id
w.save()
message = "Upload to Concept2 was successful"
except:
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
c2id = 0
else:
else:
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
c2id = 0
@@ -776,41 +777,41 @@ def add_workout_from_data(user,importid,data,strokedata,
workouttype = 'rower'
if workouttype not in [x[0] for x in Workout.workouttypes]:
workouttype = 'other'
workouttype = 'other'
try:
comments = data['comments']
comments = data['comments']
except:
comments = ' '
comments = ' '
try:
thetimezone = tz(data['timezone'])
thetimezone = tz(data['timezone'])
except:
thetimezone = 'UTC'
thetimezone = 'UTC'
r = Rower.objects.get(user=user)
try:
rowdatetime = iso8601.parse_date(data['date_utc'])
rowdatetime = iso8601.parse_date(data['date_utc'])
except KeyError:
rowdatetime = iso8601.parse_date(data['start_date'])
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
rowdatetime = iso8601.parse_date(data['date'])
rowdatetime = iso8601.parse_date(data['date'])
try:
c2intervaltype = data['workout_type']
except KeyError:
c2intervaltype = ''
c2intervaltype = ''
try:
title = data['name']
title = data['name']
except KeyError:
title = ""
try:
t = data['comments'].split('\n', 1)[0]
title += t[:20]
except:
title = 'Imported'
title = ""
try:
t = data['comments'].split('\n', 1)[0]
title += t[:20]
except:
title = 'Imported'
starttimeunix = arrow.get(rowdatetime).timestamp
@@ -825,17 +826,17 @@ def add_workout_from_data(user,importid,data,strokedata,
nr_rows = len(unixtime)
try:
latcoord = strokedata.loc[:,'lat']
loncoord = strokedata.loc[:,'lon']
latcoord = strokedata.loc[:,'lat']
loncoord = strokedata.loc[:,'lon']
except:
latcoord = np.zeros(nr_rows)
loncoord = np.zeros(nr_rows)
latcoord = np.zeros(nr_rows)
loncoord = np.zeros(nr_rows)
try:
strokelength = strokedata.loc[:,'strokelength']
strokelength = strokedata.loc[:,'strokelength']
except:
strokelength = np.zeros(nr_rows)
strokelength = np.zeros(nr_rows)
dist2 = 0.1*strokedata.loc[:,'d']
@@ -862,27 +863,27 @@ def add_workout_from_data(user,importid,data,strokedata,
# save csv
# Create data frame with all necessary data to write to csv
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
' Horizontal (meters)': dist2,
' Cadence (stokes/min)':spm,
' HRCur (bpm)':hr,
' longitude':loncoord,
' latitude':latcoord,
' Stroke500mPace (sec/500m)':pace,
' Power (watts)':power,
' DragFactor':np.zeros(nr_rows),
' DriveLength (meters)':np.zeros(nr_rows),
' StrokeDistance (meters)':strokelength,
' DriveTime (ms)':np.zeros(nr_rows),
' StrokeRecoveryTime (ms)':np.zeros(nr_rows),
' AverageDriveForce (lbs)':np.zeros(nr_rows),
' PeakDriveForce (lbs)':np.zeros(nr_rows),
' lapIdx':lapidx,
' ElapsedTime (sec)':seconds
})
' Horizontal (meters)': dist2,
' Cadence (stokes/min)':spm,
' HRCur (bpm)':hr,
' longitude':loncoord,
' latitude':latcoord,
' Stroke500mPace (sec/500m)':pace,
' Power (watts)':power,
' DragFactor':np.zeros(nr_rows),
' DriveLength (meters)':np.zeros(nr_rows),
' StrokeDistance (meters)':strokelength,
' DriveTime (ms)':np.zeros(nr_rows),
' StrokeRecoveryTime (ms)':np.zeros(nr_rows),
' AverageDriveForce (lbs)':np.zeros(nr_rows),
' PeakDriveForce (lbs)':np.zeros(nr_rows),
' lapIdx':lapidx,
' ElapsedTime (sec)':seconds
})
df.sort_values(by='TimeStamp (sec)',ascending=True)
timestr = strftime("%Y%m%d-%H%M%S")