From 623893b528f3cd688627b59eabf67f45d439f05d Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Tue, 12 Jan 2021 13:05:49 +0100 Subject: [PATCH] fixes #331 --- src/documents/models.py | 6 +++++- src/documents/tests/test_document_model.py | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/documents/models.py b/src/documents/models.py index 3f0e331d0..928b8aa4d 100755 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -12,6 +12,7 @@ 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 _ @@ -233,7 +234,10 @@ class Document(models.Model): verbose_name_plural = _("documents") def __str__(self): - created = datetime.date.isoformat(self.created) + if is_aware(self.created): + created = timezone.localdate(self.created).isoformat() + else: + created = datetime.date.isoformat(self.created) if self.correspondent and self.title: return f"{created} {self.correspondent} {self.title}" else: diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py index 74bd9a2a7..87261ecd9 100644 --- a/src/documents/tests/test_document_model.py +++ b/src/documents/tests/test_document_model.py @@ -1,10 +1,10 @@ import shutil import tempfile -from datetime import datetime from pathlib import Path from unittest import mock from django.test import TestCase, override_settings +from django.utils import timezone from ..models import Document, Correspondent @@ -47,20 +47,20 @@ class TestDocument(TestCase): def test_file_name(self): - doc = Document(mime_type="application/pdf", title="test", created=datetime(2020, 12, 25)) + doc = Document(mime_type="application/pdf", title="test", created=timezone.datetime(2020, 12, 25)) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf") def test_file_name_jpg(self): - doc = Document(mime_type="image/jpeg", title="test", created=datetime(2020, 12, 25)) + doc = Document(mime_type="image/jpeg", title="test", created=timezone.datetime(2020, 12, 25)) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.jpg") def test_file_name_unknown(self): - doc = Document(mime_type="application/zip", title="test", created=datetime(2020, 12, 25)) + doc = Document(mime_type="application/zip", title="test", created=timezone.datetime(2020, 12, 25)) self.assertEqual(doc.get_public_filename(), "2020-12-25 test.zip") def test_file_name_invalid_type(self): - doc = Document(mime_type="image/jpegasd", title="test", created=datetime(2020, 12, 25)) + doc = Document(mime_type="image/jpegasd", title="test", created=timezone.datetime(2020, 12, 25)) self.assertEqual(doc.get_public_filename(), "2020-12-25 test")