src/pyams_default_theme/component/association/__init__.py
changeset 124 62632a9571d5
parent 28 1264ca1778e4
child 130 f4f8dccfee8b
--- a/src/pyams_default_theme/component/association/__init__.py	Mon Sep 03 11:22:03 2018 +0200
+++ b/src/pyams_default_theme/component/association/__init__.py	Mon Sep 03 16:07:19 2018 +0200
@@ -18,13 +18,20 @@
 # import interfaces
 from pyams_content.component.association.interfaces import IAssociationParagraph, IAssociationInfo, \
     IAssociationContainer
+from pyams_content.component.links.interfaces import IInternalLink
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer
 from pyams_content.features.renderer.interfaces import IContentRenderer
+from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings
 from pyams_skin.layer import IPyAMSLayer
 
 # import packages
+from persistent import Persistent
 from pyams_content.features.renderer.skin import BaseContentRenderer
 from pyams_template.template import template_config
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from zope.interface import implementer
+from zope.location import Location
+from zope.schema.fieldproperty import FieldProperty
 
 from pyams_default_theme import _
 
@@ -39,6 +46,7 @@
     """Associations paragraph default renderer"""
 
     label = _("Default associations renderer")
+    associations = ()
 
     i18n_context_attrs = ('title', )
 
@@ -46,4 +54,52 @@
         super(AssociationParagraphDefaultRenderer, self).update()
         self.associations = [{'url': item.get_url(self.request),
                               'title': IAssociationInfo(item).user_title}
-                             for item in IAssociationContainer(self.context).values() if item.visible]
+                             for item in IAssociationContainer(self.context).get_visible_items(self.request)]
+
+
+#
+# Associations paragraph remote content renderer
+#
+
+ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content'
+
+
+@implementer(IAssociationParagraphRemoteContentRendererSettings)
+class AssociationParagraphRemoteContentRendererSettings(Persistent, Location):
+    """Associations paragraph remote content renderer settings"""
+
+    display_title = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_title'])
+    display_header = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_header'])
+    anchors_only = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['anchors_only'])
+    factories = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['factories'])
+
+
+@adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphRemoteContentRendererSettings)
+def association_paragraph_remote_content_renderer_settings_factory(context):
+    """Associations paragraph remote content renderer settings factory"""
+    return get_annotation_adapter(context, ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY,
+                                  AssociationParagraphRemoteContentRendererSettings)
+
+
+@adapter_config(name='remote-content', context=(IAssociationParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/association-remote-content.pt', layer=IPyAMSLayer)
+class AssociationParagraphRemoteContentRenderer(BaseContentRenderer):
+    """Associations container remote content renderer"""
+
+    label = _("Include remote content")
+
+    i18n_context_attrs = ('title', )
+    links = ()
+
+    settings_interface = IAssociationParagraphRemoteContentRendererSettings
+
+    def update(self):
+        super(AssociationParagraphRemoteContentRenderer, 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_paragraphs(self, target):
+        container = IParagraphContainer(target, None)
+        if container is not None:
+            settings = self.settings
+            yield from container.get_visible_paragraphs(settings.anchors_only, settings.factories)