Skip to content

Commit

Permalink
feat: Make timestamp in LogEntry overwritable (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqeelat committed Dec 19, 2022
1 parent 971a4f4 commit a733cd0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Fixes

- fix: Make sure `LogEntry.changes_dict()` returns an empty dict instead of `None` when `json.loads()` returns `None`. ([#472](https://github.com/jazzband/django-auditlog/pull/472))
- feat: Make timestamp in LogEntry overwritable. ([#476](https://github.com/jazzband/django-auditlog/pull/476))

## 2.2.1 (2022-11-28)

Expand Down
23 changes: 23 additions & 0 deletions auditlog/migrations/0013_alter_logentry_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.4 on 2022-12-15 21:24

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("auditlog", "0012_add_logentry_action_access"),
]

operations = [
migrations.AlterField(
model_name="logentry",
name="timestamp",
field=models.DateTimeField(
db_index=True,
default=django.utils.timezone.now,
verbose_name="timestamp",
),
),
]
5 changes: 4 additions & 1 deletion auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.db import DEFAULT_DB_ALIAS, models
from django.db.models import Q, QuerySet
from django.utils import formats
from django.utils import timezone as django_timezone
from django.utils.encoding import smart_str
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -355,7 +356,9 @@ class Action:
blank=True, null=True, verbose_name=_("remote address")
)
timestamp = models.DateTimeField(
db_index=True, auto_now_add=True, verbose_name=_("timestamp")
default=django_timezone.now,
db_index=True,
verbose_name=_("timestamp"),
)
additional_data = models.JSONField(
blank=True, null=True, verbose_name=_("additional data")
Expand Down
18 changes: 18 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ def test_create_log_to_object_from_other_database(self):
log_entry._state.db, "default", msg=msg
) # must be created in default database

def test_default_timestamp(self):
start = django_timezone.now()
self.test_recreate()
end = django_timezone.now()
history = self.obj.history.latest()
self.assertTrue(start <= history.timestamp <= end)

def test_manual_timestamp(self):
timestamp = datetime.datetime(1999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
LogEntry.objects.log_create(
instance=self.obj,
timestamp=timestamp,
changes="foo bar",
action=LogEntry.Action.UPDATE,
)
history = self.obj.history.filter(timestamp=timestamp, changes="foo bar")
self.assertTrue(history.exists())


class NoActorMixin:
def check_create_log_entry(self, obj, log_entry):
Expand Down

0 comments on commit a733cd0

Please sign in to comment.