Add rich_text form data helper

This commit is contained in:
Matt Westcott 2018-02-14 16:38:10 +00:00
parent 067c26f54d
commit c205c8fac6
5 changed files with 77 additions and 9 deletions

View file

@ -97,6 +97,8 @@ Form data helpers
.. autofunction:: nested_form_data
.. autofunction:: rich_text
.. autofunction:: streamfield
.. autofunction:: inline_formset

View file

@ -33,16 +33,22 @@ class DraftailRichTextArea(WidgetWithScript, widgets.HiddenInput):
super().__init__(*args, **kwargs)
def translate_value(self, value):
# Convert database rich text representation to the format required by
# the input field
if value is None:
value = ''
return self.converter.from_database_format(value)
def render(self, name, value, attrs=None):
if attrs is None:
attrs = {}
attrs['data-draftail-input'] = True
if value is None:
value = ''
translated_value = self.converter.from_database_format(value)
translated_value = self.translate_value(value)
return super().render(name, translated_value, attrs)
def render_js_init(self, id_, name, value):

View file

@ -102,11 +102,16 @@ class HalloRichTextArea(WidgetWithScript, widgets.Textarea):
super().__init__(*args, **kwargs)
def render(self, name, value, attrs=None):
def translate_value(self, value):
# Convert database rich text representation to the format required by
# the input field
if value is None:
translated_value = None
else:
translated_value = self.converter.from_database_format(value)
return None
return self.converter.from_database_format(value)
def render(self, name, value, attrs=None):
translated_value = self.translate_value(value)
return super().render(name, translated_value, attrs)
def render_js_init(self, id_, name, value):

View file

@ -1,11 +1,14 @@
import json
from django.test import TestCase
from wagtail.admin.tests.test_contentstate import content_state_equal
from wagtail.core.models import PAGE_MODEL_CLASSES, Page, Site
from wagtail.tests.testapp.models import (
BusinessChild, BusinessIndex, BusinessNowherePage, BusinessSubIndex, EventIndex, EventPage,
SimplePage, StreamPage)
from wagtail.tests.utils import WagtailPageTests, WagtailTestUtils
from wagtail.tests.utils.form_data import inline_formset, nested_form_data, streamfield
from wagtail.tests.utils.form_data import inline_formset, nested_form_data, rich_text, streamfield
class TestAssertTagInHTML(WagtailTestUtils, TestCase):
@ -195,3 +198,34 @@ class TestFormDataHelpers(TestCase):
'lines-1-DELETE': '',
}
)
def test_default_rich_text(self):
result = rich_text('<h2>title</h2><p>para</p>')
self.assertTrue(content_state_equal(
json.loads(result),
{
'entityMap': {},
'blocks': [
{'inlineStyleRanges': [], 'text': 'title', 'depth': 0, 'type': 'header-two', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'para', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
]
}
))
def test_rich_text_with_custom_features(self):
# feature list doesn't allow <h2>, so it should become an unstyled paragraph block
result = rich_text('<h2>title</h2><p>para</p>', features=['p'])
self.assertTrue(content_state_equal(
json.loads(result),
{
'entityMap': {},
'blocks': [
{'inlineStyleRanges': [], 'text': 'title', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'para', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
]
}
))
def test_rich_text_with_alternative_editor(self):
result = rich_text('<h2>title</h2><p>para</p>', editor='hallo')
self.assertEqual(result, '<h2>title</h2><p>para</p>')

View file

@ -6,6 +6,8 @@ the ``wagtail.tests.utils.form_data`` module provides a set of helper
functions to assist with this.
"""
from wagtail.admin.rich_text import get_rich_text_editor_widget
def _nested_form_data(data):
if isinstance(data, dict):
@ -113,3 +115,22 @@ def inline_formset(items, initial=0, min=0, max=1000):
'MAX_NUM_FORMS': str(max),
})
return data_dict
def rich_text(value, editor='default', features=None):
"""
Converts an HTML-like rich text string to the data format required by
the currently active rich text editor.
:param editor: An alternative editor name as defined in ``WAGTAILADMIN_RICH_TEXT_EDITORS``
:param features: A list of features allowed in the rich text content (see :ref:`rich_text_features`)
.. code-block:: python
self.assertCanCreate(root_page, ContentPage, nested_form_data({
'title': 'About us',
'body': rich_text('<p>Lorem ipsum dolor sit amet</p>'),
}))
"""
widget = get_rich_text_editor_widget(editor, features)
return widget.translate_value(value)