From 866e6bb36c62c08f6e17f0de951672e0cc25e034 Mon Sep 17 00:00:00 2001 From: Brian Beggs Date: Mon, 25 Nov 2024 15:34:18 -0500 Subject: [PATCH] chore: Consolidate timestamp format strings. --- enterprise_catalog/apps/api/v1/views/catalog_csv.py | 3 ++- enterprise_catalog/apps/api/v1/views/catalog_workbook.py | 3 ++- enterprise_catalog/apps/catalog/constants.py | 2 ++ enterprise_catalog/apps/catalog/tests/factories.py | 3 ++- enterprise_catalog/apps/catalog/tests/test_algolia_utils.py | 3 ++- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/enterprise_catalog/apps/api/v1/views/catalog_csv.py b/enterprise_catalog/apps/api/v1/views/catalog_csv.py index 9825de8d4..556fbf983 100644 --- a/enterprise_catalog/apps/api/v1/views/catalog_csv.py +++ b/enterprise_catalog/apps/api/v1/views/catalog_csv.py @@ -13,6 +13,7 @@ from enterprise_catalog.apps.catalog.algolia_utils import ( get_initialized_algolia_client, ) +from enterprise_catalog.apps.catalog.constants import TIMESTAMP_FORMAT # CatalogCsvView's StreamingHttpResponse requires a File-like class that has a 'write' method @@ -110,7 +111,7 @@ def get(self, request, **kwargs): if invalid_facets: return Response(f'Error: invalid facet(s): {invalid_facets} provided.', status=HTTP_400_BAD_REQUEST) - filename = f'Enterprise-Catalog-Export-{time.strftime("%Y%m%d%H%M%S")}.csv' + filename = f'Enterprise-Catalog-Export-{time.strftime(TIMESTAMP_FORMAT)}.csv' response = StreamingHttpResponse( streaming_content=(self.iter_items(facets, algoliaQuery)), diff --git a/enterprise_catalog/apps/api/v1/views/catalog_workbook.py b/enterprise_catalog/apps/api/v1/views/catalog_workbook.py index a54cc44b5..9d2bb210d 100644 --- a/enterprise_catalog/apps/api/v1/views/catalog_workbook.py +++ b/enterprise_catalog/apps/api/v1/views/catalog_workbook.py @@ -13,6 +13,7 @@ from enterprise_catalog.apps.catalog.algolia_utils import ( get_initialized_algolia_client, ) +from enterprise_catalog.apps.catalog.constants import TIMESTAMP_FORMAT logger = logging.getLogger(__name__) @@ -143,7 +144,7 @@ def get(self, request, **kwargs): output, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) - filename = f'Enterprise-Catalog-Export-{time.strftime("%Y%m%d%H%M%S")}.xlsx' + filename = f'Enterprise-Catalog-Export-{time.strftime(TIMESTAMP_FORMAT)}.xlsx' response['Content-Disposition'] = f'attachment; filename={filename}' return response diff --git a/enterprise_catalog/apps/catalog/constants.py b/enterprise_catalog/apps/catalog/constants.py index 58ed6e795..08ee11198 100644 --- a/enterprise_catalog/apps/catalog/constants.py +++ b/enterprise_catalog/apps/catalog/constants.py @@ -6,6 +6,8 @@ ) +TIMESTAMP_FORMAT = '%Y%m%d%H%M%SZ' + # Algolia timestamp default ALGOLIA_DEFAULT_TIMESTAMP = (datetime(3000, 1, 1)).timestamp() diff --git a/enterprise_catalog/apps/catalog/tests/factories.py b/enterprise_catalog/apps/catalog/tests/factories.py index 45fab4b40..740a7c66b 100644 --- a/enterprise_catalog/apps/catalog/tests/factories.py +++ b/enterprise_catalog/apps/catalog/tests/factories.py @@ -10,6 +10,7 @@ COURSE_RUN, LEARNER_PATHWAY, PROGRAM, + TIMESTAMP_FORMAT, json_serialized_course_modes, ) from enterprise_catalog.apps.catalog.models import ( @@ -180,7 +181,7 @@ def _json_metadata(self): 'overview': 'Pathway for a data engineer.', 'published': True, 'visible_via_association': True, - 'created': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'), + 'created': datetime.datetime.utcnow().strftime(TIMESTAMP_FORMAT), 'card_image': { 'card': { 'url': self.card_image_url, diff --git a/enterprise_catalog/apps/catalog/tests/test_algolia_utils.py b/enterprise_catalog/apps/catalog/tests/test_algolia_utils.py index 81ab45233..99a4cf6e6 100644 --- a/enterprise_catalog/apps/catalog/tests/test_algolia_utils.py +++ b/enterprise_catalog/apps/catalog/tests/test_algolia_utils.py @@ -15,6 +15,7 @@ LEARNER_PATHWAY, PROGRAM, RESTRICTION_FOR_B2B, + TIMESTAMP_FORMAT, ) from enterprise_catalog.apps.catalog.tests.factories import ( ContentMetadataFactory, @@ -32,7 +33,7 @@ def _days_from_now(days=0): deadline = localized_utcnow() + timedelta(days=days) - return deadline.strftime('%Y-%m-%dT%H:%M:%SZ') + return deadline.strftime(TIMESTAMP_FORMAT) def _get_from_cache(days):