Just curious

This commit is contained in:
shamoon 2025-04-03 18:10:12 -07:00
parent 2e593a0022
commit 5e9790124b
No known key found for this signature in database
10 changed files with 69 additions and 47 deletions

View File

@ -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,
}

View File

@ -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,

View File

@ -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",
),
),
]

View File

@ -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):

View File

@ -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(

View File

@ -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 = {}

View File

@ -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"),
}

View File

@ -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(

View File

@ -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):

View File

@ -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")