No longer raise an error if we fail to generate localtime.

Now, we log the error and return None
This commit is contained in:
mike cullerton 2024-09-12 08:57:33 -04:00
parent ba003f3680
commit 860007bfab
2 changed files with 35 additions and 3 deletions

View File

@ -1,3 +1,4 @@
from crc import app
from crc.api.common import ApiError from crc.api.common import ApiError
from crc.scripts.script import Script from crc.scripts.script import Script
@ -31,7 +32,16 @@ class GetLocaltime(Script):
timezone = 'US/Eastern' timezone = 'US/Eastern'
# with Python 3.9, not passing the timezone resuls in a PytzUsageWarning usage warning. # with Python 3.9, not passing the timezone resuls in a PytzUsageWarning usage warning.
parsed_timestamp = dateparser.parse(timestamp, settings={'TIMEZONE': 'UTC'}) parsed_timestamp = dateparser.parse(timestamp, settings={'TIMEZONE': 'UTC'})
localtime = parsed_timestamp.astimezone(pytz.timezone(timezone)) try:
localtime = parsed_timestamp.astimezone(pytz.timezone(timezone))
except AttributeError as ae:
# In general, we want this script to succeed. It is called during the document assembly process.
# We want to generate the zip file and submit to the IRB, even if it has minor errors
app.logger.info(f'Could not convert the timestamp to a localtime. Original error: {ae}')
localtime = None
# TODO: When we fix the frontend to no display errors on production, we can raise an error here.
# raise ApiError(code='invalid_date_or_timestamp',
# message=f'We could not process the timestamp into a localtime. Original error: {ae}')
return localtime return localtime
else: else:

View File

@ -1,13 +1,15 @@
from tests.base_test import BaseTest from tests.base_test import BaseTest
from crc.api.common import ApiError
from crc.scripts.get_localtime import GetLocaltime from crc.scripts.get_localtime import GetLocaltime
import dateparser import dateparser
import datetime import datetime
from unittest.mock import patch
class TestGetLocaltime(BaseTest): class TestGetLocaltime(BaseTest):
def test_get_localtime(self): def test_get_localtime(self):
timestamp = datetime.datetime.utcnow() timestamp = datetime.datetime.now(datetime.timezone.utc)
workflow = self.create_workflow('get_localtime') workflow = self.create_workflow('get_localtime')
workflow_api = self.get_workflow_api(workflow) workflow_api = self.get_workflow_api(workflow)
@ -27,7 +29,7 @@ class TestGetLocaltime(BaseTest):
def test_get_localtime_with_timezone(self): def test_get_localtime_with_timezone(self):
timestamp = datetime.datetime.utcnow() timestamp = datetime.datetime.now(datetime.timezone.utc)
workflow = self.create_workflow('get_localtime') workflow = self.create_workflow('get_localtime')
workflow_api = self.get_workflow_api(workflow) workflow_api = self.get_workflow_api(workflow)
@ -53,3 +55,23 @@ class TestGetLocaltime(BaseTest):
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
self.complete_form(workflow, task, {'with_timestamp': False, 'with_timezone': False}) self.complete_form(workflow, task, {'with_timestamp': False, 'with_timezone': False})
@patch('dateparser.parse') # mock_timestamp
def test_get_localtime_bad_timestamp(self, mock_timestamp):
# If we have a bad timestamp, we want the script to run, but return None
timestamp = datetime.datetime.now(datetime.timezone.utc)
mock_timestamp.return_value = None
workflow = self.create_workflow('get_localtime')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
workflow_api = self.complete_form(workflow, task, {'with_timestamp': True,
'with_timezone': True,
'timestamp': str(timestamp),
'timezone': 'US/Eastern'})
next_task = workflow_api.next_task
localtime_with = next_task.data['localtime_with']
localtime_without = next_task.data['localtime_without']
self.assertIsNone(localtime_with)
self.assertIsNone(localtime_without)