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

Attempt to change LogEntry.changes encoder to make 's usage okay #611

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 16 additions & 0 deletions auditlog/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from decimal import Decimal

from django.core.serializers.json import DjangoJSONEncoder

AUDITLOG_BUGGY_REPR_DATATYPES = (Decimal,)


class AuditLogChangesJSONEncoder(DjangoJSONEncoder):
def default(self, obj):
if isinstance(obj, list) and obj:
return [
str(o) if isinstance(o, AUDITLOG_BUGGY_REPR_DATATYPES) else o
for o in obj
]

return super().default(obj)
5 changes: 4 additions & 1 deletion auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.utils.translation import gettext_lazy as _

from auditlog.diff import mask_str
from auditlog.encoder import AuditLogChangesJSONEncoder

DEFAULT_OBJECT_REPR = "<error forming object repr>"

Expand Down Expand Up @@ -350,7 +351,9 @@ class Action:
choices=Action.choices, verbose_name=_("action"), db_index=True
)
changes_text = models.TextField(blank=True, verbose_name=_("change message"))
changes = models.JSONField(null=True, verbose_name=_("change message"))
changes = models.JSONField(
null=True, verbose_name=_("change message"), encoder=AuditLogChangesJSONEncoder
)
actor = models.ForeignKey(
to=settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
Expand Down
Loading