当前位置: 首页>>代码示例>>Python>>正文


Python ContentstateConverter.to_database_format方法代码示例

本文整理汇总了Python中wagtail.admin.rich_text.converters.contentstate.ContentstateConverter.to_database_format方法的典型用法代码示例。如果您正苦于以下问题:Python ContentstateConverter.to_database_format方法的具体用法?Python ContentstateConverter.to_database_format怎么用?Python ContentstateConverter.to_database_format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wagtail.admin.rich_text.converters.contentstate.ContentstateConverter的用法示例。


在下文中一共展示了ContentstateConverter.to_database_format方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: DraftailRichTextArea

# 需要导入模块: from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter [as 别名]
# 或者: from wagtail.admin.rich_text.converters.contentstate.ContentstateConverter import to_database_format [as 别名]
class DraftailRichTextArea(WidgetWithScript, widgets.HiddenInput):
    # this class's constructor accepts a 'features' kwarg
    accepts_features = True

    def get_panel(self):
        return RichTextFieldPanel

    def __init__(self, *args, **kwargs):
        # note: this constructor will receive an 'options' kwarg taken from the WAGTAILADMIN_RICH_TEXT_EDITORS setting,
        # but we don't currently recognise any options from there (other than 'features', which is passed here as a separate kwarg)
        self.options = {}

        self.features = kwargs.pop('features', None)
        if self.features is None:
            self.features = feature_registry.get_default_features()

        for feature in self.features:
            plugin = feature_registry.get_editor_plugin('draftail', feature)
            if plugin:
                plugin.construct_options(self.options)

        self.converter = ContentstateConverter(self.features)

        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

        translated_value = self.translate_value(value)
        return super().render(name, translated_value, attrs)

    def render_js_init(self, id_, name, value):
        return "window.draftail.initEditor('#{id}', {opts}, document.currentScript)".format(
            id=id_, opts=json.dumps(self.options))

    def value_from_datadict(self, data, files, name):
        original_value = super().value_from_datadict(data, files, name)
        if original_value is None:
            return None
        return self.converter.to_database_format(original_value)

    @property
    def media(self):
        return Media(js=[
            'wagtailadmin/js/draftail.js',
        ], css={
            'all': ['wagtailadmin/css/panels/draftail.css']
        })
开发者ID:sprymix,项目名称:wagtail,代码行数:62,代码来源:__init__.py

示例2: DraftailRichTextArea

# 需要导入模块: from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter [as 别名]
# 或者: from wagtail.admin.rich_text.converters.contentstate.ContentstateConverter import to_database_format [as 别名]
class DraftailRichTextArea(widgets.HiddenInput):
    template_name = 'wagtailadmin/widgets/draftail_rich_text_area.html'

    # this class's constructor accepts a 'features' kwarg
    accepts_features = True

    def get_panel(self):
        return RichTextFieldPanel

    def __init__(self, *args, **kwargs):
        # note: this constructor will receive an 'options' kwarg taken from the WAGTAILADMIN_RICH_TEXT_EDITORS setting,
        # but we don't currently recognise any options from there (other than 'features', which is passed here as a separate kwarg)
        kwargs.pop('options', None)
        self.options = {}

        self._media = Media(js=[
            'wagtailadmin/js/draftail.js',
        ], css={
            'all': ['wagtailadmin/css/panels/draftail.css']
        })

        self.features = kwargs.pop('features', None)
        if self.features is None:
            self.features = feature_registry.get_default_features()

        for feature in self.features:
            plugin = feature_registry.get_editor_plugin('draftail', feature)
            if plugin:
                plugin.construct_options(self.options)
                self._media += plugin.media

        self.converter = ContentstateConverter(self.features)

        default_attrs = {'data-draftail-input': True}
        attrs = kwargs.get('attrs')
        if attrs:
            default_attrs.update(attrs)
        kwargs['attrs'] = default_attrs

        super().__init__(*args, **kwargs)

    def format_value(self, value):
        # Convert database rich text representation to the format required by
        # the input field
        value = super().format_value(value)

        if value is None:
            value = ''

        return self.converter.from_database_format(value)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        context['widget']['options_json'] = json.dumps(self.options)
        return context

    def value_from_datadict(self, data, files, name):
        original_value = super().value_from_datadict(data, files, name)
        if original_value is None:
            return None
        return self.converter.to_database_format(original_value)

    @property
    def media(self):
        return self._media
开发者ID:BertrandBordage,项目名称:wagtail,代码行数:67,代码来源:__init__.py


注:本文中的wagtail.admin.rich_text.converters.contentstate.ContentstateConverter.to_database_format方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。