diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 3db8dc2fd71..41352b38164 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 diff --git a/docs/releases/6.2.md b/docs/releases/6.2.md index 17428d7da05..60c8040e187 100644 --- a/docs/releases/6.2.md +++ b/docs/releases/6.2.md @@ -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 diff --git a/wagtail/admin/rich_text/editors/draftail/__init__.py b/wagtail/admin/rich_text/editors/draftail/__init__.py index 64c7595c500..aeefa36bdcd 100644 --- a/wagtail/admin/rich_text/editors/draftail/__init__.py +++ b/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 @@ -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 @@ -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