Skip to content

Commit

Permalink
Use DjangoJSONEncoder instead of custom LazyStringEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
laymonage committed Apr 30, 2024
1 parent afbafd6 commit 56e69bc
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -10,6 +10,7 @@ Changelog
* Fix: Make `WAGTAILIMAGES_CHOOSER_PAGE_SIZE` setting functional again (Rohit Sharma)
* Fix: Enable `richtext` template tag to convert lazy translation values (Benjamin Bach)
* Docs: Remove duplicate section on frontend caching proxies from performance page (Jake Howard)
* Maintenance: Use `DjangoJSONEncoder` instead of custom `LazyStringEncoder` to serialize Draftail config (Sage Abdullah)


6.1 (xx.xx.xxxx) - IN DEVELOPMENT
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/6.2.md
Expand Up @@ -31,7 +31,7 @@ depth: 1

### Maintenance

* ...
* Use `DjangoJSONEncoder` instead of custom `LazyStringEncoder` to serialize Draftail config (Sage Abdullah)


## Upgrade considerations - changes affecting all projects
Expand Down
22 changes: 2 additions & 20 deletions wagtail/admin/rich_text/editors/draftail/__init__.py
@@ -1,10 +1,9 @@
import json
import warnings

from django.core.serializers.json import DjangoJSONEncoder
from django.forms import Media, widgets
from django.urls import reverse_lazy
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy

from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
from wagtail.admin.staticfiles import versioned_static
Expand All @@ -13,23 +12,6 @@
from wagtail.widget_adapters import WidgetAdapter


class LazyStringEncoder(json.JSONEncoder):
"""
Add support for lazy strings to the JSON encoder so that URLs and
translations can be resolved when rendering the widget only.
"""

# The string "Edit" here is arbitrary, chosen because it exists elsewhere in the
# translations dictionary and is likely to remain in the future.
lazy_string_types = [type(reverse_lazy("Edit")), type(gettext_lazy("Edit"))]

def default(self, obj):
if type(obj) in self.lazy_string_types:
return str(obj)

return json.JSONEncoder.default(self, obj)


class DraftailRichTextArea(widgets.HiddenInput):
template_name = "wagtailadmin/widgets/draftail_rich_text_area.html"
is_hidden = False
Expand Down Expand Up @@ -90,7 +72,7 @@ def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context["widget"]["attrs"]["data-w-init-detail-value"] = json.dumps(
self.options,
cls=LazyStringEncoder,
cls=DjangoJSONEncoder,
)
return context

Expand Down

0 comments on commit 56e69bc

Please sign in to comment.