A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Fixes Document public filename so it will use the local date instead of UTC date
Trenton Holmesf3dced3199ff
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.pyindex cf165d31d..94d091cab 100644--- a/src/documents/file_handling.py+++ b/src/documents/file_handling.py@@ -6,6 +6,7 @@ from collections import defaultdictimport pathvalidatefrom django.conf import settingsfrom django.template.defaultfilters import slugify+from django.utils import timezonelogger = 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.pyindex 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.pyindex 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")