feat: Make timestamp in LogEntry overwritable (#478)

This commit is contained in:
Abdullah Alaqeel 2022-12-19 02:43:29 -05:00 committed by GitHub
parent 971a4f42f8
commit a733cd0852
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 1 deletions

View file

@ -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)

View file

@ -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",
),
),
]

View file

@ -14,6 +14,7 @@ from django.core.exceptions import FieldDoesNotExist
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 _
@ -355,7 +356,9 @@ class LogEntry(models.Model):
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")

View file

@ -200,6 +200,24 @@ class SimpleModelTest(TestCase):
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):