From 5ed76f0117ff8fe6522de9fc9b92ca291a295d37 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 | 4 +++- enterprise_catalog/apps/api/v1/views/catalog_workbook.py | 3 ++- enterprise_catalog/apps/catalog/constants.py | 1 + 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..b5c3931c5 100644 --- a/enterprise_catalog/apps/api/v1/views/catalog_csv.py +++ b/enterprise_catalog/apps/api/v1/views/catalog_csv.py @@ -2,6 +2,8 @@ import csv import time +from enterprise_catalog.apps.catalog.constants import TIMESTAMP_FORMAT + from django.http import StreamingHttpResponse from rest_framework.decorators import action from rest_framework.generics import GenericAPIView @@ -110,7 +112,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..ae6ec2039 100644 --- a/enterprise_catalog/apps/catalog/constants.py +++ b/enterprise_catalog/apps/catalog/constants.py @@ -5,6 +5,7 @@ DISABLE_MODEL_ADMIN_CHANGES_SWITCH, ) +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..421a1d925 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, + t, ) 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(TIMESTMAP_FORMAT) def _get_from_cache(days):