Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialize __proxy__ objects before logging #624

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
hramezani marked this conversation as resolved.
Show resolved Hide resolved

## 3.0.0-beta.4 (2024-01-02)

Expand Down
2 changes: 2 additions & 0 deletions auditlog/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion auditlog_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down