From 96d3906c81f33e70e4b2a914ee9d84ddcf803e5b Mon Sep 17 00:00:00 2001 From: GreatBahram Date: Mon, 10 Jun 2024 15:12:21 +0200 Subject: [PATCH 1/6] Reorder condition in get_field_value function: as many people have non-database fields and this will break their code ;) --- auditlog/diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auditlog/diff.py b/auditlog/diff.py index 2fd44a97..722ae973 100644 --- a/auditlog/diff.py +++ b/auditlog/diff.py @@ -83,7 +83,7 @@ def get_field_value(obj, field): value = json.dumps(value, sort_keys=True, cls=field.encoder) except TypeError: pass - elif (field.one_to_one or field.many_to_one) and hasattr(field, "rel_class"): + elif hasattr(field, "rel_class") and (field.one_to_one or field.many_to_one): value = smart_str( getattr(obj, field.get_attname(), None), strings_only=True ) From 988cc3f9375c7ca7307d3e8951107896a20e33ed Mon Sep 17 00:00:00 2001 From: GreatBahram Date: Wed, 12 Jun 2024 06:39:16 +0200 Subject: [PATCH 2/6] add a test case to make sure rel_class is checked first --- auditlog_tests/tests.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 2e853d42..16f2a37c 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -30,7 +30,7 @@ from auditlog.admin import LogEntryAdmin from auditlog.cid import get_cid from auditlog.context import disable_auditlog, set_actor -from auditlog.diff import model_instance_diff +from auditlog.diff import model_instance_diff, get_field_value from auditlog.middleware import AuditlogMiddleware from auditlog.models import DEFAULT_OBJECT_REPR, LogEntry from auditlog.registry import AuditlogModelRegistry, AuditLogRegistrationError, auditlog @@ -2164,6 +2164,19 @@ def test_log_entry_created_if_obj_strings_are_same_for_two_objs(self): self.assertEqual(int(log_create.changes_dict["related"][1]), one_simple.id) self.assertEqual(int(log_update.changes_dict["related"][1]), two_simple.id) + def test_rel_class_checked_first(self): + mock_field = mock.Mock() + + type(mock_field).rel_class = mock.PropertyMock(return_value=None) + type(mock_field).one_to_one = mock.PropertyMock(return_value=True) + type(mock_field).many_to_one = mock.PropertyMock(return_value=True) + + mock_obj = mock.Mock() + + get_field_value(mock_obj, mock_field) + + assert "rel_class" == mock_field.method_calls[0] + class TestModelSerialization(TestCase): def setUp(self): From 59ad0cb19374b0b0ca7345bde8bdf9294919c7f9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 04:39:29 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- auditlog_tests/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 16f2a37c..d90c6899 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -30,7 +30,7 @@ from auditlog.admin import LogEntryAdmin from auditlog.cid import get_cid from auditlog.context import disable_auditlog, set_actor -from auditlog.diff import model_instance_diff, get_field_value +from auditlog.diff import get_field_value, model_instance_diff from auditlog.middleware import AuditlogMiddleware from auditlog.models import DEFAULT_OBJECT_REPR, LogEntry from auditlog.registry import AuditlogModelRegistry, AuditLogRegistrationError, auditlog From 8cda3fa15eb61ef361964369b6bfd93d70b177c0 Mon Sep 17 00:00:00 2001 From: GreatBahram Date: Wed, 12 Jun 2024 06:43:31 +0200 Subject: [PATCH 4/6] update the CHANGELOG.md file --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e5233b..5d998c38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed a problem when setting `Value(None)` in `JSONField` ([#646](https://github.com/jazzband/django-auditlog/pull/646)) - Fixed a problem when setting `django.db.models.functions.Now()` in `DateTimeField` ([#635](https://github.com/jazzband/django-auditlog/pull/635)) +- Changed the order of `rel_class` and `one_to_one` and `many_to_one` in `get_field_value` function. ([#XXX](https://github.com/jazzband/django-auditlog/pull/652)) ## 3.0.0 (2024-04-12) From 33151f04bae471c1bfb852a77ffc79f71e404df8 Mon Sep 17 00:00:00 2001 From: GreatBahram Date: Wed, 12 Jun 2024 07:43:54 +0200 Subject: [PATCH 5/6] use unittest assert --- auditlog_tests/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index d90c6899..8133795e 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -2175,7 +2175,7 @@ def test_rel_class_checked_first(self): get_field_value(mock_obj, mock_field) - assert "rel_class" == mock_field.method_calls[0] + self.assertEqual("rel_class", mock_field.method_calls[0]) class TestModelSerialization(TestCase): From fbb1bc7b03696794bde682299b0aefe9c6afbaca Mon Sep 17 00:00:00 2001 From: Bahram Aghaei Date: Thu, 13 Jun 2024 14:54:15 +0200 Subject: [PATCH 6/6] Update CHANGELOG.md Co-authored-by: Hasan Ramezani --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d998c38..aa3e5e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Fixed a problem when setting `Value(None)` in `JSONField` ([#646](https://github.com/jazzband/django-auditlog/pull/646)) - Fixed a problem when setting `django.db.models.functions.Now()` in `DateTimeField` ([#635](https://github.com/jazzband/django-auditlog/pull/635)) -- Changed the order of `rel_class` and `one_to_one` and `many_to_one` in `get_field_value` function. ([#XXX](https://github.com/jazzband/django-auditlog/pull/652)) +- Changed the order of `rel_class` and `one_to_one` and `many_to_one` in `get_field_value` function. ([#652](https://github.com/jazzband/django-auditlog/pull/652)) ## 3.0.0 (2024-04-12)