get_workout passing tests using mock upload api, need to add processing of nkid, oarlock params, etc
This commit is contained in:
@@ -1855,7 +1855,6 @@ parsers = {
|
|||||||
|
|
||||||
def parsenonpainsled(fileformat,f2,summary,startdatetime=''):
|
def parsenonpainsled(fileformat,f2,summary,startdatetime=''):
|
||||||
try:
|
try:
|
||||||
print(fileformat)
|
|
||||||
row = parsers[fileformat](f2)
|
row = parsers[fileformat](f2)
|
||||||
if startdatetime != '':
|
if startdatetime != '':
|
||||||
row.rowdatetime = arrow.get(startdatetime).datetime
|
row.rowdatetime = arrow.get(startdatetime).datetime
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ import gzip
|
|||||||
|
|
||||||
from rowsandall_app.settings import (
|
from rowsandall_app.settings import (
|
||||||
NK_CLIENT_ID, NK_REDIRECT_URI, NK_CLIENT_SECRET,
|
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:
|
try:
|
||||||
@@ -91,13 +92,13 @@ def get_token(code):
|
|||||||
|
|
||||||
def nk_open(user):
|
def nk_open(user):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
print(r.nktokenexpirydate)
|
|
||||||
if (r.nktoken == '') or (r.nktoken is None):
|
if (r.nktoken == '') or (r.nktoken is None):
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
raise NoTokenError("User has no token")
|
raise NoTokenError("User has no token")
|
||||||
else:
|
else:
|
||||||
if (timezone.now()>r.nktokenexpirydate):
|
if (timezone.now()>r.nktokenexpirydate):
|
||||||
print('refreshing token')
|
|
||||||
thetoken = rower_nk_token_refresh(user)
|
thetoken = rower_nk_token_refresh(user)
|
||||||
if thetoken == None:
|
if thetoken == None:
|
||||||
raise NoTokenError("User has no token")
|
raise NoTokenError("User has no token")
|
||||||
@@ -192,6 +193,66 @@ def getdict(x, seatIndex=1):
|
|||||||
|
|
||||||
return {}
|
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):
|
def get_workout(user,nkid):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
if (r.nktoken == '') or (r.nktoken is None):
|
if (r.nktoken == '') or (r.nktoken is None):
|
||||||
@@ -221,7 +282,7 @@ def get_workout(user,nkid):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
jsonData = response.json()
|
jsonData = response.json()
|
||||||
#print(jsonData)
|
|
||||||
strokeData = jsonData[str(nkid)]
|
strokeData = jsonData[str(nkid)]
|
||||||
|
|
||||||
df = pd.DataFrame.from_dict(strokeData)
|
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.sort_values(by='timestamp',ascending=True,inplace=True)
|
||||||
df.fillna(inplace=True,method='ffill')
|
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!
|
# not to_csv and run upload API!
|
||||||
df.to_csv('~/Downloads/nk_logbook.csv')
|
#strokedata = df.to_json(orient='records')
|
||||||
|
|
||||||
# get workout data
|
return workoutdata, df
|
||||||
|
|
||||||
return 0,0
|
|
||||||
|
|||||||
@@ -101,6 +101,30 @@ def mock_c2open(*args, **kwargs):
|
|||||||
print('mock')
|
print('mock')
|
||||||
return('aap')
|
return('aap')
|
||||||
|
|
||||||
|
def mocked_session(*args, **kwargs):
|
||||||
|
|
||||||
|
class MockEngine:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.headers = MockHeaders()
|
||||||
|
|
||||||
|
def post(self, *args, **kwargs):
|
||||||
|
return MockResponse({},200)
|
||||||
|
|
||||||
|
class MockHeaders:
|
||||||
|
def update(*args, **kwargs):
|
||||||
|
return None
|
||||||
|
|
||||||
|
class MockResponse:
|
||||||
|
def __init__(self, json_data, status_code):
|
||||||
|
self.json_data = json_data
|
||||||
|
self.status_code = status_code
|
||||||
|
self.ok = True
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return self.json_data
|
||||||
|
|
||||||
|
return MockEngine()
|
||||||
|
|
||||||
def mocked_sqlalchemy(*args, **kwargs):
|
def mocked_sqlalchemy(*args, **kwargs):
|
||||||
# return object with method
|
# return object with method
|
||||||
|
|
||||||
@@ -939,7 +963,7 @@ def mocked_requests(*args, **kwargs):
|
|||||||
|
|
||||||
return MockResponse(nkstrokedata,200)
|
return MockResponse(nkstrokedata,200)
|
||||||
if nkworkoutlisttester.match(args[0]):
|
if nkworkoutlisttester.match(args[0]):
|
||||||
|
|
||||||
return MockResponse(nkworkoutlist,200)
|
return MockResponse(nkworkoutlist,200)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -426,15 +426,17 @@ class NKObjects(DjangoTestCase):
|
|||||||
|
|
||||||
@patch('rowers.nkstuff.requests.get', side_effect=mocked_requests)
|
@patch('rowers.nkstuff.requests.get', side_effect=mocked_requests)
|
||||||
@patch('rowers.nkstuff.requests.post', side_effect=mocked_requests)
|
@patch('rowers.nkstuff.requests.post', side_effect=mocked_requests)
|
||||||
@patch('rowers.dataprep.getsmallrowdata_db')
|
@patch('rowers.nkstuff.requests.session', side_effect=mocked_session)
|
||||||
|
@patch('rowers.dataprep.getsmallrowdata_db', side_effect=mocked_getsmallrowdata_db)
|
||||||
def test_nk_import(self, mock_get, mock_post,
|
def test_nk_import(self, mock_get, mock_post,
|
||||||
|
mocked_session,
|
||||||
mocked_getsmallrowdata_db):
|
mocked_getsmallrowdata_db):
|
||||||
|
|
||||||
result = rowers.nkstuff.rower_nk_token_refresh(self.u)
|
result = rowers.nkstuff.rower_nk_token_refresh(self.u)
|
||||||
response = self.c.get('/rowers/workout/nkimport/469',follow=True)
|
response = self.c.get('/rowers/workout/nkimport/469',follow=True)
|
||||||
|
|
||||||
self.assertRedirects(response,
|
self.assertRedirects(response,
|
||||||
expected_url='/rowers/workout/'+encoded2+'/edit/',
|
expected_url='/rowers/workout/'+encoded1+'/edit/',
|
||||||
status_code=301,target_status_code=200)
|
status_code=301,target_status_code=200)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|||||||
@@ -2045,6 +2045,7 @@ def workout_getimportview(request,externalid,source = 'c2'):
|
|||||||
|
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
|
|
||||||
# strokedata not empty - continue
|
# strokedata not empty - continue
|
||||||
id,message = importsources[source].add_workout_from_data(
|
id,message = importsources[source].add_workout_from_data(
|
||||||
request.user,
|
request.user,
|
||||||
|
|||||||
Reference in New Issue
Block a user