src/pyams_content/features/share/container.py
changeset 1138 6de2ab88b4fe
child 1239 b6d9396beffd
equal deleted inserted replaced
1137:b8068bb58b9e 1138:6de2ab88b4fe
       
     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.container.ordered import OrderedContainer
       
    16 from zope.interface import implementer
       
    17 from zope.location import locate
       
    18 from zope.location.interfaces import ISublocations
       
    19 from zope.traversing.interfaces import ITraversable
       
    20 
       
    21 from pyams_catalog.utils import index_object
       
    22 from pyams_content.features.share import ISocialShareItem
       
    23 from pyams_content.features.share.interfaces import ISocialShareManager, ISocialShareManagerTarget, \
       
    24     SOCIAL_SHARE_MANAGER_KEY
       
    25 from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
       
    26 
       
    27 
       
    28 @implementer(ISocialShareManager)
       
    29 class SocialShareManager(OrderedContainer):
       
    30     """Social network share manager"""
       
    31 
       
    32     last_id = 1
       
    33 
       
    34     def append(self, value, notify=True):
       
    35         key = str(self.last_id)
       
    36         if not notify:
       
    37             # pre-locate item to avoid multiple notifications
       
    38             locate(value, self, key)
       
    39         self[key] = value
       
    40         self.last_id += 1
       
    41         if not notify:
       
    42             # make sure that item is correctly indexed
       
    43             index_object(value)
       
    44 
       
    45     def get_active_items(self):
       
    46         yield from filter(lambda x: ISocialShareItem(x).active, self.values())
       
    47 
       
    48 
       
    49 @adapter_config(context=ISocialShareManagerTarget, provides=ISocialShareManager)
       
    50 def social_share_manager_factory(context):
       
    51     """Social network share manager factory"""
       
    52     return get_annotation_adapter(context, SOCIAL_SHARE_MANAGER_KEY, SocialShareManager, name='++social-share++')
       
    53 
       
    54 
       
    55 @adapter_config(name='social-share', context=ISocialShareManagerTarget, provides=ITraversable)
       
    56 class SocialShareManagerNamespace(ContextAdapter):
       
    57     """Social network share manager ++social-share++ namespace"""
       
    58 
       
    59     def traverse(self, name, furtherpath=None):
       
    60         return ISocialShareManager(self.context)
       
    61 
       
    62 
       
    63 @adapter_config(name='social-share', context=ISocialShareManagerTarget, provides=ISublocations)
       
    64 class SocialShareManagerSublocations(ContextAdapter):
       
    65     """Social network share manager sub-locations adapter"""
       
    66 
       
    67     def sublocations(self):
       
    68         return ISocialShareManager(self.context).values()