This commit is contained in:
jonaswinkler 2021-01-12 13:05:49 +01:00
parent 890f6f35b5
commit 623893b528
2 changed files with 10 additions and 6 deletions

View File

@ -12,6 +12,7 @@ from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import is_aware
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -233,7 +234,10 @@ class Document(models.Model):
verbose_name_plural = _("documents") verbose_name_plural = _("documents")
def __str__(self): 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: if self.correspondent and self.title:
return f"{created} {self.correspondent} {self.title}" return f"{created} {self.correspondent} {self.title}"
else: else:

View File

@ -1,10 +1,10 @@
import shutil import shutil
import tempfile import tempfile
from datetime import datetime
from pathlib import Path from pathlib import Path
from unittest import mock from unittest import mock
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils import timezone
from ..models import Document, Correspondent from ..models import Document, Correspondent
@ -47,20 +47,20 @@ class TestDocument(TestCase):
def test_file_name(self): 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") self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
def test_file_name_jpg(self): 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") self.assertEqual(doc.get_public_filename(), "2020-12-25 test.jpg")
def test_file_name_unknown(self): 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") self.assertEqual(doc.get_public_filename(), "2020-12-25 test.zip")
def test_file_name_invalid_type(self): 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") self.assertEqual(doc.get_public_filename(), "2020-12-25 test")