From 0ba1ba55bd998c6689fe373e2fa4e89c0d0fd23b Mon Sep 17 00:00:00 2001 From: Oliver Lippert Date: Sun, 1 May 2022 08:41:38 +0200 Subject: [PATCH 1/4] add test for filename expectation with modified timezone Signed-off-by: Oliver Lippert #267 --- src/documents/tests/test_document_model.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index a99d6dd18..bea8b6b91 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -3,6 +3,7 @@ import tempfile from pathlib import Path from unittest import mock +import pytz from django.test import override_settings from django.test import TestCase from django.utils import timezone @@ -55,6 +56,24 @@ class TestDocument(TestCase): ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") + def test_file_name_with_timezone(self): + + doc = Document( + mime_type="application/pdf", + title="test", + created=timezone.datetime( + 2020, + 12, + 25, + 0, + 0, + 0, + 0, + pytz.timezone("Europe/Berlin"), + ), + ) + self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") + def test_file_name_jpg(self): doc = Document( From 182cea3385cc4ac35b157d0b9d510f248c939314 Mon Sep 17 00:00:00 2001 From: Oliver Lippert Date: Sun, 1 May 2022 08:54:41 +0200 Subject: [PATCH 2/4] always use datetime.date.isoformat to generate filename fixes #267 Signed-off-by: Oliver Lippert --- src/documents/models.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/documents/models.py b/src/documents/models.py index a1a787325..8f531e401 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -11,7 +11,6 @@ from django.conf import settings from django.contrib.auth.models import User from django.db import models from django.utils import timezone -from django.utils.timezone import is_aware from django.utils.translation import gettext_lazy as _ from documents.parsers import get_default_file_extension @@ -210,10 +209,8 @@ class Document(models.Model): verbose_name_plural = _("documents") def __str__(self): - if is_aware(self.created): - created = timezone.localdate(self.created).isoformat() - else: - created = datetime.date.isoformat(self.created) + created = datetime.date.isoformat(self.created) + if self.correspondent and self.title: return f"{created} {self.correspondent} {self.title}" else: From c3b5b47b22ca6f40ae788655bfdfed2ea0f3cad8 Mon Sep 17 00:00:00 2001 From: Oliver Lippert Date: Tue, 3 May 2022 08:08:28 +0200 Subject: [PATCH 3/4] use zoneinfo instead of pytz Signed-off-by: Oliver Lippert #267 --- src/documents/tests/test_document_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index bea8b6b91..3ff4d3253 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -1,9 +1,9 @@ import shutil import tempfile +import zoneinfo from pathlib import Path from unittest import mock -import pytz from django.test import override_settings from django.test import TestCase from django.utils import timezone @@ -69,7 +69,7 @@ class TestDocument(TestCase): 0, 0, 0, - pytz.timezone("Europe/Berlin"), + zoneinfo.ZoneInfo("Europe/Berlin"), ), ) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") From 7432ef9e19603f392c4240f26eef90d113a80f57 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Wed, 4 May 2022 15:13:17 -0700 Subject: [PATCH 4/4] Accounts for Python 3.8 backported zoneinfo --- src/documents/tests/test_document_model.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index 3ff4d3253..41e3d9337 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -1,9 +1,13 @@ import shutil import tempfile -import zoneinfo from pathlib import Path from unittest import mock +try: + import zoneinfo +except ImportError: + import backports.zoneinfo as zoneinfo + from django.test import override_settings from django.test import TestCase from django.utils import timezone