# HG changeset patch # User Thierry Florac # Date 1519989336 -3600 # Node ID edf9ce1b3f69c23c7f263945a8fc910b8354761e # Parent 824fef8088451f63a0731cefc0202deae36c2b76 Added renderer for header paragraph diff -r 824fef808845 -r edf9ce1b3f69 src/pyams_content/component/paragraph/header.py --- a/src/pyams_content/component/paragraph/header.py Fri Mar 02 12:14:11 2018 +0100 +++ b/src/pyams_content/component/paragraph/header.py Fri Mar 02 12:15:36 2018 +0100 @@ -19,22 +19,27 @@ from pyams_content.component.paragraph.interfaces import IParagraphFactory from pyams_content.component.paragraph.interfaces.header import IHeaderParagraph, HEADER_PARAGRAPH_TYPE from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE +from pyams_content.features.renderer.interfaces import IContentRenderer from pyams_i18n.interfaces import II18n, II18nManager, INegotiator # import packages from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory +from pyams_content.features.renderer import RenderedContentMixin from pyams_utils.adapter import adapter_config from pyams_utils.registry import utility_config, get_utility +from pyams_utils.request import check_request from pyams_utils.text import get_text_start from pyams_utils.traversing import get_parent +from pyams_utils.vocabulary import vocabulary_config from zope.interface import implementer from zope.schema.fieldproperty import FieldProperty +from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm from pyams_content import _ @implementer(IHeaderParagraph) -class HeaderParagraph(BaseParagraph): +class HeaderParagraph(RenderedContentMixin, BaseParagraph): """Header paragraph""" icon_class = 'fa-header' @@ -46,6 +51,7 @@ return get_text_start(header, 50, 10) header = FieldProperty(IHeaderParagraph['header']) + renderer = FieldProperty(IHeaderParagraph['renderer']) @utility_config(name=HEADER_PARAGRAPH_TYPE, provides=IParagraphFactory) @@ -79,3 +85,19 @@ else: output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang)) return output + + +@vocabulary_config(name='PyAMS header paragraph renderers') +class HeaderParagraphRendererVocabulary(SimpleVocabulary): + """Header paragraph renderers vocabulary""" + + def __init__(self, context=None): + request = check_request() + translate = request.localizer.translate + registry = request.registry + if not IHeaderParagraph.providedBy(context): + context = HeaderParagraph() + terms = [SimpleTerm(name, title=translate(adapter.label)) + for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer), + key=lambda x: x[1].weight)] + super(HeaderParagraphRendererVocabulary, self).__init__(terms) diff -r 824fef808845 -r edf9ce1b3f69 src/pyams_content/component/paragraph/interfaces/header.py --- a/src/pyams_content/component/paragraph/interfaces/header.py Fri Mar 02 12:14:11 2018 +0100 +++ b/src/pyams_content/component/paragraph/interfaces/header.py Fri Mar 02 12:15:36 2018 +0100 @@ -17,9 +17,11 @@ # import interfaces from pyams_content.component.paragraph.interfaces import IBaseParagraph +from pyams_content.features.renderer import IRenderedContent # import packages from pyams_i18n.schema import I18nTextField +from zope.schema import Choice from pyams_content import _ @@ -31,8 +33,13 @@ HEADER_PARAGRAPH_TYPE = 'Header' -class IHeaderParagraph(IBaseParagraph): +class IHeaderParagraph(IRenderedContent, IBaseParagraph): """Header paragraph""" header = I18nTextField(title=_("Header"), required=False) + + renderer = Choice(title=_("Header template"), + description=_("Presentation template used for this header"), + vocabulary='PyAMS header paragraph renderers', + default='hidden') diff -r 824fef808845 -r edf9ce1b3f69 src/pyams_content/component/paragraph/zmi/header.py --- a/src/pyams_content/component/paragraph/zmi/header.py Fri Mar 02 12:14:11 2018 +0100 +++ b/src/pyams_content/component/paragraph/zmi/header.py Fri Mar 02 12:15:36 2018 +0100 @@ -31,8 +31,8 @@ from pyams_content.component.paragraph.header import HeaderParagraph from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \ BaseParagraphAddMenu, BaseParagraphPropertiesEditForm +from pyams_content.features.renderer.zmi.widget import RendererFieldWidget from pyams_pagelet.pagelet import pagelet_config -from pyams_template.template import template_config from pyams_utils.adapter import adapter_config from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider from pyams_zmi.form import AdminDialogAddForm @@ -62,7 +62,7 @@ legend = _("Add new header paragraph") icon_css_class = 'fa fa-fw fa-header' - fields = field.Fields(IHeaderParagraph).select('header') + fields = field.Fields(IHeaderParagraph).select('header', 'renderer') ajax_handler = 'add-header-paragraph.json' edit_permission = MANAGE_CONTENT_PERMISSION @@ -92,7 +92,9 @@ legend = _("Edit header paragraph properties") icon_css_class = 'fa fa-fw fa-header' - fields = field.Fields(IHeaderParagraph).select('header') + fields = field.Fields(IHeaderParagraph).select('header', 'renderer') + fields['renderer'].widgetFactory = RendererFieldWidget + ajax_handler = 'properties.json' edit_permission = MANAGE_CONTENT_PERMISSION @@ -131,6 +133,8 @@ label_css_class = 'control-label col-md-2' input_css_class = 'col-md-10' + ajax_handler = 'inner-properties.json' + @property def buttons(self): if self.mode == INPUT_MODE: @@ -139,22 +143,51 @@ return button.Buttons() +@view_config(name='inner-properties.json', context=IHeaderParagraph, request_type=IPyAMSLayer, + permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) +class HeaderParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, HeaderParagraphInnerEditForm): + """Header paragraph inner edit form, JSON renderer""" + + def get_ajax_output(self, changes): + output = super(HeaderParagraphInnerAJAXEditForm, self).get_ajax_output(changes) + updated = changes.get(IHeaderParagraph, ()) + if 'renderer' in updated: + form = HeaderParagraphInnerEditForm(self.context, self.request) + form.update() + output.setdefault('events', []).append({ + 'event': 'myams.refresh', + 'options': { + 'object_id': '{0}_{1}_{2}'.format( + self.context.__class__.__name__, + getattr(form.getContent(), '__name__', 'noname').replace('++', ''), + form.id), + 'content': form.render() + } + }) + return output + + # -# HTML paragraph preview +# Header paragraph preview # @adapter_config(context=(IHeaderParagraph, IPyAMSLayer), provides=IParagraphPreview) -@template_config(template='templates/header-preview.pt', layer=IPyAMSLayer) class HeaderParagraphPreview(BaseContentProvider): """Header paragraph preview""" + def __init__(self, context, request): + super(HeaderParagraphPreview, self).__init__(context, request) + self.renderer = self.context.get_renderer() + language = None def update(self): - i18n = II18n(self.context) - if self.language: - for attr in ('header', ): - setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request)) + if self.renderer is not None: + self.renderer.language = self.language + self.renderer.update() + + def render(self): + if self.renderer is not None: + return self.renderer.render() else: - for attr in ('header', ): - setattr(self, attr, i18n.query_attribute(attr, request=self.request)) + return '' diff -r 824fef808845 -r edf9ce1b3f69 src/pyams_content/component/paragraph/zmi/templates/header-preview.pt --- a/src/pyams_content/component/paragraph/zmi/templates/header-preview.pt Fri Mar 02 12:14:11 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -
header