Added illustrations paragraph renderer
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Mar 2018 17:36:12 +0100
changeset 437 4a4482e283df
parent 436 3b92ae4b8951
child 438 117089568313
Added illustrations paragraph renderer
src/pyams_content/component/association/interfaces/__init__.py
src/pyams_content/component/association/paragraph.py
src/pyams_content/component/association/zmi/paragraph.py
src/pyams_content/component/association/zmi/templates/paragraph-preview.pt
--- a/src/pyams_content/component/association/interfaces/__init__.py	Fri Mar 02 17:24:06 2018 +0100
+++ b/src/pyams_content/component/association/interfaces/__init__.py	Fri Mar 02 17:36:12 2018 +0100
@@ -17,13 +17,14 @@
 
 # import interfaces
 from pyams_content.component.paragraph.interfaces import IBaseParagraph
+from pyams_content.features.renderer import IRenderedContent
 from zope.annotation.interfaces import IAttributeAnnotatable
 from zope.container.interfaces import IOrderedContainer
 
 # import packages
 from zope.container.constraints import containers, contains
 from zope.interface import Interface, Attribute
-from zope.schema import Bool
+from zope.schema import Bool, Choice
 
 from pyams_content import _
 
@@ -80,7 +81,13 @@
 
 
 ASSOCIATION_PARAGRAPH_TYPE = 'Associations'
+ASSOCIATION_PARAGRAPH_RENDERERS = 'PyAMS.associations.renderers'
 
 
-class IAssociationParagraph(IBaseParagraph):
+class IAssociationParagraph(IRenderedContent, IBaseParagraph):
     """Associations paragraph interface"""
+
+    renderer = Choice(title=_("Associations template"),
+                      description=_("Presentation template used for associations"),
+                      vocabulary=ASSOCIATION_PARAGRAPH_RENDERERS,
+                      default='default')
--- a/src/pyams_content/component/association/paragraph.py	Fri Mar 02 17:24:06 2018 +0100
+++ b/src/pyams_content/component/association/paragraph.py	Fri Mar 02 17:36:12 2018 +0100
@@ -16,31 +16,39 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.association.interfaces import IAssociationParagraph, ASSOCIATION_PARAGRAPH_TYPE
+from pyams_content.component.association.interfaces import IAssociationParagraph, ASSOCIATION_PARAGRAPH_TYPE, \
+    ASSOCIATION_PARAGRAPH_RENDERERS
 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
 from pyams_content.component.links.interfaces import ILinkContainerTarget
 from pyams_content.component.paragraph.interfaces import IParagraphFactory
 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.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(IAssociationParagraph, IExtFileContainerTarget, ILinkContainerTarget)
-class AssociationParagraph(BaseParagraph):
+class AssociationParagraph(RenderedContentMixin, BaseParagraph):
     """Associations paragraph"""
 
     icon_class = 'fa-link'
     icon_hint = _("Associations paragraph")
 
+    renderer = FieldProperty(IAssociationParagraph['renderer'])
+
 
 @utility_config(name=ASSOCIATION_PARAGRAPH_TYPE, provides=IParagraphFactory)
 class AssociationParagraphFactory(BaseParagraphFactory):
@@ -80,3 +88,19 @@
                 else:
                     output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
         return output
+
+
+@vocabulary_config(name=ASSOCIATION_PARAGRAPH_RENDERERS)
+class AssociationParagraphRendererVocabulary(SimpleVocabulary):
+    """Associations paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IAssociationParagraph.providedBy(context):
+            context = AssociationParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(AssociationParagraphRendererVocabulary, self).__init__(terms)
--- a/src/pyams_content/component/association/zmi/paragraph.py	Fri Mar 02 17:24:06 2018 +0100
+++ b/src/pyams_content/component/association/zmi/paragraph.py	Fri Mar 02 17:36:12 2018 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
 
 __docformat__ = 'restructuredtext'
 
@@ -34,6 +35,7 @@
 from pyams_content.component.association.paragraph import AssociationParagraph
 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
@@ -66,7 +68,7 @@
     legend = _("Add new association paragraph")
     icon_css_class = 'fa fa-fw fa-link'
 
-    fields = field.Fields(IAssociationParagraph).select('title')
+    fields = field.Fields(IAssociationParagraph).select('title', 'renderer')
     ajax_handler = 'add-association-paragraph.json'
     edit_permission = MANAGE_CONTENT_PERMISSION
 
@@ -96,7 +98,9 @@
     legend = _("Edit association paragraph properties")
     icon_css_class = 'fa fa-fw fa-link'
 
-    fields = field.Fields(IAssociationParagraph).select('title')
+    fields = field.Fields(IAssociationParagraph).select('title', 'renderer')
+    fields['renderer'].widgetFactory = RendererFieldWidget
+
     ajax_handler = 'properties.json'
     edit_permission = MANAGE_CONTENT_PERMISSION
 
@@ -113,6 +117,7 @@
     """Association paragraph inner edit form"""
 
     legend = None
+    ajax_handler = 'inner-properties.json'
 
     @property
     def buttons(self):
@@ -122,26 +127,34 @@
             return button.Buttons()
 
 
+@view_config(name='inner-properties.json', context=IAssociationParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class AssociationParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, AssociationParagraphInnerEditForm):
+    """Associations paragraph inner edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(AssociationParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
+        updated = changes.get(IAssociationParagraph, ())
+        if 'renderer' in updated:
+            form = AssociationParagraphInnerEditForm(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
+
+
 #
 # Association paragraph preview
 #
 
 @adapter_config(context=(IAssociationParagraph, IPyAMSLayer), provides=IParagraphPreview)
-@template_config(template='templates/paragraph-preview.pt', layer=IPyAMSLayer)
-class AssociationParagraphPreview(BaseContentProvider):
+class AssociationParagraphPreview(BaseRenderedContentPreview):
     """Association paragraph preview"""
-
-    language = None
-    associations = None
-
-    def update(self):
-        i18n = II18n(self.context)
-        if self.language:
-            for attr in ('title', ):
-                setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request))
-        else:
-            for attr in ('title', ):
-                setattr(self, attr, i18n.query_attribute(attr, request=self.request))
-        self.associations = [{'url': item.get_url(self.request),
-                              'title': IAssociationInfo(item).user_title}
-                             for item in IAssociationContainer(self.context).values() if item.visible]
--- a/src/pyams_content/component/association/zmi/templates/paragraph-preview.pt	Fri Mar 02 17:24:06 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-<i18n:var domain="pyams_content">
-	<h3 tal:content="i18n:title">ยง title</h3>
-	<ul>
-		<li tal:repeat="item view.associations">
-			<a tal:attributes="href item['url']"
-			   tal:content="item['title']" target="_blank">Link</a>
-		</li>
-	</ul>
-</i18n:var>