From 27f57a53ffdc2053ccc22bfba9d6811f84a0b40a Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 13 Dec 2022 21:35:31 +0100 Subject: [PATCH] Fix LogEntry.changes_dict() to return {} when json.loads() returns None (#472) Co-authored-by: Hasan Ramezani --- CHANGELOG.md | 4 ++++ auditlog/models.py | 2 +- auditlog_tests/tests.py | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 109bcdd..5c9ee82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Next Release +#### 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)) + ## 2.2.1 (2022-11-28) #### Fixes diff --git a/auditlog/models.py b/auditlog/models.py index e073c88..9a7340e 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -387,7 +387,7 @@ class LogEntry(models.Model): :return: The changes recorded in this log entry as a dictionary object. """ try: - return json.loads(self.changes) + return json.loads(self.changes) or {} except ValueError: return {} diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index e88c706..9a00458 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -1841,6 +1841,7 @@ class TestAccessLog(TestCase): log_entry.action, LogEntry.Action.ACCESS, msg="Action is 'ACCESS'" ) self.assertEqual(log_entry.changes, "null") + self.assertEqual(log_entry.changes_dict, {}) @override_settings(AUDITLOG_DISABLE_ON_RAW_SAVE=True)