Added new remote content paragraph renderer dev-dc
authorDamien Correia
Tue, 04 Dec 2018 15:59:30 +0100
branchdev-dc
changeset 275 ed29fe62235b
parent 274 39bf2d8df31d
child 276 283807967468
Added new remote content paragraph renderer
src/pyams_default_theme/component/association/__init__.py
src/pyams_default_theme/component/association/interfaces/__init__.py
--- a/src/pyams_default_theme/component/association/__init__.py	Tue Dec 04 15:53:25 2018 +0100
+++ b/src/pyams_default_theme/component/association/__init__.py	Tue Dec 04 15:59:30 2018 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from itertools import islice
 
 __docformat__ = 'restructuredtext'
 
@@ -23,7 +24,8 @@
 from pyams_content.component.links.interfaces import IBaseLink, IInternalLink
 from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget
 from pyams_content.features.renderer.interfaces import IContentRenderer
-from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings
+from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings, \
+    IAssociationParagraphSlicedRemoteContentRendererSettings
 from pyams_default_theme.features.renderer import BaseContentRenderer
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
@@ -70,6 +72,7 @@
 #
 
 ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content'
+ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content-sliced'
 
 
 @implementer(IAssociationParagraphRemoteContentRendererSettings)
@@ -82,6 +85,15 @@
     anchors_only = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['anchors_only'])
 
 
+@implementer(IAssociationParagraphSlicedRemoteContentRendererSettings)
+class AssociationParagraphSlicedRemoteContentRendererSettings(Persistent, Location):
+    """Associations paragraph sliced remote content renderer settings"""
+
+    display_title = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_title'])
+    display_header = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_header'])
+    display_n_paragraphs = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_n_paragraphs'])
+
+
 @adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphRemoteContentRendererSettings)
 def association_paragraph_remote_content_renderer_settings_factory(context):
     """Associations paragraph remote content renderer settings factory"""
@@ -116,3 +128,41 @@
                 if renderer is not None:
                     renderer.update()
                     yield renderer.render()
+
+
+@adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphSlicedRemoteContentRendererSettings)
+def association_paragraph_sliced_remote_content_renderer_settings_factory(context):
+    """Associations paragraph sliced remote content renderer settings factory"""
+    return get_annotation_adapter(context, ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY,
+                                  AssociationParagraphSlicedRemoteContentRendererSettings)
+
+
+@adapter_config(name='reportage', context=(IAssociationParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/association-remote-content.pt', layer=IPyAMSLayer)
+class AssociationParagraphSlicedRemoteContentRenderer(BaseContentRenderer):
+    """Associations container sliced remote content renderer"""
+
+    label = _("Include slice remote content")
+    weight = 11
+
+    i18n_context_attrs = ('title',)
+    links = ()
+
+    settings_interface = IAssociationParagraphSlicedRemoteContentRendererSettings
+
+    def update(self):
+        super(AssociationParagraphSlicedRemoteContentRenderer, self).update()
+        self.links = [item for item in IAssociationContainer(self.context).get_visible_items(self.request)
+                      if IInternalLink.providedBy(item) and IParagraphContainerTarget.providedBy(item.target)]
+
+    def get_renderers(self, target):
+        container = IParagraphContainer(target, None)
+        if container is not None:
+            settings = self.settings
+            n = settings.display_n_paragraphs
+            for renderer in islice(map(lambda x: x.get_renderer(self.request),
+                                    container.get_visible_paragraphs()), n):
+                if renderer is not None:
+                    renderer.update()
+                    yield renderer.render()
+
--- a/src/pyams_default_theme/component/association/interfaces/__init__.py	Tue Dec 04 15:53:25 2018 +0100
+++ b/src/pyams_default_theme/component/association/interfaces/__init__.py	Tue Dec 04 15:59:30 2018 +0100
@@ -17,14 +17,14 @@
 
 # import packages
 from zope.interface import Interface
-from zope.schema import Bool, Set, Choice
+from zope.schema import Bool, Set, Choice, Int
 
 # import interfaces
 from pyams_content.component.paragraph.interfaces import PARAGRAPH_FACTORIES_VOCABULARY
 from pyams_default_theme import _
 
 
-class IAssociationParagraphRemoteContentRendererSettings(Interface):
+class IBaseAssociationParagraphRemoteContentRendererSettings(Interface):
     """Associations paragraph remote content renderer settings interface"""
 
     display_title = Bool(title=_("Display title?"),
@@ -37,6 +37,19 @@
                           required=True,
                           default=False)
 
+
+class IAssociationParagraphSlicedRemoteContentRendererSettings(IBaseAssociationParagraphRemoteContentRendererSettings):
+    """Associations paragraph special reportage content renderer settings interface"""
+
+    display_n_paragraphs = Int(title=_("N first paragraphs"),
+                               description=_("Select the N first paragraphs found for each remote content (default: N=1)"),
+                               required=False,
+                               default=1)
+
+
+class IAssociationParagraphRemoteContentRendererSettings(IBaseAssociationParagraphRemoteContentRendererSettings):
+    """Associations paragraph remote content renderer settings interface"""
+
     factories = Set(title=_("Paragraph types"),
                     description=_("Select list of paragraph types you want to include; an empty "
                                   "selection means that all paragraphs will be selected"),