src/pyams_content/component/extfile/container.py
changeset 0 7c0001cacf8e
child 60 da1454d7d358
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/extfile/container.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,148 @@
+#
+# 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.extfile.interfaces import IExtFileContainer, IExtFileContainerTarget, \
+    EXTFILE_CONTAINER_KEY, IExtFileLinksContainer, IExtFileLinksContainerTarget, EXTFILE_LINKS_CONTAINER_KEY
+from pyams_file.interfaces import IMediaFile, IImage, IVideo, IAudio
+from pyams_i18n.interfaces import II18n
+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_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, getVocabularyRegistry, SimpleTerm
+
+
+#
+# External files container
+#
+
+@implementer(IExtFileContainer)
+class ExtFileContainer(Folder):
+    """External files container"""
+
+    last_id = 1
+
+    def __setitem__(self, key, value):
+        key = str(self.last_id)
+        super(ExtFileContainer, self).__setitem__(key, value)
+        self.last_id += 1
+
+    @property
+    def files(self):
+        return (file for file in self.values() if not IMediaFile.providedBy(II18n(file).query_attribute('data')))
+
+    @property
+    def medias(self):
+        return (file for file in self.values() if IMediaFile.providedBy(II18n(file).query_attribute('data')))
+
+    @property
+    def images(self):
+        return (file for file in self.values() if IImage.providedBy(II18n(file).query_attribute('data')))
+
+    @property
+    def videos(self):
+        return (file for file in self.values() if IVideo.providedBy(II18n(file).query_attribute('data')))
+
+    @property
+    def audios(self):
+        return (file for file in self.values() if IAudio.providedBy(II18n(file).query_attribute('data')))
+
+
+@adapter_config(context=IExtFileContainerTarget, provides=IExtFileContainer)
+def extfile_container_factory(target):
+    """External files container factory"""
+    annotations = IAnnotations(target)
+    container = annotations.get(EXTFILE_CONTAINER_KEY)
+    if container is None:
+        container = annotations[EXTFILE_CONTAINER_KEY] = ExtFileContainer()
+        get_current_registry().notify(ObjectCreatedEvent(container))
+        locate(container, target, '++files++')
+    return container
+
+
+@adapter_config(name='files', context=IExtFileContainerTarget, provides=ITraversable)
+class ExtFileContainerNamespace(ContextAdapter):
+    """++files++ namespace adapter"""
+
+    def traverse(self, name, furtherpath=None):
+        return IExtFileContainer(self.context)
+
+
+@adapter_config(name='extfile', context=IExtFileContainerTarget, provides=ISublocations)
+class ExtFileContainerSublocations(ContextAdapter):
+    """External files container sublocations"""
+
+    def sublocations(self):
+        return IExtFileContainer(self.context).values()
+
+
+@provider(IVocabularyFactory)
+class ExtFileContainerFilesVocabulary(SimpleVocabulary):
+    """External files container files vocabulary"""
+
+    def __init__(self, context):
+        target = get_parent(context, IExtFileContainerTarget)
+        terms = [SimpleTerm(file.__name__, title=II18n(file).query_attribute('title'))
+                 for file in IExtFileContainer(target).values()]
+        super(ExtFileContainerFilesVocabulary, self).__init__(terms)
+
+getVocabularyRegistry().register('PyAMS content external files', ExtFileContainerFilesVocabulary)
+
+
+#
+# External file links container
+#
+
+@implementer(IExtFileLinksContainer)
+class ExtFileLinksContainer(Persistent, Contained):
+    """External files links container"""
+
+    def __init__(self):
+        self.files = PersistentList()
+
+
+@adapter_config(context=IExtFileLinksContainerTarget, provides=IExtFileLinksContainer)
+def extfile_links_container_factory(target):
+    """External files links container factory"""
+    annotations = IAnnotations(target)
+    container = annotations.get(EXTFILE_LINKS_CONTAINER_KEY)
+    if container is None:
+        container = annotations[EXTFILE_LINKS_CONTAINER_KEY] = ExtFileLinksContainer()
+        get_current_registry().notify(ObjectCreatedEvent(container))
+        locate(container, target, '++files-links++')
+    return container
+
+
+@adapter_config(name='files-links', context=IExtFileLinksContainerTarget, provides=ITraversable)
+class ExtFileLinksContainerNamespace(ContextAdapter):
+    """++files-links++ namespace adapter"""
+
+    def traverse(self, name, furtherpath=None):
+        return IExtFileLinksContainer(self.context)