Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display timestamps in server timezone #404

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

#### Fixes

- fix: Display `created` timestamp in server timezone ([#404](https://github.com/jazzband/django-auditlog/pull/404))

## 2.1.1 (2022-07-27)

#### Improvements
Expand Down
3 changes: 2 additions & 1 deletion auditlog/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 auditlog.models import LogEntry

Expand All @@ -13,7 +14,7 @@

class LogEntryAdminMixin:
def created(self, obj):
return obj.timestamp.strftime("%Y-%m-%d %H:%M:%S")
return localtime(obj.timestamp).strftime("%Y-%m-%d %H:%M:%S")

created.short_description = "Created"

Expand Down
44 changes: 27 additions & 17 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
from unittest import mock

import freezegun
from dateutil.tz import gettz
from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -1233,31 +1234,40 @@ def test_changes_display_dict_arrayfield(self):


class AdminPanelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.username = "test_admin"
cls.password = User.objects.make_random_password()
cls.user, created = User.objects.get_or_create(username=cls.username)
cls.user.set_password(cls.password)
cls.user.is_staff = True
cls.user.is_superuser = True
cls.user.is_active = True
cls.user.save()
cls.obj = SimpleModel.objects.create(text="For admin logentry test")
def setUp(self):
self.user = User.objects.create_user(
username="test_admin", is_staff=True, is_superuser=True, is_active=True
)
self.site = AdminSite()
self.admin = LogEntryAdmin(LogEntry, self.site)
with freezegun.freeze_time("2022-08-01 12:00:00Z"):
self.obj = SimpleModel.objects.create(text="For admin logentry test")

def test_auditlog_admin(self):
self.client.login(username=self.username, password=self.password)
self.client.force_login(self.user)
log_pk = self.obj.history.latest().pk
res = self.client.get("/admin/auditlog/logentry/")
assert res.status_code == 200
self.assertEqual(res.status_code, 200)
res = self.client.get("/admin/auditlog/logentry/add/")
assert res.status_code == 403
self.assertEqual(res.status_code, 403)
res = self.client.get(f"/admin/auditlog/logentry/{log_pk}/", follow=True)
assert res.status_code == 200
self.assertEqual(res.status_code, 200)
res = self.client.get(f"/admin/auditlog/logentry/{log_pk}/delete/")
assert res.status_code == 200
self.assertEqual(res.status_code, 200)
res = self.client.get(f"/admin/auditlog/logentry/{log_pk}/history/")
assert res.status_code == 200
self.assertEqual(res.status_code, 200)

def test_created_timezone(self):
log_entry = self.obj.history.latest()

for tz, timestamp in [
("UTC", "2022-08-01 12:00:00"),
("Asia/Tbilisi", "2022-08-01 16:00:00"),
("America/Buenos_Aires", "2022-08-01 09:00:00"),
("Asia/Kathmandu", "2022-08-01 17:45:00"),
]:
with self.settings(TIME_ZONE=tz):
self.assertEqual(self.admin.created(log_entry), timestamp)


class DiffMsgTest(TestCase):
Expand Down