Added renderer for key points paragraph
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Mar 2018 12:43:43 +0100
changeset 421 20a2b671ade1
parent 420 edf9ce1b3f69
child 422 38fa157bfc5f
Added renderer for key points paragraph
src/pyams_content/component/paragraph/interfaces/keypoint.py
src/pyams_content/component/paragraph/keypoint.py
src/pyams_content/component/paragraph/zmi/keypoint.py
src/pyams_content/component/paragraph/zmi/templates/keypoints-preview.pt
--- 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')
--- 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)
--- 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 ''
--- 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 @@
-<div class="margin-bottom-10"
-	 tal:define="keypoints view.keypoints"
-	 tal:condition="keypoints">
-	<ul class="inside">
-		<li tal:repeat="item keypoints"
-			tal:content="item">item</li>
-	</ul>
-</div>
\ No newline at end of file