added fixes for updates and deletes if AUDITLOG_STORE_JSON_CHANGES is True (#732)

This commit is contained in:
The Alchemist 2025-07-03 15:56:37 -04:00 committed by GitHub
parent 8fe73932a0
commit 138e4fc948
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 0 deletions

View file

@ -8,6 +8,8 @@
#### Fixes
- fix: ```AUDITLOG_STORE_JSON_CHANGES=True``` was not respected during updates and deletions.
## 3.2.0 (2025-06-26)
#### Improvements

View file

@ -184,6 +184,8 @@ def model_instance_diff(
:type new: Model
:param fields_to_check: An iterable of the field names to restrict the diff to, while ignoring the rest of
the model's fields. This is used to pass the `update_fields` kwarg from the model's `save` method.
:param use_json_for_changes: whether or not to use a JSON for changes
(see settings.AUDITLOG_STORE_JSON_CHANGES)
:type fields_to_check: Iterable
:return: A dictionary with the names of the changed fields as keys and a two tuple of the old and new
field values as value.

View file

@ -64,6 +64,7 @@ def log_update(sender, instance, **kwargs):
diff_old=old,
diff_new=instance,
fields_to_check=update_fields,
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
)
@ -81,6 +82,7 @@ def log_delete(sender, instance, **kwargs):
sender=sender,
diff_old=instance,
diff_new=None,
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
)
@ -98,6 +100,7 @@ def log_access(sender, instance, **kwargs):
diff_old=None,
diff_new=None,
force_log=True,
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
)
@ -152,6 +155,7 @@ def _create_log_entry(
changes=changes,
log_entry=log_entry,
log_created=log_entry is not None,
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
)
if error:
raise error

View file

@ -1,6 +1,7 @@
from django.test import TestCase, override_settings
from test_app.models import JSONModel, RelatedModel, SimpleModel
from auditlog.models import LogEntry
from auditlog.registry import AuditlogModelRegistry
@ -113,3 +114,36 @@ class JSONForChangesTest(TestCase):
field_one_to_one_changes = changes_dict["one_to_one"]
self.assertEqual(field_one_to_one_changes, [None, simple.id])
@override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
def test_use_json_for_changes_update(self):
self.test_auditlog.register_from_settings()
simple = SimpleModel(text="original")
simple.save()
simple.text = "new"
simple.save()
changes_dict = simple.history.latest().changes_dict
text_changes = changes_dict["text"]
self.assertEqual(text_changes, ["original", "new"])
@override_settings(AUDITLOG_STORE_JSON_CHANGES=True)
def test_use_json_for_changes_delete(self):
self.test_auditlog.register_from_settings()
simple = SimpleModel()
simple.save()
simple.delete()
history = LogEntry.objects.all()
self.assertEqual(history.count(), 1, '"DELETE" record is always retained')
changes_dict = history.first().changes_dict
self.assertTrue(
all(v[1] is None for k, v in changes_dict.items()),
'all values in the changes dict should None, not "None"',
)