mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
added fixes for updates and deletes if AUDITLOG_STORE_JSON_CHANGES is True (#732)
This commit is contained in:
parent
8fe73932a0
commit
138e4fc948
4 changed files with 42 additions and 0 deletions
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
#### Fixes
|
#### Fixes
|
||||||
|
|
||||||
|
- fix: ```AUDITLOG_STORE_JSON_CHANGES=True``` was not respected during updates and deletions.
|
||||||
|
|
||||||
## 3.2.0 (2025-06-26)
|
## 3.2.0 (2025-06-26)
|
||||||
|
|
||||||
#### Improvements
|
#### Improvements
|
||||||
|
|
|
||||||
|
|
@ -184,6 +184,8 @@ def model_instance_diff(
|
||||||
:type new: Model
|
:type new: Model
|
||||||
:param fields_to_check: An iterable of the field names to restrict the diff to, while ignoring the rest of
|
: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.
|
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
|
: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
|
: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.
|
field values as value.
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ def log_update(sender, instance, **kwargs):
|
||||||
diff_old=old,
|
diff_old=old,
|
||||||
diff_new=instance,
|
diff_new=instance,
|
||||||
fields_to_check=update_fields,
|
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,
|
sender=sender,
|
||||||
diff_old=instance,
|
diff_old=instance,
|
||||||
diff_new=None,
|
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_old=None,
|
||||||
diff_new=None,
|
diff_new=None,
|
||||||
force_log=True,
|
force_log=True,
|
||||||
|
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -152,6 +155,7 @@ def _create_log_entry(
|
||||||
changes=changes,
|
changes=changes,
|
||||||
log_entry=log_entry,
|
log_entry=log_entry,
|
||||||
log_created=log_entry is not None,
|
log_created=log_entry is not None,
|
||||||
|
use_json_for_changes=settings.AUDITLOG_STORE_JSON_CHANGES,
|
||||||
)
|
)
|
||||||
if error:
|
if error:
|
||||||
raise error
|
raise error
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from test_app.models import JSONModel, RelatedModel, SimpleModel
|
from test_app.models import JSONModel, RelatedModel, SimpleModel
|
||||||
|
|
||||||
|
from auditlog.models import LogEntry
|
||||||
from auditlog.registry import AuditlogModelRegistry
|
from auditlog.registry import AuditlogModelRegistry
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -113,3 +114,36 @@ class JSONForChangesTest(TestCase):
|
||||||
|
|
||||||
field_one_to_one_changes = changes_dict["one_to_one"]
|
field_one_to_one_changes = changes_dict["one_to_one"]
|
||||||
self.assertEqual(field_one_to_one_changes, [None, simple.id])
|
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"',
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue