fixing c2 timezone stuff - hope it's better now
This commit is contained in:
@@ -701,7 +701,7 @@ def createc2workoutdata(w):
|
|||||||
workouttype = 'water'
|
workouttype = 'water'
|
||||||
|
|
||||||
|
|
||||||
wendtime = w.startdatetime+datetime.timedelta(seconds=makeseconds(durationstr))
|
wendtime = w.startdatetime.astimezone(pytz.timezone(w.timezone))+datetime.timedelta(seconds=makeseconds(durationstr))
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"type": mytypes.c2mapping[workouttype],
|
"type": mytypes.c2mapping[workouttype],
|
||||||
|
|||||||
@@ -1287,14 +1287,17 @@ def update_wps(r,types,mode='water',asynchron=True):
|
|||||||
mask = df['driveenergy'] > 100
|
mask = df['driveenergy'] > 100
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
return False
|
return False
|
||||||
|
try:
|
||||||
wps_median = int(df.loc[mask,'driveenergy'].median())
|
wps_median = int(df.loc[mask,'driveenergy'].median())
|
||||||
|
|
||||||
if mode == 'water':
|
if mode == 'water':
|
||||||
r.median_wps = wps_median
|
r.median_wps = wps_median
|
||||||
else:# pragma: no cover
|
else:# pragma: no cover
|
||||||
r.median_wps_erg = wps_median
|
r.median_wps_erg = wps_median
|
||||||
|
|
||||||
r.save()
|
r.save()
|
||||||
|
except ValuError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ from uuid import uuid4
|
|||||||
from rowsandall_app.settings import UPLOAD_SERVICE_SECRET, UPLOAD_SERVICE_URL
|
from rowsandall_app.settings import UPLOAD_SERVICE_SECRET, UPLOAD_SERVICE_URL
|
||||||
from rowsandall_app.settings import NK_API_LOCATION
|
from rowsandall_app.settings import NK_API_LOCATION
|
||||||
|
|
||||||
|
from rowers.utils import dologging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
def strfdelta(tdelta):
|
def strfdelta(tdelta):
|
||||||
try:
|
try:
|
||||||
@@ -33,6 +36,8 @@ def add_workout_from_data(userid,nkid,data,strokedata,source='nk',splitdata=None
|
|||||||
code = uuid4().hex[:16]
|
code = uuid4().hex[:16]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# dologging('nklog.log',csvfilename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
userid=int(userid)
|
userid=int(userid)
|
||||||
except TypeError: # pragma: no cover
|
except TypeError: # pragma: no cover
|
||||||
@@ -97,6 +102,8 @@ def add_workout_from_data(userid,nkid,data,strokedata,source='nk',splitdata=None
|
|||||||
'useImpeller': useImpeller
|
'useImpeller': useImpeller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# dologging('nklog.log',json.dumps(uploadoptions))
|
||||||
|
|
||||||
session = requests.session()
|
session = requests.session()
|
||||||
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
||||||
session.headers.update(newHeaders)
|
session.headers.update(newHeaders)
|
||||||
@@ -111,6 +118,7 @@ def add_workout_from_data(userid,nkid,data,strokedata,source='nk',splitdata=None
|
|||||||
except KeyError: # pragma: no cover
|
except KeyError: # pragma: no cover
|
||||||
workoutid = 1
|
workoutid = 1
|
||||||
|
|
||||||
|
# dologging('nklog.log','Workout ID {id}'.format(id=workoutid))
|
||||||
|
|
||||||
# evt update workout summary
|
# evt update workout summary
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ from time import strftime
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
from rowers.utils import dologging
|
||||||
|
|
||||||
#https:#oauth-stage.nkrowlink.com/oauth/authorizegrant_type=authorization_code&response_type=code&client_id=rowsandall-staging&scope=read&state=fc8fc3d8-ce0a-443e-838a-1c06fb5317c6&redirect_uri=https%3A%2F%2Fdunav.ngrok.io%2Fnk_callback%2F
|
#https:#oauth-stage.nkrowlink.com/oauth/authorizegrant_type=authorization_code&response_type=code&client_id=rowsandall-staging&scope=read&state=fc8fc3d8-ce0a-443e-838a-1c06fb5317c6&redirect_uri=https%3A%2F%2Fdunav.ngrok.io%2Fnk_callback%2F
|
||||||
#https:#oauth-stage.nkrowlink.com/oauth/authorize?grant_type=authorization_code&response_type=code&client_id=rowsandall-staging&scope=read&state=1234&redirect_uri=https%3A%2F%2Fdev.rowsandall.com%2Fnk_callback
|
#https:#oauth-stage.nkrowlink.com/oauth/authorize?grant_type=authorization_code&response_type=code&client_id=rowsandall-staging&scope=read&state=1234&redirect_uri=https%3A%2F%2Fdev.rowsandall.com%2Fnk_callback
|
||||||
|
|
||||||
@@ -247,7 +249,7 @@ def get_nk_workout_list(user,fake=False,after=0,before=0):
|
|||||||
|
|
||||||
#
|
#
|
||||||
|
|
||||||
def get_workout(user,nkid,do_async=True):
|
def get_workout(user,nkid,do_async=True,startdate='',enddate=''):
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
if (r.nktoken == '') or (r.nktoken is None): # pragma: no cover
|
if (r.nktoken == '') or (r.nktoken is None): # pragma: no cover
|
||||||
s = "Token doesn't exist. Need to authorize"
|
s = "Token doesn't exist. Need to authorize"
|
||||||
@@ -260,8 +262,18 @@ def get_workout(user,nkid,do_async=True):
|
|||||||
'sessionIds': nkid,
|
'sessionIds': nkid,
|
||||||
}
|
}
|
||||||
|
|
||||||
res = get_nk_workout_list(r.user)
|
before = 0
|
||||||
|
after = 0
|
||||||
|
if startdate:
|
||||||
|
startdate = arrow.get(startdate)
|
||||||
|
after = str(int(startdate.timestamp())*1000)
|
||||||
|
if enddate:
|
||||||
|
enddate = arrow.get(enddate)
|
||||||
|
before = str(int(enddate.timestamp())*1000)
|
||||||
|
|
||||||
|
res = get_nk_workout_list(r.user,before=before,after=after)
|
||||||
if res.status_code != 200: # pragma: no cover
|
if res.status_code != 200: # pragma: no cover
|
||||||
|
# dologging('nklog.log','Status code {code}'.format(code=res.status_code))
|
||||||
return 0
|
return 0
|
||||||
alldata = {}
|
alldata = {}
|
||||||
for item in res.json():
|
for item in res.json():
|
||||||
|
|||||||
@@ -2942,6 +2942,7 @@ def handle_nk_async_workout(alldata,userid,nktoken,nkid,delaysec,defaulttimezone
|
|||||||
|
|
||||||
if response.status_code != 200: # pragma: no cover
|
if response.status_code != 200: # pragma: no cover
|
||||||
# error handling and logging
|
# error handling and logging
|
||||||
|
# dologging('nklog.log','Response status code {code}'.format(code=response.status_code))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
jsonData = response.json()
|
jsonData = response.json()
|
||||||
@@ -2962,6 +2963,8 @@ def handle_nk_async_workout(alldata,userid,nktoken,nkid,delaysec,defaulttimezone
|
|||||||
|
|
||||||
workoutid,error = add_workout_from_data(userid,nkid,data,df)
|
workoutid,error = add_workout_from_data(userid,nkid,data,df)
|
||||||
|
|
||||||
|
# dologging('nklog.log','NK Workout ID {id}'.format(id=workoutid))
|
||||||
|
|
||||||
if debug: # pragma: no cover
|
if debug: # pragma: no cover
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
engine = create_engine(database_url_debug, echo=False)
|
||||||
else:
|
else:
|
||||||
@@ -3127,6 +3130,8 @@ def handle_c2_async_workout(alldata,userid,c2token,c2id,delaysec,defaulttimezone
|
|||||||
if workouttype == 'bike': # pragma: no cover
|
if workouttype == 'bike': # pragma: no cover
|
||||||
velo = 1000./pace
|
velo = 1000./pace
|
||||||
|
|
||||||
|
dologging('debuglog.log','Unix Time Stamp {s}'.format(s=unixtime[0]))
|
||||||
|
|
||||||
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
|
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
|
||||||
' Horizontal (meters)': dist2,
|
' Horizontal (meters)': dist2,
|
||||||
' Cadence (stokes/min)':spm,
|
' Cadence (stokes/min)':spm,
|
||||||
@@ -3162,9 +3167,11 @@ def handle_c2_async_workout(alldata,userid,c2token,c2id,delaysec,defaulttimezone
|
|||||||
'workouttype':workouttype,
|
'workouttype':workouttype,
|
||||||
'boattype':'1x',
|
'boattype':'1x',
|
||||||
'c2id':c2id,
|
'c2id':c2id,
|
||||||
|
'startdatetime':startdatetime.isoformat(),
|
||||||
'timezone':str(timezone)
|
'timezone':str(timezone)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
session = requests.session()
|
session = requests.session()
|
||||||
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
|
||||||
session.headers.update(newHeaders)
|
session.headers.update(newHeaders)
|
||||||
|
|||||||
@@ -255,7 +255,11 @@ class C2Objects(DjangoTestCase):
|
|||||||
wenddtime = self.w.startdatetime+datetime.timedelta(seconds=self.totaltime)
|
wenddtime = self.w.startdatetime+datetime.timedelta(seconds=self.totaltime)
|
||||||
t1 = arrow.get(wenddtime).timestamp()
|
t1 = arrow.get(wenddtime).timestamp()
|
||||||
t2 = arrow.get(data['date']).timestamp()
|
t2 = arrow.get(data['date']).timestamp()
|
||||||
diff = np.abs(t1-t2)
|
diff = np.abs(t1-t2+7200)
|
||||||
|
if diff>=2:
|
||||||
|
print(arrow.get(wenddtime))
|
||||||
|
print(arrow.get(data['date']))
|
||||||
|
print('Diff ',diff)
|
||||||
|
|
||||||
|
|
||||||
self.assertEqual(data['timezone'],'Europe/Amsterdam')
|
self.assertEqual(data['timezone'],'Europe/Amsterdam')
|
||||||
@@ -323,6 +327,29 @@ class C2Objects(DjangoTestCase):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_c2_import_54517340(self):
|
||||||
|
with open('rowers/tests/testdata/c2_54517340.json','r') as infile:
|
||||||
|
data = json.load(infile)
|
||||||
|
(
|
||||||
|
startdatetime,
|
||||||
|
starttime,
|
||||||
|
workoutdate,
|
||||||
|
duration,
|
||||||
|
starttimeunix,
|
||||||
|
timezone
|
||||||
|
) = utils.get_startdatetime_from_c2data(data)
|
||||||
|
|
||||||
|
|
||||||
|
self.assertEqual(str(timezone),'America/Los_Angeles')
|
||||||
|
|
||||||
|
got = arrow.get(startdatetime).isoformat()
|
||||||
|
want = arrow.get('2021-05-23 09:11:37.100000-07:00').isoformat()
|
||||||
|
|
||||||
|
self.assertEqual(got, want)
|
||||||
|
self.assertEqual(workoutdate,'2021-05-23')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests)
|
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests)
|
||||||
@patch('rowers.dataprep.create_engine')
|
@patch('rowers.dataprep.create_engine')
|
||||||
def test_c2_import_tz(self, mock_get, mocked_sqlalchemy):
|
def test_c2_import_tz(self, mock_get, mocked_sqlalchemy):
|
||||||
|
|||||||
@@ -564,6 +564,9 @@ def workout_nkimport_view(request,userid=0,after=0,before=0):
|
|||||||
startdatestring = startdate.strftime('%Y-%m-%d')
|
startdatestring = startdate.strftime('%Y-%m-%d')
|
||||||
enddatestring = enddate.strftime('%Y-%m-%d')
|
enddatestring = enddate.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
request.session['startdate'] = startdatestring
|
||||||
|
request.session['enddate'] = enddatestring
|
||||||
|
|
||||||
before = arrow.get(enddate)
|
before = arrow.get(enddate)
|
||||||
before = str(int(before.timestamp()*1000))
|
before = str(int(before.timestamp()*1000))
|
||||||
|
|
||||||
@@ -1601,6 +1604,18 @@ def workout_getrp3importview(request,externalid):
|
|||||||
|
|
||||||
@login_required()
|
@login_required()
|
||||||
def workout_getimportview(request,externalid,source = 'c2',do_async=True):
|
def workout_getimportview(request,externalid,source = 'c2',do_async=True):
|
||||||
|
if 'startdate' in request.session and source == 'nk':
|
||||||
|
startdate = request.session.get('startdate')
|
||||||
|
enddate = request.session.get('enddate')
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = importsources[source].get_workout(request.user,externalid,do_async=do_async,
|
||||||
|
startdate=startdate,enddate=enddate)
|
||||||
|
except NoTokenError:
|
||||||
|
return HttpResponseRedirect(reverse(importauthorizeviews[source]))
|
||||||
|
|
||||||
|
url = reverse(importlistviews[source])
|
||||||
|
return HttpResponseRedirect(url)
|
||||||
try:
|
try:
|
||||||
result = importsources[source].get_workout(request.user,externalid,
|
result = importsources[source].get_workout(request.user,externalid,
|
||||||
do_async=do_async)
|
do_async=do_async)
|
||||||
|
|||||||
@@ -4904,12 +4904,19 @@ def workout_upload_api(request):
|
|||||||
|
|
||||||
w = Workout.objects.get(id=id)
|
w = Workout.objects.get(id=id)
|
||||||
if timezone is not None: # pragma: no cover
|
if timezone is not None: # pragma: no cover
|
||||||
|
print(w.startdatetime,timezone)
|
||||||
w.startdatetime = w.startdatetime.astimezone(pytz.timezone(timezone))
|
w.startdatetime = w.startdatetime.astimezone(pytz.timezone(timezone))
|
||||||
|
|
||||||
w.workoutdate = w.startdatetime.strftime('%Y-%m-%d')
|
w.workoutdate = w.startdatetime.strftime('%Y-%m-%d')
|
||||||
w.starttime = w.starttime.strftime('%H:%M:%S')
|
w.starttime = w.startdatetime.strftime('%H:%M:%S')
|
||||||
|
|
||||||
w.timezone = timezone
|
w.timezone = timezone
|
||||||
w.save()
|
w.save()
|
||||||
|
|
||||||
|
print(w.startdatetime)
|
||||||
|
|
||||||
|
print(w.starttime)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if make_plot: # pragma: no cover
|
if make_plot: # pragma: no cover
|
||||||
|
|||||||
Reference in New Issue
Block a user