Private
Public Access
1
0

get_workout passing tests using mock upload api, need to add processing of nkid, oarlock params, etc

This commit is contained in:
Sander Roosendaal
2021-04-04 17:22:01 +02:00
parent f82c451e58
commit 13391c0e40
5 changed files with 117 additions and 13 deletions

View File

@@ -29,7 +29,8 @@ import gzip
from rowsandall_app.settings import (
NK_CLIENT_ID, NK_REDIRECT_URI, NK_CLIENT_SECRET,
SITE_URL, NK_API_LOCATION
SITE_URL, NK_API_LOCATION,
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET,
)
try:
@@ -91,13 +92,13 @@ def get_token(code):
def nk_open(user):
r = Rower.objects.get(user=user)
print(r.nktokenexpirydate)
if (r.nktoken == '') or (r.nktoken is None):
s = "Token doesn't exist. Need to authorize"
raise NoTokenError("User has no token")
else:
if (timezone.now()>r.nktokenexpirydate):
print('refreshing token')
thetoken = rower_nk_token_refresh(user)
if thetoken == None:
raise NoTokenError("User has no token")
@@ -192,6 +193,66 @@ def getdict(x, seatIndex=1):
return {}
def add_workout_from_data(user,nkid,data,strokedata,source='nk',splitdata=None,
workoutsource='nklinklogbook'):
csvfilename = 'media/{code}_{nkid}.csv.gz'.format(
nkid=nkid,
code = uuid4().hex[:16]
)
strokedata.to_csv(csvfilename, index_label='index', compression='gzip')
userid = user.id
title = data["name"]
speedInput = data["speedInput"]
elapsedTime = data["elapsedTime"]
totalDistanceGps = data["totalDistanceGps"]
totalDistanceImp = data["totalDistanceImp"]
intervals = data["intervals"]
oarlockSessions = data["oarlockSessions"]
# oarlock inboard, length, boat name
if oarlockSessions:
oarlocksession = oarlockSessions[0] # should take seatIndex
boatName = oarlocksession["boatName"]
oarLength = oarlocksession["oarLength"] # cm
oarInboardLength = oarlocksession["oarInboardLength"] # cm
seatNumber = oarlocksession["seatNumber"]
workouttype = "water"
boattype = "1x"
uploadoptions = {
'secret': UPLOAD_SERVICE_SECRET,
'user':userid,
'file': csvfilename,
'title': title,
'workouttype': workouttype,
'boattype': boattype,
'nkid':nkid,
}
session = requests.session()
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
session.headers.update(newHeaders)
response = session.post(UPLOAD_SERVICE_URL,json=uploadoptions)
if response.status_code != 200:
return 0,response.text
try:
workoutid = response.json()['id']
except KeyError:
workoutid = 1
# evt update workout summary
# return
return workoutid,""
def get_workout(user,nkid):
r = Rower.objects.get(user=user)
if (r.nktoken == '') or (r.nktoken is None):
@@ -221,7 +282,7 @@ def get_workout(user,nkid):
pass
jsonData = response.json()
#print(jsonData)
strokeData = jsonData[str(nkid)]
df = pd.DataFrame.from_dict(strokeData)
@@ -237,11 +298,28 @@ def get_workout(user,nkid):
df.sort_values(by='timestamp',ascending=True,inplace=True)
df.fillna(inplace=True,method='ffill')
#df = pd.concat([df,df2],axis=1)
# get workout data
timestampbegin = df['timestamp'].min()
timestampend = df['timestamp'].max()
url = NK_API_LOCATION+"api/v1/sessions/"
params = {
'startTime':timestampbegin-1,
'endTime': timestampend+1,
}
response = requests.get(url, headers=headers,params=params)
if response.status_code != 200:
# error handling
pass
jsondata = response.json()
workoutdata = {}
for w in jsondata:
if str(w['id']) == str(nkid):
workoutdata = w
# not to_csv and run upload API!
df.to_csv('~/Downloads/nk_logbook.csv')
#strokedata = df.to_json(orient='records')
# get workout data
return 0,0
return workoutdata, df