fixing c2 timezone issue when timezone is unknown
This commit is contained in:
@@ -22,6 +22,8 @@ import json
|
|||||||
from scipy import optimize
|
from scipy import optimize
|
||||||
from json.decoder import JSONDecodeError
|
from json.decoder import JSONDecodeError
|
||||||
|
|
||||||
|
from pytz.exceptions import UnknownTimeZoneError
|
||||||
|
|
||||||
from rowsandall_app.settings import (
|
from rowsandall_app.settings import (
|
||||||
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
|
||||||
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET
|
UPLOAD_SERVICE_URL, UPLOAD_SERVICE_SECRET
|
||||||
@@ -1079,8 +1081,10 @@ def add_workout_from_data(user,importid,data,strokedata,
|
|||||||
except: # pragma: no cover
|
except: # pragma: no cover
|
||||||
comments = ' '
|
comments = ' '
|
||||||
|
|
||||||
|
try:
|
||||||
thetimezone = pytz.timezone(data['timezone'])
|
thetimezone = pytz.timezone(data['timezone'])
|
||||||
|
except UnknownTimeZoneError:
|
||||||
|
thetimezone = 'UTC'
|
||||||
|
|
||||||
|
|
||||||
r = Rower.objects.get(user=user)
|
r = Rower.objects.get(user=user)
|
||||||
@@ -1228,7 +1232,10 @@ def add_workout_from_data(user,importid,data,strokedata,
|
|||||||
|
|
||||||
w = Workout.objects.get(id=id)
|
w = Workout.objects.get(id=id)
|
||||||
|
|
||||||
local_tz = pytz.timezone(data['timezone'])
|
try:
|
||||||
|
local_tz = pytz.timezone(data['timezone'])
|
||||||
|
except UnknownTimeZoneError:
|
||||||
|
local_tz = pytz.utc
|
||||||
# local_tz = pytz.timezone(thetimezone)
|
# local_tz = pytz.timezone(thetimezone)
|
||||||
|
|
||||||
w.startdatetime = w.startdatetime.astimezone(local_tz)
|
w.startdatetime = w.startdatetime.astimezone(local_tz)
|
||||||
|
|||||||
@@ -719,6 +719,8 @@ def mocked_requests(*args, **kwargs):
|
|||||||
with open('rowers/tests/testdata/c2_timezone2.json','r') as infile:
|
with open('rowers/tests/testdata/c2_timezone2.json','r') as infile:
|
||||||
c2timezoneworkoutdata2 = json.load(infile)
|
c2timezoneworkoutdata2 = json.load(infile)
|
||||||
|
|
||||||
|
with open('rowers/tests/testdata/c2_timezonebad.json','r') as infile:
|
||||||
|
c2timezoneworkoutdatabad = json.load(infile)
|
||||||
|
|
||||||
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)
|
||||||
@@ -1192,6 +1194,8 @@ def mocked_requests(*args, **kwargs):
|
|||||||
return MockResponse(c2workoutdata,200)
|
return MockResponse(c2workoutdata,200)
|
||||||
elif '31' in args[0]:
|
elif '31' in args[0]:
|
||||||
return MockResponse(c2timezoneworkoutdata2,200)
|
return MockResponse(c2timezoneworkoutdata2,200)
|
||||||
|
elif '32' in args[0]:
|
||||||
|
return MockResponse(c2timezoneworkoutdatabad,200)
|
||||||
else:
|
else:
|
||||||
return MockResponse(c2timezoneworkoutdata,200)
|
return MockResponse(c2timezoneworkoutdata,200)
|
||||||
elif c2workoutlisttester.match(args[0]):
|
elif c2workoutlisttester.match(args[0]):
|
||||||
|
|||||||
@@ -277,6 +277,22 @@ class C2Objects(DjangoTestCase):
|
|||||||
w = Workout.objects.get(id=2)
|
w = Workout.objects.get(id=2)
|
||||||
self.assertEqual(w.timezone,'Europe/Prague')
|
self.assertEqual(w.timezone,'Europe/Prague')
|
||||||
|
|
||||||
|
|
||||||
|
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests)
|
||||||
|
@patch('rowers.dataprep.create_engine')
|
||||||
|
def test_c2_import_tz3(self, mock_get, mocked_sqlalchemy):
|
||||||
|
|
||||||
|
response = self.c.get('/rowers/workout/c2import/32/',follow=True)
|
||||||
|
|
||||||
|
self.assertRedirects(response,
|
||||||
|
expected_url='/rowers/workout/'+encoded2+'/edit/',
|
||||||
|
status_code=302,target_status_code=200)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
w = Workout.objects.get(id=2)
|
||||||
|
self.assertEqual(w.timezone,'UTC')
|
||||||
|
|
||||||
@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_tz2(self, mock_get, mocked_sqlalchemy):
|
def test_c2_import_tz2(self, mock_get, mocked_sqlalchemy):
|
||||||
|
|||||||
Reference in New Issue
Block a user