starting to work on nkstuff.get_workout
This commit is contained in:
@@ -116,7 +116,6 @@ def do_refresh_token(refreshtoken):
|
|||||||
url = oauth_data['base_url']
|
url = oauth_data['base_url']
|
||||||
|
|
||||||
response = requests.post(url,data=post_data,auth=HTTPBasicAuth(oauth_data['client_id'],oauth_data['client_secret']))
|
response = requests.post(url,data=post_data,auth=HTTPBasicAuth(oauth_data['client_id'],oauth_data['client_secret']))
|
||||||
print(response.status_code)
|
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return [0,0,0]
|
return [0,0,0]
|
||||||
@@ -150,7 +149,7 @@ def make_authorization_url(request):
|
|||||||
|
|
||||||
def get_nk_workout_list(user,fake=False):
|
def get_nk_workout_list(user,fake=False):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
print(r.nktoken,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"
|
||||||
return custom_exception_handler(401,s)
|
return custom_exception_handler(401,s)
|
||||||
@@ -181,24 +180,54 @@ def get_nk_workout_list(user,fake=False):
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def get_nk_workout_strokes(user,nkid):
|
|
||||||
r = Rower.objects.get(user=user)
|
|
||||||
|
|
||||||
if (r.nktoken == '') or (r.nktoken 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:
|
|
||||||
# ready to fetch. Hurray
|
|
||||||
authorizationstring = str('Bearer ' + r.nktoken)
|
|
||||||
headers = {'Authorization': authorizationstring,
|
|
||||||
'user-agent': 'sanderroosendaal',
|
|
||||||
'Content-Type': 'application/json'}
|
|
||||||
url = "https://log.concept2.com/api/users/me/results/"+str(nkid)+"/strokes"
|
|
||||||
s = requests.get(url,headers=headers)
|
|
||||||
|
|
||||||
return s
|
|
||||||
#
|
#
|
||||||
#def get_workout(user,nkid):
|
#def get_workout(user,nkid):
|
||||||
|
def getdict(x):
|
||||||
|
try:
|
||||||
|
return x[0]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_workout(user,nkid):
|
||||||
|
print('aap')
|
||||||
|
r = Rower.objects.get(user=user)
|
||||||
|
if (r.nktoken == '') or (r.nktoken is None):
|
||||||
|
s = "Token doesn't exist. Need to authorize"
|
||||||
|
return custom_exception_handler(401,s) ,0
|
||||||
|
elif (timezone.now()>r.nktokenexpirydate):
|
||||||
|
s = "Token expired. Needs to refresh."
|
||||||
|
return custom_exception_handler(401,s),0
|
||||||
|
|
||||||
|
params = {
|
||||||
|
'sessionIds': nkid,
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizationstring = str('Bearer ' + r.nktoken)
|
||||||
|
headers = {'Authorization': authorizationstring,
|
||||||
|
'user-agent': 'sanderroosendaal',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# get strokes
|
||||||
|
url = NK_API_LOCATION+"api/v1/sessions/strokes"
|
||||||
|
response = requests.get(url,headers=headers,params=params)
|
||||||
|
if response != 200:
|
||||||
|
# error handling
|
||||||
|
pass
|
||||||
|
|
||||||
|
data = response.json()[str(nkid)]
|
||||||
|
|
||||||
|
df = pd.DataFrame.from_dict(data)
|
||||||
|
ol = df['oarlockStrokes']
|
||||||
|
ol = ol.apply(lambda x:getdict(x))
|
||||||
|
df2 = pd.DataFrame.from_records(ol.values)
|
||||||
|
df2 = df2.rename(colunms={"timestamp":"ts"})
|
||||||
|
df = pd.concat([df,df2],axis=1)
|
||||||
|
|
||||||
|
# get workout data
|
||||||
|
|
||||||
|
return 0,0
|
||||||
|
|||||||
@@ -593,6 +593,12 @@ def mocked_requests(*args, **kwargs):
|
|||||||
with open('rowers/tests/testdata/c2jsonstrokedata.txt','r') as infile:
|
with open('rowers/tests/testdata/c2jsonstrokedata.txt','r') as infile:
|
||||||
c2strokedata = json.load(infile)
|
c2strokedata = json.load(infile)
|
||||||
|
|
||||||
|
with open('rowers/tests/testdata/nk_strokes.json','r') as infile:
|
||||||
|
nkstrokedata = json.load(infile)
|
||||||
|
|
||||||
|
with open('rowers/tests/testdata/nk_list.json','r') as infile:
|
||||||
|
nkworkoutlist = json.load(infile)
|
||||||
|
|
||||||
polar_json = {
|
polar_json = {
|
||||||
'available-user-data': []
|
'available-user-data': []
|
||||||
}
|
}
|
||||||
@@ -621,7 +627,7 @@ def mocked_requests(*args, **kwargs):
|
|||||||
stravaworkoutlist = json.load(open('rowers/tests/testdata/stravaworkoutlist.txt'))
|
stravaworkoutlist = json.load(open('rowers/tests/testdata/stravaworkoutlist.txt'))
|
||||||
sporttracksworkoutlist = json.load(open('rowers/tests/testdata/sporttracksworkouts.txt'))
|
sporttracksworkoutlist = json.load(open('rowers/tests/testdata/sporttracksworkouts.txt'))
|
||||||
|
|
||||||
nkworkoutlist = json.load(open('rowers/tests/testdata/nkworkouts.txt'))
|
#nkworkoutlist = json.load(open('rowers/tests/testdata/nkworkouts.txt'))
|
||||||
|
|
||||||
rkworkoutlistjson = json.load(open('rowers/tests/testdata/rkworkoutslist.txt','r'))
|
rkworkoutlistjson = json.load(open('rowers/tests/testdata/rkworkoutslist.txt','r'))
|
||||||
uaworkoutlistjson = json.load(open('rowers/tests/testdata/uaworkoutlist.txt','r'))
|
uaworkoutlistjson = json.load(open('rowers/tests/testdata/uaworkoutlist.txt','r'))
|
||||||
@@ -752,6 +758,9 @@ def mocked_requests(*args, **kwargs):
|
|||||||
nkworkoutlistregex = '.*?nkrowlink\.com\/api\/v1\/sessions'
|
nkworkoutlistregex = '.*?nkrowlink\.com\/api\/v1\/sessions'
|
||||||
nkworkoutlisttester = re.compile(nkworkoutlistregex)
|
nkworkoutlisttester = re.compile(nkworkoutlistregex)
|
||||||
|
|
||||||
|
nkstrokesregex = '.*?nkrowlink\.com\/api\/v1\/strokes\?sessionIds=\d'
|
||||||
|
nkstrokestester = re.compile(nkstrokesregex)
|
||||||
|
|
||||||
stravaathleteregex = '.*?strava\.com\/api\/v3\/athlete$'
|
stravaathleteregex = '.*?strava\.com\/api\/v3\/athlete$'
|
||||||
stravaathletetester = re.compile(stravaathleteregex)
|
stravaathletetester = re.compile(stravaathleteregex)
|
||||||
|
|
||||||
@@ -925,17 +934,11 @@ def mocked_requests(*args, **kwargs):
|
|||||||
'refresh_token': 'jHJhFzCfOOKB8oyiayubhLAlxaMkG3ruC1E8YxaR'
|
'refresh_token': 'jHJhFzCfOOKB8oyiayubhLAlxaMkG3ruC1E8YxaR'
|
||||||
}
|
}
|
||||||
return MockResponse(json_data,200)
|
return MockResponse(json_data,200)
|
||||||
|
if nkstrokestester.match(args[0]):
|
||||||
|
return MockResponse(nkstrokedata,200)
|
||||||
if nkworkoutlisttester.match(args[0]):
|
if nkworkoutlisttester.match(args[0]):
|
||||||
return MockResponse(nkworkoutlist,200)
|
return MockResponse(nkworkoutlist,200)
|
||||||
elif 'token' in args[0]:
|
|
||||||
json_data = {
|
|
||||||
"token_type": "Bearer",
|
|
||||||
"access_token": "987654321234567898765432123456789",
|
|
||||||
"refresh_token": "1234567898765432112345678987654321",
|
|
||||||
"expires_at": arrow.now().timestamp()+7200
|
|
||||||
}
|
|
||||||
|
|
||||||
return MockResponse(json_data,200)
|
|
||||||
|
|
||||||
|
|
||||||
if stravatester.match(args[0]):
|
if stravatester.match(args[0]):
|
||||||
|
|||||||
@@ -424,6 +424,21 @@ class NKObjects(DjangoTestCase):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code,200)
|
self.assertEqual(response.status_code,200)
|
||||||
|
|
||||||
|
@patch('rowers.nkstuff.requests.get', side_effect=mocked_requests)
|
||||||
|
@patch('rowers.nkstuff.requests.post', side_effect=mocked_requests)
|
||||||
|
@patch('rowers.dataprep.getsmallrowdata_db')
|
||||||
|
def test_nk_import(self, mock_get, mock_post,
|
||||||
|
mocked_getsmallrowdata_db):
|
||||||
|
|
||||||
|
result = rowers.nkstuff.rower_nk_token_refresh(self.u)
|
||||||
|
response = self.c.get('/rowers/workout/nkimport/12',follow=True)
|
||||||
|
|
||||||
|
self.assertRedirects(response,
|
||||||
|
expected_url='/rowers/workout/'+encoded2+'/edit/',
|
||||||
|
status_code=301,target_status_code=200)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#@pytest.mark.django_db
|
#@pytest.mark.django_db
|
||||||
|
|||||||
@@ -1931,7 +1931,8 @@ importsources = {
|
|||||||
'runkeeper':runkeeperstuff,
|
'runkeeper':runkeeperstuff,
|
||||||
'sporttracks':sporttracksstuff,
|
'sporttracks':sporttracksstuff,
|
||||||
'trainingpeaks':tpstuff,
|
'trainingpeaks':tpstuff,
|
||||||
'underarmour':underarmourstuff
|
'underarmour':underarmourstuff,
|
||||||
|
'nk':nkstuff,
|
||||||
}
|
}
|
||||||
|
|
||||||
@login_required()
|
@login_required()
|
||||||
@@ -1960,7 +1961,7 @@ def workout_getrp3importview(request,externalid):
|
|||||||
def workout_getimportview(request,externalid,source = 'c2'):
|
def workout_getimportview(request,externalid,source = 'c2'):
|
||||||
data,strokedata = importsources[source].get_workout(request.user,externalid)
|
data,strokedata = importsources[source].get_workout(request.user,externalid)
|
||||||
if not data:
|
if not data:
|
||||||
messages.error(request,res[1])
|
messages.error(request,"No strokedata received")
|
||||||
url = reverse('workouts_view')
|
url = reverse('workouts_view')
|
||||||
|
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user