Browse Source

Use DjangoJSONEncoder instead of custom LazyStringEncoder

Sage Abdullah 11 months ago
parent
commit
56e69bc3ea
3 changed files with 4 additions and 21 deletions
  1. 1 0
      CHANGELOG.txt
  2. 1 1
      docs/releases/6.2.md
  3. 2 20
      wagtail/admin/rich_text/editors/draftail/__init__.py

+ 1 - 0
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

+ 1 - 1
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

+ 2 - 20
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.telepath import register
 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 @@ class DraftailRichTextArea(widgets.HiddenInput):
         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