src/pyams_content/component/gallery/__init__.py
changeset 140 67bad9f880ee
parent 60 da1454d7d358
child 252 2dafc720b378
--- a/src/pyams_content/component/gallery/__init__.py	Mon Sep 11 14:53:15 2017 +0200
+++ b/src/pyams_content/component/gallery/__init__.py	Mon Sep 11 14:54:30 2017 +0200
@@ -16,93 +16,33 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.gallery.interfaces import IGalleryFileInfo, GALLERY_FILE_INFO_KEY, IGallery, IGalleryFile
+from pyams_content.component.gallery.interfaces import IGallery, IGalleryTarget, \
+    GALLERY_CONTAINER_KEY, IGalleryRenderer
 from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_form.interfaces.form import IFormContextPermissionChecker
-from pyams_i18n.interfaces import II18n
 from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
+from zope.location.interfaces import ISublocations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
-from persistent import Persistent
-from pyams_file.property import FileProperty
+from pyams_catalog.utils import index_object
 from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyams_utils.container import BTreeOrderedContainer
+from pyams_utils.request import check_request
 from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
+from zope.interface import implementer
 from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent
-from zope.container.contained import Contained
-from zope.interface import implementer
 from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
 #
-# Gallery file
-#
-
-@implementer(IGalleryFileInfo)
-class GalleryFileInfo(Persistent, Contained):
-    """Gallery file info"""
-
-    title = FieldProperty(IGalleryFileInfo['title'])
-    description = FieldProperty(IGalleryFileInfo['description'])
-    author = FieldProperty(IGalleryFileInfo['author'])
-    author_comments = FieldProperty(IGalleryFileInfo['author_comments'])
-    sound = FileProperty(IGalleryFileInfo['sound'])
-    sound_title = FieldProperty(IGalleryFileInfo['sound_title'])
-    sound_description = FieldProperty(IGalleryFileInfo['sound_description'])
-    pif_number = FieldProperty(IGalleryFileInfo['pif_number'])
-    visible = FieldProperty(IGalleryFileInfo['visible'])
-
-    def get_title(self, request=None):
-        return II18n(self).query_attribute('title', request=request)
-
-
-@adapter_config(context=IGalleryFile, provides=IGalleryFileInfo)
-def media_gallery_info_factory(file):
-    """Gallery file gallery info factory"""
-    annotations = IAnnotations(file)
-    info = annotations.get(GALLERY_FILE_INFO_KEY)
-    if info is None:
-        info = annotations[GALLERY_FILE_INFO_KEY] = GalleryFileInfo()
-        get_current_registry().notify(ObjectCreatedEvent(info))
-        locate(info, file, '++gallery-info++')
-    return info
-
-
-@adapter_config(name='gallery-info', context=IGalleryFile, provides=ITraversable)
-class MediaGalleryInfoTraverser(ContextAdapter):
-    """Gallery file gallery info adapter"""
-
-    def traverse(self, name, furtherpath=None):
-        return IGalleryFileInfo(self.context)
-
-
-@adapter_config(context=IGalleryFile, provides=IFormContextPermissionChecker)
-class GalleryFilePermissionChecker(ContextAdapter):
-    """Gallery file permission checker"""
-
-    @property
-    def edit_permission(self):
-        content = get_parent(self.context, IWfSharedContent)
-        return IFormContextPermissionChecker(content).edit_permission
-
-
-@adapter_config(context=IGalleryFileInfo, provides=IFormContextPermissionChecker)
-class GalleryFileInfoPermissionChecker(ContextAdapter):
-    """Gallery file info permission checker"""
-
-    @property
-    def edit_permission(self):
-        content = get_parent(self.context, IWfSharedContent)
-        return IFormContextPermissionChecker(content).edit_permission
-
-
-#
-# Gallery
+# Galleries container
 #
 
 @implementer(IGallery)
@@ -111,19 +51,53 @@
 
     title = FieldProperty(IGallery['title'])
     description = FieldProperty(IGallery['description'])
-    visible = FieldProperty(IGallery['visible'])
+    renderer = FieldProperty(IGallery['renderer'])
 
     last_id = 1
 
-    def __setitem__(self, key, value):
+    def append(self, value, notify=True):
         key = str(self.last_id)
-        super(Gallery, self).__setitem__(key, value)
+        if not notify:
+            # pre-locate gallery item to avoid multiple notifications
+            locate(value, self.key)
+        self[key] = value
         self.last_id += 1
+        if not notify:
+            # make sure that gallery item is correctly indexed
+            index_object(value)
 
     def get_visible_images(self):
         return [image for image in self.values() if image.visible]
 
 
+@adapter_config(context=IGalleryTarget, provides=IGallery)
+def gallery_factory(target):
+    """Galleries container factory"""
+    annotations = IAnnotations(target)
+    gallery = annotations.get(GALLERY_CONTAINER_KEY)
+    if gallery is None:
+        gallery = annotations[GALLERY_CONTAINER_KEY] = Gallery()
+        get_current_registry().notify(ObjectCreatedEvent(gallery))
+        locate(gallery, target, '++gallery++')
+    return gallery
+
+
+@adapter_config(name='gallery', context=IGalleryTarget, provides=ITraversable)
+class GalleryContainerNamespace(ContextAdapter):
+    """++gallery++ namespace traverser"""
+
+    def traverse(self, name, furtherpath=None):
+        return IGallery(self.context)
+
+
+@adapter_config(name='gallery', context=IGalleryTarget, provides=ISublocations)
+class GalleryContainerSublocations(ContextAdapter):
+    """Galleries container sublocations"""
+
+    def sublocations(self):
+        return IGallery(self.context).values()
+
+
 @adapter_config(context=IGallery, provides=IFormContextPermissionChecker)
 class GalleryPermissionChecker(ContextAdapter):
     """Gallery permission checker"""
@@ -156,3 +130,18 @@
     content = get_parent(event.object, IWfSharedContent)
     if content is not None:
         get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@vocabulary_config(name='PyAMS gallery renderers')
+class GalleryRendererVocabulary(SimpleVocabulary):
+    """Gallery renderer utilities vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        context = Gallery()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IGalleryRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(GalleryRendererVocabulary, self).__init__(terms)