src/pyams_content/component/gallery/container.py
changeset 0 7c0001cacf8e
child 60 da1454d7d358
equal deleted inserted replaced
-1:000000000000 0:7c0001cacf8e
       
     1 #
       
     2 # Copyright (c) 2008-2015 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 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_content.component.gallery.interfaces import IGalleryContainer, IGalleryContainerTarget, \
       
    20     GALLERY_CONTAINER_KEY, IGalleryLinksContainer, IGalleryLinksContainerTarget, GALLERY_LINKS_CONTAINER_KEY
       
    21 from zope.annotation.interfaces import IAnnotations
       
    22 from zope.location.interfaces import ISublocations
       
    23 from zope.schema.interfaces import IVocabularyFactory
       
    24 from zope.traversing.interfaces import ITraversable
       
    25 
       
    26 # import packages
       
    27 from persistent import Persistent
       
    28 from persistent.list import PersistentList
       
    29 from pyams_i18n.interfaces import II18n
       
    30 from pyams_utils.adapter import adapter_config, ContextAdapter
       
    31 from pyams_utils.traversing import get_parent
       
    32 from pyramid.threadlocal import get_current_registry
       
    33 from zope.container.contained import Contained
       
    34 from zope.container.folder import Folder
       
    35 from zope.interface import implementer, provider
       
    36 from zope.lifecycleevent import ObjectCreatedEvent
       
    37 from zope.location import locate
       
    38 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
       
    39 
       
    40 
       
    41 #
       
    42 # Galleries container
       
    43 #
       
    44 
       
    45 @implementer(IGalleryContainer)
       
    46 class GalleryContainer(Folder):
       
    47     """Galleries container"""
       
    48 
       
    49     last_id = 1
       
    50 
       
    51     def __setitem__(self, key, value):
       
    52         key = str(self.last_id)
       
    53         super(GalleryContainer, self).__setitem__(key, value)
       
    54         self.last_id += 1
       
    55 
       
    56 
       
    57 @adapter_config(context=IGalleryContainerTarget, provides=IGalleryContainer)
       
    58 def gallery_container_factory(target):
       
    59     """Galleries container factory"""
       
    60     annotations = IAnnotations(target)
       
    61     container = annotations.get(GALLERY_CONTAINER_KEY)
       
    62     if container is None:
       
    63         container = annotations[GALLERY_CONTAINER_KEY] = GalleryContainer()
       
    64         get_current_registry().notify(ObjectCreatedEvent(container))
       
    65         locate(container, target, '++gallery++')
       
    66     return container
       
    67 
       
    68 
       
    69 @adapter_config(name='gallery', context=IGalleryContainerTarget, provides=ITraversable)
       
    70 class GalleryContainerNamespace(ContextAdapter):
       
    71     """++gallery++ namespace traverser"""
       
    72 
       
    73     def traverse(self, name, furtherpath=None):
       
    74         return IGalleryContainer(self.context)
       
    75 
       
    76 
       
    77 @adapter_config(name='gallery', context=IGalleryContainerTarget, provides=ISublocations)
       
    78 class GalleryContainerSublocations(ContextAdapter):
       
    79     """Galleries container sublocations"""
       
    80 
       
    81     def sublocations(self):
       
    82         return IGalleryContainer(self.context).values()
       
    83 
       
    84 
       
    85 @provider(IVocabularyFactory)
       
    86 class GalleryContainerGalleriesVocabulary(SimpleVocabulary):
       
    87     """Galleries container galleries vocabulary"""
       
    88 
       
    89     def __init__(self, context):
       
    90         target = get_parent(context, IGalleryContainerTarget)
       
    91         terms = [SimpleTerm(gallery.__name__, title=II18n(gallery).query_attribute('title'))
       
    92                  for gallery in IGalleryContainer(target).values()]
       
    93         super(GalleryContainerGalleriesVocabulary, self).__init__(terms)
       
    94 
       
    95 getVocabularyRegistry().register('PyAMS content galleries', GalleryContainerGalleriesVocabulary)
       
    96 
       
    97 
       
    98 #
       
    99 # Galleries links container
       
   100 #
       
   101 
       
   102 @implementer(IGalleryLinksContainer)
       
   103 class GalleryLinksContainer(Persistent, Contained):
       
   104     """Galleries links container"""
       
   105 
       
   106     def __init__(self):
       
   107         self.galleries = PersistentList()
       
   108 
       
   109 
       
   110 @adapter_config(context=IGalleryLinksContainerTarget, provides=IGalleryLinksContainer)
       
   111 def gallery_links_container_factory(target):
       
   112     """Galleries links container factory"""
       
   113     annotations = IAnnotations(target)
       
   114     container = annotations.get(GALLERY_LINKS_CONTAINER_KEY)
       
   115     if container is None:
       
   116         container = annotations[GALLERY_LINKS_CONTAINER_KEY] = GalleryLinksContainer()
       
   117         get_current_registry().notify(ObjectCreatedEvent(container))
       
   118         locate(container, target, '++gallery-links++')
       
   119     return container
       
   120 
       
   121 
       
   122 @adapter_config(name='gallery-links', context=IGalleryLinksContainerTarget, provides=ITraversable)
       
   123 class GalleryLinksContainerNamespace(ContextAdapter):
       
   124     """++gallery-links++ namespace adapter"""
       
   125 
       
   126     def traverse(self, name, furtherpath=None):
       
   127         return IGalleryLinksContainer(self.context)