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

[draft] Provide a JsonCompatible adapter for RichTextValue objects #1584

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions news/1583.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide a JsonCompatible adapter for RichTextValue objects
[erral]
1 change: 1 addition & 0 deletions src/plone/restapi/serializer/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<adapter factory=".converters.persistent_list_converter" />
<adapter factory=".converters.persistent_mapping_converter" />
<adapter factory=".converters.python_datetime_converter" />
<adapter factory=".converters.richtextvalue_converter" />
<adapter factory=".converters.RichtextDXContextConverter" />
<adapter factory=".converters.set_converter" />
<adapter factory=".converters.bytes_converter" />
Expand Down
9 changes: 7 additions & 2 deletions src/plone/restapi/serializer/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def default_converter(value):
return value

raise TypeError(
"No converter for making"
" {!r} ({}) JSON compatible.".format(value, type(value))
"No converter for making {!r} ({}) JSON compatible.".format(value, type(value))
)


Expand Down Expand Up @@ -173,6 +172,12 @@ def timedelta_converter(value):
return json_compatible(value.total_seconds())


@adapter(IRichTextValue)
@implementer(IJsonCompatible)
def richtextvalue_converter(value):
return json_compatible(value.output)


@adapter(IRichTextValue, IDexterityContent)
@implementer(IContextawareJsonCompatible)
class RichtextDXContextConverter:
Expand Down
18 changes: 18 additions & 0 deletions src/plone/restapi/tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-

import unittest
from plone.app.textfield import RichTextValue
from plone.restapi.interfaces import IJsonCompatible
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING


class TestConverters(unittest.TestCase):

layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING

def test_richtextvalue_converter(self):
"""test that a RichTextValue is converted to a proper HTML"""
html = "<p>This is a demo HTML</p>"
value = RichTextValue(html, "text/html", "text/html")
json_compatible_value = IJsonCompatible(value)
self.assertEqual(json_compatible_value, html)
40 changes: 36 additions & 4 deletions src/plone/restapi/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class ITaggedValuesSchema(model.Schema):

parametrized_widget_field = schema.TextLine(title="Parametrized widget field")
form.widget(
"parametrized_widget_field", a_param="some_value", defaultFactory=lambda: "Foo"
"parametrized_widget_field",
a_param="some_value",
defaultFactory=lambda: "Foo",
)

not_parametrized_widget_field = schema.TextLine(
Expand Down Expand Up @@ -459,7 +461,11 @@ def test_decimal(self):

def test_int(self):
field = schema.Int(
title="My field", description="My great field", min=0, max=100, default=50
title="My field",
description="My great field",
min=0,
max=100,
default=50,
)
adapter = getMultiAdapter(
(field, self.portal, self.request), IJsonSchemaProvider
Expand Down Expand Up @@ -742,9 +748,33 @@ def test_richtext(self):
adapter.get_schema(),
)

def test_richtext_with_default(self):
field = RichText(
title="My field",
description="My great field",
default="<p>Some default value</p>",
)
adapter = getMultiAdapter(
(field, self.portal, self.request), IJsonSchemaProvider
)

self.assertEqual(
{
"type": "string",
"title": "My field",
"factory": "Rich Text",
"description": "My great field",
"widget": "richtext",
"default": "<p>Some default value</p>",
},
adapter.get_schema(),
)

def test_date(self):
field = schema.Date(
title="My field", description="My great field", default=date(2016, 1, 1)
title="My field",
description="My great field",
default=date(2016, 1, 1),
)
adapter = getMultiAdapter(
(field, self.portal, self.request), IJsonSchemaProvider
Expand Down Expand Up @@ -781,7 +811,9 @@ def test_datetime(self):

def test_jsonfield(self):
field = JSONField(
title="My field", description="My great field", widget="my_widget_name"
title="My field",
description="My great field",
widget="my_widget_name",
)
adapter = getMultiAdapter(
(field, self.portal, self.request), IJsonSchemaProvider
Expand Down
Loading