src/pyams_default_theme/component/association/__init__.py
branchdev-dc
changeset 275 ed29fe62235b
parent 204 713edde7dbf1
child 277 4064779f9ab0
equal deleted inserted replaced
274:39bf2d8df31d 275:ed29fe62235b
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
       
    12 from itertools import islice
    12 
    13 
    13 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    14 
    15 
    15 from persistent import Persistent
    16 from persistent import Persistent
    16 from zope.interface import implementer
    17 from zope.interface import implementer
    21     IAssociationParagraph
    22     IAssociationParagraph
    22 from pyams_content.component.extfile import IExtFile
    23 from pyams_content.component.extfile import IExtFile
    23 from pyams_content.component.links.interfaces import IBaseLink, IInternalLink
    24 from pyams_content.component.links.interfaces import IBaseLink, IInternalLink
    24 from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget
    25 from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget
    25 from pyams_content.features.renderer.interfaces import IContentRenderer
    26 from pyams_content.features.renderer.interfaces import IContentRenderer
    26 from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings
    27 from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings, \
       
    28     IAssociationParagraphSlicedRemoteContentRendererSettings
    27 from pyams_default_theme.features.renderer import BaseContentRenderer
    29 from pyams_default_theme.features.renderer import BaseContentRenderer
    28 from pyams_skin.layer import IPyAMSLayer
    30 from pyams_skin.layer import IPyAMSLayer
    29 from pyams_template.template import template_config
    31 from pyams_template.template import template_config
    30 from pyams_utils.adapter import adapter_config, get_annotation_adapter
    32 from pyams_utils.adapter import adapter_config, get_annotation_adapter
    31 
    33 
    68 #
    70 #
    69 # Associations paragraph remote content renderer
    71 # Associations paragraph remote content renderer
    70 #
    72 #
    71 
    73 
    72 ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content'
    74 ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content'
       
    75 ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content-sliced'
    73 
    76 
    74 
    77 
    75 @implementer(IAssociationParagraphRemoteContentRendererSettings)
    78 @implementer(IAssociationParagraphRemoteContentRendererSettings)
    76 class AssociationParagraphRemoteContentRendererSettings(Persistent, Location):
    79 class AssociationParagraphRemoteContentRendererSettings(Persistent, Location):
    77     """Associations paragraph remote content renderer settings"""
    80     """Associations paragraph remote content renderer settings"""
    78 
    81 
    79     display_title = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_title'])
    82     display_title = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_title'])
    80     display_header = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_header'])
    83     display_header = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['display_header'])
    81     factories = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['factories'])
    84     factories = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['factories'])
    82     anchors_only = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['anchors_only'])
    85     anchors_only = FieldProperty(IAssociationParagraphRemoteContentRendererSettings['anchors_only'])
       
    86 
       
    87 
       
    88 @implementer(IAssociationParagraphSlicedRemoteContentRendererSettings)
       
    89 class AssociationParagraphSlicedRemoteContentRendererSettings(Persistent, Location):
       
    90     """Associations paragraph sliced remote content renderer settings"""
       
    91 
       
    92     display_title = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_title'])
       
    93     display_header = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_header'])
       
    94     display_n_paragraphs = FieldProperty(IAssociationParagraphSlicedRemoteContentRendererSettings['display_n_paragraphs'])
    83 
    95 
    84 
    96 
    85 @adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphRemoteContentRendererSettings)
    97 @adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphRemoteContentRendererSettings)
    86 def association_paragraph_remote_content_renderer_settings_factory(context):
    98 def association_paragraph_remote_content_renderer_settings_factory(context):
    87     """Associations paragraph remote content renderer settings factory"""
    99     """Associations paragraph remote content renderer settings factory"""
   114             for renderer in map(lambda x: x.get_renderer(self.request),
   126             for renderer in map(lambda x: x.get_renderer(self.request),
   115                                 container.get_visible_paragraphs(None, settings.anchors_only, settings.factories)):
   127                                 container.get_visible_paragraphs(None, settings.anchors_only, settings.factories)):
   116                 if renderer is not None:
   128                 if renderer is not None:
   117                     renderer.update()
   129                     renderer.update()
   118                     yield renderer.render()
   130                     yield renderer.render()
       
   131 
       
   132 
       
   133 @adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphSlicedRemoteContentRendererSettings)
       
   134 def association_paragraph_sliced_remote_content_renderer_settings_factory(context):
       
   135     """Associations paragraph sliced remote content renderer settings factory"""
       
   136     return get_annotation_adapter(context, ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY,
       
   137                                   AssociationParagraphSlicedRemoteContentRendererSettings)
       
   138 
       
   139 
       
   140 @adapter_config(name='reportage', context=(IAssociationParagraph, IPyAMSLayer), provides=IContentRenderer)
       
   141 @template_config(template='templates/association-remote-content.pt', layer=IPyAMSLayer)
       
   142 class AssociationParagraphSlicedRemoteContentRenderer(BaseContentRenderer):
       
   143     """Associations container sliced remote content renderer"""
       
   144 
       
   145     label = _("Include slice remote content")
       
   146     weight = 11
       
   147 
       
   148     i18n_context_attrs = ('title',)
       
   149     links = ()
       
   150 
       
   151     settings_interface = IAssociationParagraphSlicedRemoteContentRendererSettings
       
   152 
       
   153     def update(self):
       
   154         super(AssociationParagraphSlicedRemoteContentRenderer, self).update()
       
   155         self.links = [item for item in IAssociationContainer(self.context).get_visible_items(self.request)
       
   156                       if IInternalLink.providedBy(item) and IParagraphContainerTarget.providedBy(item.target)]
       
   157 
       
   158     def get_renderers(self, target):
       
   159         container = IParagraphContainer(target, None)
       
   160         if container is not None:
       
   161             settings = self.settings
       
   162             n = settings.display_n_paragraphs
       
   163             for renderer in islice(map(lambda x: x.get_renderer(self.request),
       
   164                                     container.get_visible_paragraphs()), n):
       
   165                 if renderer is not None:
       
   166                     renderer.update()
       
   167                     yield renderer.render()
       
   168