# HG changeset patch # User Thierry Florac # Date 1519991023 -3600 # Node ID 20a2b671ade16b0d055d18eca282f05e43c1bdfd # Parent edf9ce1b3f69c23c7f263945a8fc910b8354761e Added renderer for key points paragraph diff -r edf9ce1b3f69 -r 20a2b671ade1 src/pyams_content/component/paragraph/interfaces/keypoint.py --- a/src/pyams_content/component/paragraph/interfaces/keypoint.py Fri Mar 02 12:15:36 2018 +0100 +++ b/src/pyams_content/component/paragraph/interfaces/keypoint.py Fri Mar 02 12:43:43 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 _ @@ -29,11 +31,17 @@ # KEYPOINTS_PARAGRAPH_TYPE = 'Keypoints' +KEYPOINTS_PARAGRAPH_RENDERERS = 'PyAMS.paragraph.keypoint.renderer' -class IKeypointsParagraph(IBaseParagraph): - """Keypoints paragraph""" +class IKeypointsParagraph(IRenderedContent, IBaseParagraph): + """Key points paragraph""" body = I18nTextField(title=_("Key points"), description=_("Enter one key point by line, without hyphen or prefix"), required=False) + + renderer = Choice(title=_("Presentation template"), + description=_("Presentation template used for this paragraph"), + vocabulary=KEYPOINTS_PARAGRAPH_RENDERERS, + default='hidden') diff -r edf9ce1b3f69 -r 20a2b671ade1 src/pyams_content/component/paragraph/keypoint.py --- a/src/pyams_content/component/paragraph/keypoint.py Fri Mar 02 12:15:36 2018 +0100 +++ b/src/pyams_content/component/paragraph/keypoint.py Fri Mar 02 12:43:43 2018 +0100 @@ -17,24 +17,30 @@ # import interfaces from pyams_content.component.paragraph.interfaces import IParagraphFactory -from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE +from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE, \ + KEYPOINTS_PARAGRAPH_RENDERERS 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(IKeypointsParagraph) -class KeypointsParagraph(BaseParagraph): +class KeypointsParagraph(RenderedContentMixin, BaseParagraph): """Key points paragraph""" icon_class = 'fa-key' @@ -46,6 +52,7 @@ return get_text_start(body, 50, 10) body = FieldProperty(IKeypointsParagraph['body']) + renderer = FieldProperty(IKeypointsParagraph['renderer']) @utility_config(name=KEYPOINTS_PARAGRAPH_TYPE, provides=IParagraphFactory) @@ -80,3 +87,19 @@ else: output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang)) return output + + +@vocabulary_config(name=KEYPOINTS_PARAGRAPH_RENDERERS) +class KeypointsParagraphRendererVocabulary(SimpleVocabulary): + """Key points paragraph renderers vocabulary""" + + def __init__(self, context=None): + request = check_request() + translate = request.localizer.translate + registry = request.registry + if not IKeypointsParagraph.providedBy(context): + context = KeypointsParagraph() + terms = [SimpleTerm(name, title=translate(adapter.label)) + for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer), + key=lambda x: x[1].weight)] + super(KeypointsParagraphRendererVocabulary, self).__init__(terms) diff -r edf9ce1b3f69 -r 20a2b671ade1 src/pyams_content/component/paragraph/zmi/keypoint.py --- a/src/pyams_content/component/paragraph/zmi/keypoint.py Fri Mar 02 12:15:36 2018 +0100 +++ b/src/pyams_content/component/paragraph/zmi/keypoint.py Fri Mar 02 12:43:43 2018 +0100 @@ -31,6 +31,7 @@ from pyams_content.component.paragraph.keypoint import KeypointsParagraph 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 @@ -62,7 +63,7 @@ legend = _("Add new key points paragraph") icon_css_class = 'fa fa-fw fa-key' - fields = field.Fields(IKeypointsParagraph).select('body') + fields = field.Fields(IKeypointsParagraph).select('body', 'renderer') ajax_handler = 'add-keypoints-paragraph.json' edit_permission = MANAGE_CONTENT_PERMISSION @@ -92,7 +93,9 @@ legend = _("Edit key points paragraph properties") icon_css_class = 'fa fa-fw fa-key' - fields = field.Fields(IKeypointsParagraph).select('body') + fields = field.Fields(IKeypointsParagraph).select('body', 'renderer') + fields['renderer'].widgetFactory = RendererFieldWidget + ajax_handler = 'properties.json' edit_permission = MANAGE_CONTENT_PERMISSION @@ -131,6 +134,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,26 +144,51 @@ return button.Buttons() +@view_config(name='inner-properties.json', context=IKeypointsParagraph, request_type=IPyAMSLayer, + permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) +class KeypointsParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, KeypointsParagraphInnerEditForm): + """Key points paragraph inner edit form, JSON renderer""" + + def get_ajax_output(self, changes): + output = super(KeypointsParagraphInnerAJAXEditForm, self).get_ajax_output(changes) + updated = changes.get(IKeypointsParagraph, ()) + if 'renderer' in updated: + form = KeypointsParagraphInnerEditForm(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 + + # # Key points paragraph preview # @adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphPreview) -@template_config(template='templates/keypoints-preview.pt', layer=IPyAMSLayer) class KeypointsParagraphPreview(BaseContentProvider): """Key points paragraph preview""" + def __init__(self, context, request): + super(KeypointsParagraphPreview, 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 ('body', ): - 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 ('body', ): - setattr(self, attr, i18n.query_attribute(attr, request=self.request)) - - @property - def keypoints(self): - return (self.body or '').split('\n') \ No newline at end of file + return '' diff -r edf9ce1b3f69 -r 20a2b671ade1 src/pyams_content/component/paragraph/zmi/templates/keypoints-preview.pt --- a/src/pyams_content/component/paragraph/zmi/templates/keypoints-preview.pt Fri Mar 02 12:15:36 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -
- -
\ No newline at end of file