From f3dced3199ff54df000ba617c40d1827ef0aab18 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Fri, 13 May 2022 09:10:36 -0700 Subject: [PATCH 1/2] Fixes Document public filename so it will use the local date instead of UTC date --- src/documents/file_handling.py | 21 +++++++----- src/documents/models.py | 4 ++- src/documents/tests/test_document_model.py | 37 ++++++++++++++-------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index cf165d31d..94d091cab 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -6,6 +6,7 @@ from collections import defaultdict import pathvalidate from django.conf import settings from django.template.defaultfilters import slugify +from django.utils import timezone logger = logging.getLogger("paperless.filehandling") @@ -158,18 +159,22 @@ def generate_filename(doc, counter=0, append_gpg=True, archive_filename=False): else: asn = "none" + # Convert UTC database date to localized date + local_added = timezone.localdate(doc.added) + local_created = timezone.localdate(doc.created) + path = settings.PAPERLESS_FILENAME_FORMAT.format( title=pathvalidate.sanitize_filename(doc.title, replacement_text="-"), correspondent=correspondent, document_type=document_type, - created=datetime.date.isoformat(doc.created), - created_year=doc.created.year if doc.created else "none", - created_month=f"{doc.created.month:02}" if doc.created else "none", - created_day=f"{doc.created.day:02}" if doc.created else "none", - added=datetime.date.isoformat(doc.added), - added_year=doc.added.year if doc.added else "none", - added_month=f"{doc.added.month:02}" if doc.added else "none", - added_day=f"{doc.added.day:02}" if doc.added else "none", + created=datetime.date.isoformat(local_created), + created_year=local_created.year, + created_month=f"{local_created.month:02}", + created_day=f"{local_created.day:02}", + added=datetime.date.isoformat(local_added), + added_year=local_added.year, + added_month=f"{local_added.month:02}", + added_day=f"{local_added.day:02}", asn=asn, tags=tags, tag_list=tag_list, diff --git a/src/documents/models.py b/src/documents/models.py index ecabe085c..206df4e8a 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -208,7 +208,9 @@ class Document(models.Model): verbose_name_plural = _("documents") def __str__(self): - created = datetime.date.isoformat(self.created) + + # Convert UTC database time to local time + created = datetime.date.isoformat(timezone.localdate(self.created)) if self.correspondent and self.title: return f"{created} {self.correspondent} {self.title}" diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index 41e3d9337..c456a4bb7 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -56,26 +56,35 @@ class TestDocument(TestCase): doc = Document( mime_type="application/pdf", title="test", - created=timezone.datetime(2020, 12, 25), + created=timezone.datetime(2020, 12, 25, tzinfo=zoneinfo.ZoneInfo("UTC")), ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") + @override_settings( + TIME_ZONE="Europe/Berlin", + ) def test_file_name_with_timezone(self): + # See https://docs.djangoproject.com/en/4.0/ref/utils/#django.utils.timezone.now + # The default for created is an aware datetime in UTC + # This does that, just manually, with a fixed date + local_create_date = timezone.datetime( + 2020, + 12, + 25, + tzinfo=zoneinfo.ZoneInfo("Europe/Berlin"), + ) + + utc_create_date = local_create_date.astimezone(zoneinfo.ZoneInfo("UTC")) + + self.assertEqual(utc_create_date.date().day, 24) + doc = Document( mime_type="application/pdf", title="test", - created=timezone.datetime( - 2020, - 12, - 25, - 0, - 0, - 0, - 0, - zoneinfo.ZoneInfo("Europe/Berlin"), - ), + created=utc_create_date, ) + self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") def test_file_name_jpg(self): @@ -83,7 +92,7 @@ class TestDocument(TestCase): doc = Document( mime_type="image/jpeg", title="test", - created=timezone.datetime(2020, 12, 25), + created=timezone.datetime(2020, 12, 25, tzinfo=zoneinfo.ZoneInfo("UTC")), ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.jpg") @@ -92,7 +101,7 @@ class TestDocument(TestCase): doc = Document( mime_type="application/zip", title="test", - created=timezone.datetime(2020, 12, 25), + created=timezone.datetime(2020, 12, 25, tzinfo=zoneinfo.ZoneInfo("UTC")), ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.zip") @@ -101,6 +110,6 @@ class TestDocument(TestCase): doc = Document( mime_type="image/jpegasd", title="test", - created=timezone.datetime(2020, 12, 25), + created=timezone.datetime(2020, 12, 25, tzinfo=zoneinfo.ZoneInfo("UTC")), ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test") From 1d1f3bde9678d39cf429ff1c5ef030a8d9db5c13 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Sat, 14 May 2022 08:20:55 -0700 Subject: [PATCH 2/2] Moves the asserts to be closer together --- src/documents/tests/test_document_model.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index c456a4bb7..57455fb8f 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -77,7 +77,24 @@ class TestDocument(TestCase): utc_create_date = local_create_date.astimezone(zoneinfo.ZoneInfo("UTC")) + doc = Document( + mime_type="application/pdf", + title="test", + created=utc_create_date, + ) + + # Ensure the create date would cause an off by 1 if not properly created above self.assertEqual(utc_create_date.date().day, 24) + self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") + + local_create_date = timezone.datetime( + 2020, + 1, + 1, + tzinfo=zoneinfo.ZoneInfo("Europe/Berlin"), + ) + + utc_create_date = local_create_date.astimezone(zoneinfo.ZoneInfo("UTC")) doc = Document( mime_type="application/pdf", @@ -85,7 +102,9 @@ class TestDocument(TestCase): created=utc_create_date, ) - self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") + # Ensure the create date would cause an off by 1 in the year if not properly created above + self.assertEqual(utc_create_date.date().year, 2019) + self.assertEqual(doc.get_public_filename(), "2020-01-01 test.pdf") def test_file_name_jpg(self):