[backport] Fix a bug in audit log admin page when USE_TZ=False

This commit is contained in:
Hasan Ramezani 2023-05-25 19:23:51 +03:30
parent 2595a36c71
commit d4d5bade12
3 changed files with 18 additions and 2 deletions

View file

@ -2,6 +2,9 @@
## Next Release
#### Fixes
fix: Fix a bug in audit log admin page when `USE_TZ=False`. ([#511](https://github.com/jazzband/django-auditlog/pull/511))
## 2.2.2 (2023-01-16)
#### Fixes

View file

@ -8,7 +8,7 @@ from django.forms.utils import pretty_name
from django.urls.exceptions import NoReverseMatch
from django.utils.html import format_html, format_html_join
from django.utils.safestring import mark_safe
from django.utils.timezone import localtime
from django.utils.timezone import is_aware, localtime
from auditlog.models import LogEntry
from auditlog.registry import auditlog
@ -20,7 +20,9 @@ MAX = 75
class LogEntryAdminMixin:
@admin.display(description="Created")
def created(self, obj):
return localtime(obj.timestamp)
if is_aware(obj.timestamp):
return localtime(obj.timestamp)
return obj.timestamp
@admin.display(description="User")
def user_url(self, obj):

View file

@ -1314,6 +1314,17 @@ class AdminPanelTest(TestCase):
created = self.admin.created(log_entry)
self.assertEqual(created.strftime("%Y-%m-%d %H:%M:%S"), timestamp)
@freezegun.freeze_time("2022-08-01 12:00:00Z")
def test_created_naive_datetime(self):
with self.settings(USE_TZ=False):
obj = SimpleModel.objects.create(text="For USE_TZ=False test")
log_entry = obj.history.latest()
created = self.admin.created(log_entry)
self.assertEqual(
created.strftime("%Y-%m-%d %H:%M:%S"),
"2022-08-01 12:00:00",
)
class DiffMsgTest(TestCase):
def setUp(self):