--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/extfile/__init__.py Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,138 @@
+#
+# 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 IBaseExtFile, IExtFile, IExtImage, IExtVideo, IExtAudio
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_form.interfaces.form import IFormContextPermissionChecker
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
+from zope.schema.interfaces import IVocabularyFactory
+
+# import packages
+from persistent import Persistent
+from pyams_i18n.property import I18nFileProperty
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.traversing import get_parent
+from pyramid.events import subscriber
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer, provider
+from zope.lifecycleevent import ObjectModifiedEvent
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
+
+from pyams_content import _
+
+
+EXTERNAL_FILES_FACTORIES = {}
+
+
+def register_file_factory(key, factory, name=None):
+ """Register new file factory"""
+ if key not in EXTERNAL_FILES_FACTORIES:
+ EXTERNAL_FILES_FACTORIES[key] = (factory, name or key)
+
+
+@provider(IVocabularyFactory)
+class ExternalFilesFactoriesVocabulary(SimpleVocabulary):
+ """External files factories vocabulary"""
+
+ def __init__(self, context):
+ terms = sorted([SimpleTerm(k, title=v[1]) for k, v in EXTERNAL_FILES_FACTORIES.items()],
+ key=lambda x: x.title)
+ super(ExternalFilesFactoriesVocabulary, self).__init__(terms)
+
+getVocabularyRegistry().register('PyAMS files factories', ExternalFilesFactoriesVocabulary)
+
+
+@implementer(IBaseExtFile)
+class BaseExtFile(Persistent, Contained):
+ """External file persistent class"""
+
+ title = FieldProperty(IExtFile['title'])
+ description = FieldProperty(IExtFile['description'])
+ author = FieldProperty(IExtFile['author'])
+
+
+@adapter_config(context=IBaseExtFile, provides=IFormContextPermissionChecker)
+class BaseExtFilePermissionChecker(ContextAdapter):
+ """External file permission checker"""
+
+ @property
+ def edit_permission(self):
+ content = get_parent(self.context, IWfSharedContent)
+ return IFormContextPermissionChecker(content).edit_permission
+
+
+@subscriber(IObjectAddedEvent, context_selector=IBaseExtFile)
+def handle_added_extfile(event):
+ """Handle added external file"""
+ content = get_parent(event.object, IWfSharedContent)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectModifiedEvent, context_selector=IBaseExtFile)
+def handle_modified_extfile(event):
+ """Handle modified external file"""
+ content = get_parent(event.object, IWfSharedContent)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectRemovedEvent, context_selector=IBaseExtFile)
+def handle_removed_extfile(event):
+ """Handle removed external file"""
+ content = get_parent(event.object, IWfSharedContent)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@implementer(IExtFile)
+class ExtFile(BaseExtFile):
+ """Generic external file persistent class"""
+
+ data = I18nFileProperty(IExtFile['data'])
+
+register_file_factory('file', ExtFile, _("Standard file"))
+
+
+@implementer(IExtImage)
+class ExtImage(BaseExtFile):
+ """External image persistent class"""
+
+ data = I18nFileProperty(IExtImage['data'])
+
+register_file_factory('image', ExtImage, _("Image"))
+
+
+@implementer(IExtVideo)
+class ExtVideo(BaseExtFile):
+ """External video file persistent class"""
+
+ data = I18nFileProperty(IExtVideo['data'])
+
+register_file_factory('video', ExtVideo, _("Video"))
+
+
+@implementer(IExtAudio)
+class ExtAudio(BaseExtFile):
+ """External audio file persistent class"""
+
+ data = I18nFileProperty(IExtAudio['data'])
+
+register_file_factory('audio', ExtAudio, _("Audio file"))