From a93d9ef6650132566185b66ad7f69a52b7df40ef Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 8 Apr 2024 12:06:36 +0200 Subject: [PATCH] Fix serialize __proxy__ objects before logging (#624) * Fix serialize __proxy__ objects before logging * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update CHANGELOG.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hasan Ramezani --- CHANGELOG.md | 3 +++ auditlog/diff.py | 2 ++ auditlog_tests/models.py | 2 +- auditlog_tests/tests.py | 10 ++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4815afce..d5f0a410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changes ## Next Release +#### Fixes + +- Fixed logging problem related to django translation before logging ([#624](https://github.com/jazzband/django-auditlog/pull/624)) ## 3.0.0-beta.4 (2024-01-02) diff --git a/auditlog/diff.py b/auditlog/diff.py index eece7254..b4294fed 100644 --- a/auditlog/diff.py +++ b/auditlog/diff.py @@ -82,6 +82,8 @@ def get_field_value(obj, field): ) else: value = smart_str(getattr(obj, field.name, None)) + if type(value).__name__ == "__proxy__": + value = str(value) except ObjectDoesNotExist: value = ( field.default diff --git a/auditlog_tests/models.py b/auditlog_tests/models.py index 1e16f933..f5db94e6 100644 --- a/auditlog_tests/models.py +++ b/auditlog_tests/models.py @@ -24,7 +24,7 @@ class SimpleModel(models.Model): history = AuditlogHistoryField(delete_related=True) def __str__(self): - return self.text + return str(self.text) class AltPrimaryKeyModel(models.Model): diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 1ce319f0..b04077e7 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -23,6 +23,7 @@ from django.utils import dateformat, formats from django.utils import timezone as django_timezone from django.utils.encoding import smart_str +from django.utils.translation import gettext_lazy as _ from auditlog.admin import LogEntryAdmin from auditlog.cid import get_cid @@ -1641,6 +1642,15 @@ def test_changes_msg_delete(self): ), ) + def test_instance_translation_and_history_logging(self): + first = SimpleModel() + second = SimpleModel(text=_("test")) + changes = model_instance_diff(first, second) + self.assertEqual(changes, {"text": ("", "test")}) + second.save() + log_one = second.history.last() + self.assertTrue(isinstance(log_one, LogEntry)) + def test_changes_msg_create(self): log_entry = self._create_log_entry( LogEntry.Action.CREATE,