From 5e9790124bc3f2cea762f7bff082b38dc2456c87 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 3 Apr 2025 18:10:12 -0700 Subject: [PATCH] Just curious --- src/documents/filters.py | 17 +++++++++---- src/documents/index.py | 3 ++- .../migrations/1066_alter_document_created.py | 24 +++++++++++++++++++ src/documents/models.py | 12 ++++++---- src/documents/serialisers.py | 14 +++++------ src/documents/signals/handlers.py | 8 +++---- src/documents/templating/filepath.py | 16 ++++++------- src/documents/tests/test_api_documents.py | 2 +- src/documents/tests/test_consumer.py | 18 +++----------- src/documents/tests/test_file_handling.py | 2 +- 10 files changed, 69 insertions(+), 47 deletions(-) create mode 100644 src/documents/migrations/1066_alter_document_created.py diff --git a/src/documents/filters.py b/src/documents/filters.py index 90161a1e6..f8b0799c7 100644 --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -48,6 +48,15 @@ CHAR_KWARGS = ["istartswith", "iendswith", "icontains", "iexact"] ID_KWARGS = ["in", "exact"] INT_KWARGS = ["exact", "gt", "gte", "lt", "lte", "isnull"] DATE_KWARGS = [ + "year", + "month", + "day", + "gt", + "gte", + "lt", + "lte", +] +DATETIME_KWARGS = [ "year", "month", "day", @@ -739,8 +748,8 @@ class DocumentFilterSet(FilterSet): "content": CHAR_KWARGS, "archive_serial_number": INT_KWARGS, "created": DATE_KWARGS, - "added": DATE_KWARGS, - "modified": DATE_KWARGS, + "added": DATETIME_KWARGS, + "modified": DATETIME_KWARGS, "original_filename": CHAR_KWARGS, "checksum": CHAR_KWARGS, "correspondent": ["isnull"], @@ -764,8 +773,8 @@ class ShareLinkFilterSet(FilterSet): class Meta: model = ShareLink fields = { - "created": DATE_KWARGS, - "expiration": DATE_KWARGS, + "created": DATETIME_KWARGS, + "expiration": DATETIME_KWARGS, } diff --git a/src/documents/index.py b/src/documents/index.py index 9b3a1724c..3a2b2cb58 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -5,6 +5,7 @@ import math from collections import Counter from contextlib import contextmanager from datetime import datetime +from datetime import time from datetime import timezone from shutil import rmtree from typing import TYPE_CHECKING @@ -168,7 +169,7 @@ def update_document(writer: AsyncWriter, doc: Document) -> None: type=doc.document_type.name if doc.document_type else None, type_id=doc.document_type.id if doc.document_type else None, has_type=doc.document_type is not None, - created=doc.created, + created=datetime.combine(doc.created, time.min), added=doc.added, asn=asn, modified=doc.modified, diff --git a/src/documents/migrations/1066_alter_document_created.py b/src/documents/migrations/1066_alter_document_created.py new file mode 100644 index 000000000..28725cf76 --- /dev/null +++ b/src/documents/migrations/1066_alter_document_created.py @@ -0,0 +1,24 @@ +# Generated by Django 5.1.7 on 2025-04-04 01:08 + +import datetime + +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + dependencies = [ + ("documents", "1065_workflowaction_assign_custom_fields_values"), + ] + + operations = [ + migrations.AlterField( + model_name="document", + name="created", + field=models.DateField( + db_index=True, + default=datetime.date(2025, 4, 4), + verbose_name="created", + ), + ), + ] diff --git a/src/documents/models.py b/src/documents/models.py index 4b3f97e50..a4fc10028 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -1,4 +1,3 @@ -import datetime from pathlib import Path from typing import Final @@ -213,7 +212,11 @@ class Document(SoftDeleteModel, ModelWithOwner): ), ) - created = models.DateTimeField(_("created"), default=timezone.now, db_index=True) + created = models.DateField( + _("created"), + default=timezone.now().date(), + db_index=True, + ) modified = models.DateTimeField( _("modified"), @@ -291,8 +294,7 @@ class Document(SoftDeleteModel, ModelWithOwner): verbose_name_plural = _("documents") def __str__(self) -> str: - # Convert UTC database time to local time - created = datetime.date.isoformat(timezone.localdate(self.created)) + created = self.created.isoformat() res = f"{created}" @@ -371,7 +373,7 @@ class Document(SoftDeleteModel, ModelWithOwner): @property def created_date(self): - return timezone.localdate(self.created) + return self.created class SavedView(ModelWithOwner): diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 782f4d6c8..1d0899ae2 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1,10 +1,9 @@ from __future__ import annotations -import datetime import logging import math import re -import zoneinfo +from datetime import datetime from decimal import Decimal from typing import TYPE_CHECKING @@ -952,11 +951,7 @@ class DocumentSerializer( def update(self, instance: Document, validated_data): if "created_date" in validated_data and "created" not in validated_data: - new_datetime = datetime.datetime.combine( - validated_data.get("created_date"), - datetime.time(0, 0, 0, 0, zoneinfo.ZoneInfo(settings.TIME_ZONE)), - ) - instance.created = new_datetime + instance.created = validated_data.get("created_date") instance.save() if "created_date" in validated_data: validated_data.pop("created_date") @@ -1632,6 +1627,11 @@ class PostDocumentSerializer(serializers.Serializer): else: return None + def validate_created(self, created): + # support datetime format for created for backwards compatibility + if isinstance(created, datetime): + return created.date() + class BulkDownloadSerializer(DocumentListSerializer): content = serializers.ChoiceField( diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 21ea76836..0e0860c3b 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -995,7 +995,7 @@ def run_workflows( filename = document.original_filename or "" current_filename = document.filename or "" added = timezone.localtime(document.added) - created = timezone.localtime(document.created) + created = document.created else: title = overrides.title if overrides.title else str(document.original_file) doc_url = "" @@ -1017,7 +1017,7 @@ def run_workflows( filename = document.original_file if document.original_file else "" current_filename = filename added = timezone.localtime(timezone.now()) - created = timezone.localtime(overrides.created) + created = overrides.created subject = ( parse_w_workflow_placeholders( @@ -1083,7 +1083,7 @@ def run_workflows( filename = document.original_filename or "" current_filename = document.filename or "" added = timezone.localtime(document.added) - created = timezone.localtime(document.created) + created = document.created else: title = overrides.title if overrides.title else str(document.original_file) doc_url = "" @@ -1105,7 +1105,7 @@ def run_workflows( filename = document.original_file if document.original_file else "" current_filename = filename added = timezone.localtime(timezone.now()) - created = timezone.localtime(overrides.created) + created = overrides.created try: data = {} diff --git a/src/documents/templating/filepath.py b/src/documents/templating/filepath.py index 45e1cad9e..633a85cc8 100644 --- a/src/documents/templating/filepath.py +++ b/src/documents/templating/filepath.py @@ -137,16 +137,14 @@ def get_creation_date_context(document: Document) -> dict[str, str]: Given a Document, localizes the creation date and builds a context dictionary with some common, shorthand formatted values from it """ - local_created = timezone.localdate(document.created) - return { - "created": local_created.isoformat(), - "created_year": local_created.strftime("%Y"), - "created_year_short": local_created.strftime("%y"), - "created_month": local_created.strftime("%m"), - "created_month_name": local_created.strftime("%B"), - "created_month_name_short": local_created.strftime("%b"), - "created_day": local_created.strftime("%d"), + "created": document.created.isoformat(), + "created_year": document.created.strftime("%Y"), + "created_year_short": document.created.strftime("%y"), + "created_month": document.created.strftime("%m"), + "created_month_name": document.created.strftime("%B"), + "created_month_name_short": document.created.strftime("%b"), + "created_day": document.created.strftime("%d"), } diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index a0a380e41..0fb42166a 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -1313,7 +1313,7 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase): _, overrides = self.get_last_consume_delay_call_args() - self.assertEqual(overrides.created, created) + self.assertEqual(overrides.created, created.date()) def test_upload_with_asn(self): self.consume_file_mock.return_value = celery.result.AsyncResult( diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index 96afa61d3..54ea77db3 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -3,7 +3,6 @@ import os import shutil import stat import tempfile -import zoneinfo from pathlib import Path from unittest import mock from unittest.mock import MagicMock @@ -247,20 +246,9 @@ class TestConsumer( self._assert_first_last_send_progress() - # Convert UTC time from DB to local time - document_date_local = timezone.localtime(document.created) - - self.assertEqual( - document_date_local.tzinfo, - zoneinfo.ZoneInfo("America/Chicago"), - ) - self.assertEqual(document_date_local.tzinfo, rough_create_date_local.tzinfo) - self.assertEqual(document_date_local.year, rough_create_date_local.year) - self.assertEqual(document_date_local.month, rough_create_date_local.month) - self.assertEqual(document_date_local.day, rough_create_date_local.day) - self.assertEqual(document_date_local.hour, rough_create_date_local.hour) - self.assertEqual(document_date_local.minute, rough_create_date_local.minute) - # Skipping seconds and more precise + self.assertEqual(document.created.year, rough_create_date_local.year) + self.assertEqual(document.created.month, rough_create_date_local.month) + self.assertEqual(document.created.day, rough_create_date_local.day) @override_settings(FILENAME_FORMAT=None) def testDeleteMacFiles(self): diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py index 6d2d396fc..aad4945ed 100644 --- a/src/documents/tests/test_file_handling.py +++ b/src/documents/tests/test_file_handling.py @@ -333,7 +333,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase): self.assertEqual(generate_filename(doc1), "2020-03-06.pdf") - doc1.created = timezone.make_aware(datetime.datetime(2020, 11, 16, 1, 1, 1)) + doc1.created = datetime.date(2020, 11, 16) self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")