src/pyams_content/component/gallery/container.py
changeset 0 7c0001cacf8e
child 60 da1454d7d358
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/gallery/container.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,127 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.gallery.interfaces import IGalleryContainer, IGalleryContainerTarget, \
+    GALLERY_CONTAINER_KEY, IGalleryLinksContainer, IGalleryLinksContainerTarget, GALLERY_LINKS_CONTAINER_KEY
+from zope.annotation.interfaces import IAnnotations
+from zope.location.interfaces import ISublocations
+from zope.schema.interfaces import IVocabularyFactory
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from persistent import Persistent
+from persistent.list import PersistentList
+from pyams_i18n.interfaces import II18n
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.traversing import get_parent
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.container.folder import Folder
+from zope.interface import implementer, provider
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.location import locate
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
+
+
+#
+# Galleries container
+#
+
+@implementer(IGalleryContainer)
+class GalleryContainer(Folder):
+    """Galleries container"""
+
+    last_id = 1
+
+    def __setitem__(self, key, value):
+        key = str(self.last_id)
+        super(GalleryContainer, self).__setitem__(key, value)
+        self.last_id += 1
+
+
+@adapter_config(context=IGalleryContainerTarget, provides=IGalleryContainer)
+def gallery_container_factory(target):
+    """Galleries container factory"""
+    annotations = IAnnotations(target)
+    container = annotations.get(GALLERY_CONTAINER_KEY)
+    if container is None:
+        container = annotations[GALLERY_CONTAINER_KEY] = GalleryContainer()
+        get_current_registry().notify(ObjectCreatedEvent(container))
+        locate(container, target, '++gallery++')
+    return container
+
+
+@adapter_config(name='gallery', context=IGalleryContainerTarget, provides=ITraversable)
+class GalleryContainerNamespace(ContextAdapter):
+    """++gallery++ namespace traverser"""
+
+    def traverse(self, name, furtherpath=None):
+        return IGalleryContainer(self.context)
+
+
+@adapter_config(name='gallery', context=IGalleryContainerTarget, provides=ISublocations)
+class GalleryContainerSublocations(ContextAdapter):
+    """Galleries container sublocations"""
+
+    def sublocations(self):
+        return IGalleryContainer(self.context).values()
+
+
+@provider(IVocabularyFactory)
+class GalleryContainerGalleriesVocabulary(SimpleVocabulary):
+    """Galleries container galleries vocabulary"""
+
+    def __init__(self, context):
+        target = get_parent(context, IGalleryContainerTarget)
+        terms = [SimpleTerm(gallery.__name__, title=II18n(gallery).query_attribute('title'))
+                 for gallery in IGalleryContainer(target).values()]
+        super(GalleryContainerGalleriesVocabulary, self).__init__(terms)
+
+getVocabularyRegistry().register('PyAMS content galleries', GalleryContainerGalleriesVocabulary)
+
+
+#
+# Galleries links container
+#
+
+@implementer(IGalleryLinksContainer)
+class GalleryLinksContainer(Persistent, Contained):
+    """Galleries links container"""
+
+    def __init__(self):
+        self.galleries = PersistentList()
+
+
+@adapter_config(context=IGalleryLinksContainerTarget, provides=IGalleryLinksContainer)
+def gallery_links_container_factory(target):
+    """Galleries links container factory"""
+    annotations = IAnnotations(target)
+    container = annotations.get(GALLERY_LINKS_CONTAINER_KEY)
+    if container is None:
+        container = annotations[GALLERY_LINKS_CONTAINER_KEY] = GalleryLinksContainer()
+        get_current_registry().notify(ObjectCreatedEvent(container))
+        locate(container, target, '++gallery-links++')
+    return container
+
+
+@adapter_config(name='gallery-links', context=IGalleryLinksContainerTarget, provides=ITraversable)
+class GalleryLinksContainerNamespace(ContextAdapter):
+    """++gallery-links++ namespace adapter"""
+
+    def traverse(self, name, furtherpath=None):
+        return IGalleryLinksContainer(self.context)