mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Add support for m2m changes in AbstractLogEntry.changes_str (#798)
Some checks failed
Test / SQLite • Python 3.10 (push) Has been cancelled
Test / SQLite • Python 3.11 (push) Has been cancelled
Test / SQLite • Python 3.12 (push) Has been cancelled
Test / SQLite • Python 3.13 (push) Has been cancelled
Test / PostgreSQL • Python 3.10 (push) Has been cancelled
Test / PostgreSQL • Python 3.11 (push) Has been cancelled
Test / PostgreSQL • Python 3.12 (push) Has been cancelled
Test / PostgreSQL • Python 3.13 (push) Has been cancelled
Test / MySQL • Python 3.10 (push) Has been cancelled
Test / MySQL • Python 3.11 (push) Has been cancelled
Test / MySQL • Python 3.12 (push) Has been cancelled
Test / MySQL • Python 3.13 (push) Has been cancelled
Some checks failed
Test / SQLite • Python 3.10 (push) Has been cancelled
Test / SQLite • Python 3.11 (push) Has been cancelled
Test / SQLite • Python 3.12 (push) Has been cancelled
Test / SQLite • Python 3.13 (push) Has been cancelled
Test / PostgreSQL • Python 3.10 (push) Has been cancelled
Test / PostgreSQL • Python 3.11 (push) Has been cancelled
Test / PostgreSQL • Python 3.12 (push) Has been cancelled
Test / PostgreSQL • Python 3.13 (push) Has been cancelled
Test / MySQL • Python 3.10 (push) Has been cancelled
Test / MySQL • Python 3.11 (push) Has been cancelled
Test / MySQL • Python 3.12 (push) Has been cancelled
Test / MySQL • Python 3.13 (push) Has been cancelled
* Add test case to test log entry changes_str property for m2m changes. * Add support for m2m field changes and generic changes in AbstractLogEntry.changes_str property. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add more test cases for changes_str. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add chengelog note * Validate type and length of changes_dict values. * Restructure change iterator. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
198c060c3b
commit
6d170da5fc
3 changed files with 58 additions and 10 deletions
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## Next Release
|
## Next Release
|
||||||
|
|
||||||
|
#### Fixes
|
||||||
|
|
||||||
|
- `KeyError` when calling `changes_str` on a log entry that tracks many-to-many field changes ([#798](https://github.com/jazzband/django-auditlog/pull/798))
|
||||||
|
|
||||||
## 3.4.1 (2025-12-13)
|
## 3.4.1 (2025-12-13)
|
||||||
|
|
||||||
#### Fixes
|
#### Fixes
|
||||||
|
|
|
||||||
|
|
@ -427,21 +427,29 @@ class AbstractLogEntry(models.Model):
|
||||||
not satisfying, please use :py:func:`LogEntry.changes_dict` and format the string yourself.
|
not satisfying, please use :py:func:`LogEntry.changes_dict` and format the string yourself.
|
||||||
|
|
||||||
:param colon: The string to place between the field name and the values.
|
:param colon: The string to place between the field name and the values.
|
||||||
:param arrow: The string to place between each old and new value.
|
:param arrow: The string to place between each old and new value (non-m2m field changes only).
|
||||||
:param separator: The string to place between each field.
|
:param separator: The string to place between each field.
|
||||||
:return: A readable string of the changes in this log entry.
|
:return: A readable string of the changes in this log entry.
|
||||||
"""
|
"""
|
||||||
substrings = []
|
substrings = []
|
||||||
|
|
||||||
for field, values in self.changes_dict.items():
|
for field, value in sorted(self.changes_dict.items()):
|
||||||
substring = "{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}".format(
|
if isinstance(value, (list, tuple)) and len(value) == 2:
|
||||||
field_name=field,
|
# handle regular field change
|
||||||
colon=colon,
|
substring = "{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}".format(
|
||||||
old=values[0],
|
field_name=field,
|
||||||
arrow=arrow,
|
colon=colon,
|
||||||
new=values[1],
|
old=value[0],
|
||||||
)
|
arrow=arrow,
|
||||||
substrings.append(substring)
|
new=value[1],
|
||||||
|
)
|
||||||
|
substrings.append(substring)
|
||||||
|
elif isinstance(value, dict) and value.get("type") == "m2m":
|
||||||
|
# handle m2m change
|
||||||
|
substring = (
|
||||||
|
f"{field}{colon}{value['operation']} {sorted(value['objects'])}"
|
||||||
|
)
|
||||||
|
substrings.append(substring)
|
||||||
|
|
||||||
return separator.join(substrings)
|
return separator.join(substrings)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,11 @@ class SimpleModelTest(TestCase):
|
||||||
{"boolean": ["False", "True"]},
|
{"boolean": ["False", "True"]},
|
||||||
msg="The change is correctly logged",
|
msg="The change is correctly logged",
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
history.changes_str,
|
||||||
|
"boolean: False → True",
|
||||||
|
msg="Changes string is correct",
|
||||||
|
)
|
||||||
|
|
||||||
def test_update_specific_field_supplied_via_save_method(self):
|
def test_update_specific_field_supplied_via_save_method(self):
|
||||||
obj = self.obj
|
obj = self.obj
|
||||||
|
|
@ -149,6 +154,11 @@ class SimpleModelTest(TestCase):
|
||||||
"when using the `update_fields`."
|
"when using the `update_fields`."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
obj.history.get(action=LogEntry.Action.UPDATE).changes_str,
|
||||||
|
"boolean: False → True",
|
||||||
|
msg="Changes string is correct",
|
||||||
|
)
|
||||||
|
|
||||||
def test_django_update_fields_edge_cases(self):
|
def test_django_update_fields_edge_cases(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -179,6 +189,11 @@ class SimpleModelTest(TestCase):
|
||||||
{"boolean": ["False", "True"], "integer": ["None", "1"]},
|
{"boolean": ["False", "True"], "integer": ["None", "1"]},
|
||||||
msg="The 2 fields changed are correctly logged",
|
msg="The 2 fields changed are correctly logged",
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
obj.history.get(action=LogEntry.Action.UPDATE).changes_str,
|
||||||
|
"boolean: False → True; integer: None → 1",
|
||||||
|
msg="Changes string is correct",
|
||||||
|
)
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
"""Deletion is logged correctly."""
|
"""Deletion is logged correctly."""
|
||||||
|
|
@ -494,6 +509,13 @@ class ManyRelatedModelTest(TestCase):
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_changes_str(self):
|
||||||
|
self.obj.related.add(self.related)
|
||||||
|
log_entry = self.obj.history.first()
|
||||||
|
self.assertEqual(
|
||||||
|
log_entry.changes_str, f"related: add {[smart_str(self.related)]}"
|
||||||
|
)
|
||||||
|
|
||||||
def test_adding_existing_related_obj(self):
|
def test_adding_existing_related_obj(self):
|
||||||
self.obj.related.add(self.related)
|
self.obj.related.add(self.related)
|
||||||
log_entry = self.obj.history.first()
|
log_entry = self.obj.history.first()
|
||||||
|
|
@ -725,6 +747,11 @@ class SimpleIncludeModelTest(TestCase):
|
||||||
{"label": ["Initial label", "New label"]},
|
{"label": ["Initial label", "New label"]},
|
||||||
msg="Only the label was logged, regardless of multiple entries in `update_fields`",
|
msg="Only the label was logged, regardless of multiple entries in `update_fields`",
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
obj.history.get(action=LogEntry.Action.UPDATE).changes_str,
|
||||||
|
"label: Initial label → New label",
|
||||||
|
msg="Changes string is correct",
|
||||||
|
)
|
||||||
|
|
||||||
def test_register_include_fields(self):
|
def test_register_include_fields(self):
|
||||||
sim = SimpleIncludeModel(label="Include model", text="Looong text")
|
sim = SimpleIncludeModel(label="Include model", text="Looong text")
|
||||||
|
|
@ -2061,6 +2088,11 @@ class JSONModelTest(TestCase):
|
||||||
{"json": ["{}", '{"quantity": "1"}']},
|
{"json": ["{}", '{"quantity": "1"}']},
|
||||||
msg="The change is correctly logged",
|
msg="The change is correctly logged",
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
history.changes_str,
|
||||||
|
'json: {} → {"quantity": "1"}',
|
||||||
|
msg="Changes string is correct",
|
||||||
|
)
|
||||||
|
|
||||||
def test_update_with_no_changes(self):
|
def test_update_with_no_changes(self):
|
||||||
"""No changes are logged."""
|
"""No changes are logged."""
|
||||||
|
|
@ -2697,6 +2729,7 @@ class TestAccessLog(TestCase):
|
||||||
)
|
)
|
||||||
self.assertIsNone(log_entry.changes)
|
self.assertIsNone(log_entry.changes)
|
||||||
self.assertEqual(log_entry.changes_dict, {})
|
self.assertEqual(log_entry.changes_dict, {})
|
||||||
|
self.assertEqual(log_entry.changes_str, "")
|
||||||
|
|
||||||
|
|
||||||
class SignalTests(TestCase):
|
class SignalTests(TestCase):
|
||||||
|
|
@ -3120,6 +3153,9 @@ class BaseManagerSettingTest(TestCase):
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
log_entry.changes_str, f"m2m_related: add {[smart_str(obj_two)]}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestMaskStr(TestCase):
|
class TestMaskStr(TestCase):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue