Added logos paragraph renderer
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Mar 2018 17:54:01 +0100
changeset 438 117089568313
parent 437 4a4482e283df
child 439 2a61d39de0fc
Added logos paragraph renderer
src/pyams_content/shared/logo/interfaces/__init__.py
src/pyams_content/shared/logo/paragraph.py
src/pyams_content/shared/logo/zmi/paragraph.py
src/pyams_content/shared/logo/zmi/templates/paragraph-preview.pt
--- a/src/pyams_content/shared/logo/interfaces/__init__.py	Fri Mar 02 17:36:12 2018 +0100
+++ b/src/pyams_content/shared/logo/interfaces/__init__.py	Fri Mar 02 17:54:01 2018 +0100
@@ -12,16 +12,18 @@
 
 __docformat__ = 'restructuredtext'
 
+
 # import standard library
 
 # import interfaces
 from pyams_content.component.paragraph import IBaseParagraph
+from pyams_content.features.renderer.interfaces import IRenderedContent
 from pyams_content.shared.common.interfaces import ISharedTool, IWfSharedContent, ISharedContent
 
 # import packages
 from pyams_file.schema import ImageField
 from pyams_sequence.schema import InternalReferencesList
-from zope.schema import URI
+from zope.schema import URI, Choice
 
 from pyams_content import _
 
@@ -51,14 +53,20 @@
 
 
 LOGOS_PARAGRAPH_TYPE = 'Logos'
+LOGOS_PARAGRAPH_RENDERERS = 'PyAMS.shared.logos.renderers'
 
 
-class ILogosParagraph(IBaseParagraph):
+class ILogosParagraph(IRenderedContent, IBaseParagraph):
     """Logos paragraph"""
 
     references = InternalReferencesList(title=_("Logos references"),
                                         description=_("List of internal logos references"),
                                         content_type=LOGO_CONTENT_TYPE)
 
+    renderer = Choice(title=_("Gallery template"),
+                      description=_("Presentation template used for this gallery"),
+                      vocabulary=LOGOS_PARAGRAPH_RENDERERS,
+                      default='default')
+
     def get_targets(self, state=None):
         """Get references targets"""
--- a/src/pyams_content/shared/logo/paragraph.py	Fri Mar 02 17:36:12 2018 +0100
+++ b/src/pyams_content/shared/logo/paragraph.py	Fri Mar 02 17:54:01 2018 +0100
@@ -17,31 +17,36 @@
 
 # import interfaces
 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE, ERROR_VALUE
-from pyams_content.shared.logo.interfaces import ILogosParagraph, LOGOS_PARAGRAPH_TYPE
+from pyams_content.shared.logo.interfaces import ILogosParagraph, LOGOS_PARAGRAPH_TYPE, LOGOS_PARAGRAPH_RENDERERS
 from pyams_i18n.interfaces import II18nManager, INegotiator, II18n
 from pyams_workflow.interfaces import IWorkflow, IWorkflowState
 
 # import packages
 from pyams_content.component.paragraph import BaseParagraph, IParagraphFactory, BaseParagraphFactory, \
     BaseParagraphContentChecker
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
 from pyams_sequence.utility import get_reference_target
 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(ILogosParagraph)
-class LogosParagraph(BaseParagraph):
+class LogosParagraph(RenderedContentMixin, BaseParagraph):
     """Logos paragraph"""
 
     icon_class = 'fa-th-large'
     icon_hint = _("Logos")
 
     references = FieldProperty(ILogosParagraph['references'])
+    renderer = FieldProperty(ILogosParagraph['renderer'])
 
     def get_targets(self, state=None, with_reference=False):
         for reference in self.references or ():
@@ -103,3 +108,19 @@
                                 message=translate(_("logo '{0}' is not published")).format(
                                     II18n(target).query_attribute('title', request=request))))
         return output
+
+
+@vocabulary_config(name=LOGOS_PARAGRAPH_RENDERERS)
+class LogosParagraphRendererVocabulary(SimpleVocabulary):
+    """Logos paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not ILogosParagraph.providedBy(context):
+            context = LogosParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(LogosParagraphRendererVocabulary, self).__init__(terms)
--- a/src/pyams_content/shared/logo/zmi/paragraph.py	Fri Mar 02 17:36:12 2018 +0100
+++ b/src/pyams_content/shared/logo/zmi/paragraph.py	Fri Mar 02 17:54:01 2018 +0100
@@ -31,9 +31,10 @@
 # import packages
 from pyams_content.component.paragraph.zmi import IParagraphContainerView, BaseParagraphAddMenu, \
     BaseParagraphAJAXAddForm, BaseParagraphPropertiesEditForm, BaseParagraphAJAXEditForm
+from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
 from pyams_content.shared.logo.paragraph import LogosParagraph
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_utils.traversing import get_parent
 from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
@@ -95,6 +96,8 @@
     icon_css_class = 'fa fa-fw fa-th-large'
 
     fields = field.Fields(ILogosParagraph).omit('__parent__', '__name__', 'visible')
+    fields['renderer'].widgetFactory = RendererFieldWidget
+
     ajax_handler = 'properties.json'
     edit_permission = MANAGE_CONTENT_PERMISSION
 
@@ -128,7 +131,8 @@
 
     def get_ajax_output(self, changes):
         output = super(LogosParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
-        if 'references' in changes.get(ILogosParagraph, ()):
+        updated = changes.get(ILogosParagraph, ())
+        if ('references' in updated) or ('renderer' in updated):
             form = LogosParagraphInnerEditForm(self.context, self.request)
             form.update()
             content = form.getContent()
@@ -150,15 +154,5 @@
 #
 
 @adapter_config(context=(ILogosParagraph, IPyAMSLayer), provides=IParagraphPreview)
-@template_config(template='templates/paragraph-preview.pt', layer=IPyAMSLayer)
-class LogosParagraphPreview(BaseContentProvider):
+class LogosParagraphPreview(BaseRenderedContentPreview):
     """Logos paragraph preview"""
-
-    language = None
-
-    def update(self):
-        i18n = II18n(self.context)
-        if self.language:
-            setattr(self, 'title', i18n.get_attribute('title', self.language, request=self.request))
-        else:
-            setattr(self, 'title', i18n.query_attribute('title', request=self.request))
--- a/src/pyams_content/shared/logo/zmi/templates/paragraph-preview.pt	Fri Mar 02 17:36:12 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-<h3 tal:content="view.title">title</h3>
-<div class="padding-10" i18n:domain="pyams_content">
-	<tal:loop repeat="logo context.get_targets()">
-		<tal:if condition="logo is not None">
-			<a tal:omit-tag="not:logo.url"
-			   tal:attributes="href logo.url" target="_blank">
-				<img class="thumbnail margin-10"
-					 tal:define="thumbnails extension:thumbnails(logo.image);
-								 thumbnail thumbnails.get_thumbnail('200x200');"
-					 tal:attributes="src extension:absolute_url(thumbnail)" />
-			</a>
-		</tal:if>
-	</tal:loop>
-</div>