Skip to content

Commit

Permalink
Fix serialize __proxy__ objects before logging (#624)
Browse files Browse the repository at this point in the history
* 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 <hasan.r67@gmail.com>
  • Loading branch information
3 people authored Apr 8, 2024
1 parent 2f949dd commit a93d9ef
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
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))

## 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

0 comments on commit a93d9ef

Please sign in to comment.