src/pyams_default_theme/component/association/viewlet.py
changeset 181 97e8610cbabd
parent 179 f805794b2500
child 184 6815147a533f
equal deleted inserted replaced
177:610811d72a10 181:97e8610cbabd
       
     1 #
       
     2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     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
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 from zope.interface import Interface
       
    16 
       
    17 from pyams_skin.layer import IPyAMSUserLayer
       
    18 from pyams_content.component.association.interfaces import IAssociationContainer, IAssociationInfo
       
    19 from pyams_content.component.extfile.interfaces import IExtFile
       
    20 from pyams_content.component.links.interfaces import IBaseLink
       
    21 from pyams_template.template import template_config
       
    22 from pyams_viewlet.viewlet import ViewContentProvider, contentprovider_config
       
    23 
       
    24 
       
    25 @contentprovider_config(name='pyams.associations', layer=IPyAMSUserLayer, view=Interface)
       
    26 @template_config(template='templates/association-viewlet.pt', layer=IPyAMSUserLayer)
       
    27 class AssociationContentProvider(ViewContentProvider):
       
    28     """Association default content provider"""
       
    29 
       
    30     links = None
       
    31     attachments = None
       
    32 
       
    33     def __init__(self, *args, **kwargs):
       
    34         super().__init__(*args, **kwargs)
       
    35         self.attachments = []
       
    36         self.links = []
       
    37 
       
    38     def get_associations(self):
       
    39         yield from IAssociationContainer(self.context).get_visible_items(self.request)
       
    40 
       
    41     @staticmethod
       
    42     def get_link_info(link):
       
    43         return IAssociationInfo(link)
       
    44 
       
    45     def update(self):
       
    46         super().update()
       
    47         for item in self.get_associations():
       
    48             if IExtFile.providedBy(item):
       
    49                 self.attachments.append(item)
       
    50             elif IBaseLink.providedBy(item):
       
    51                 self.links.append(item)