src/pyams_content/shared/common/types.py
changeset 276 78422a1c4228
child 501 3407e6940f6a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/common/types.py	Fri Nov 10 12:07:43 2017 +0100
@@ -0,0 +1,134 @@
+#
+# 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 IExtFileContainerTarget
+from pyams_content.component.links.interfaces import ILinkContainerTarget
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget
+from pyams_content.interfaces import MANAGE_TOOL_PERMISSION
+from pyams_content.shared.common.interfaces.types import IDataType, ISubType, IBaseDataType, ITypedSharedTool, \
+    ITypedDataManager, DATA_MANAGER_ANNOTATION_KEY
+from pyams_form.interfaces.form import IFormContextPermissionChecker
+from zope.location.interfaces import ISublocations
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from persistent import Persistent
+from pyams_content.shared.common.manager import SharedTool
+from pyams_i18n.property import I18nFileProperty
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.registry import get_global_registry
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from zope.annotation.interfaces import IAnnotations
+from zope.container.contained import Contained
+from zope.container.ordered import OrderedContainer
+from zope.interface import implementer
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.location import locate
+from zope.schema import getFieldsInOrder
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+
+class BaseDataType(Persistent, Contained):
+    """Base data type"""
+
+    label = FieldProperty(IBaseDataType['label'])
+    navigation_label = FieldProperty(IBaseDataType['navigation_label'])
+    tabfolder_label = FieldProperty(IBaseDataType['tabfolder_label'])
+    seealso_label = FieldProperty(IBaseDataType['seealso_label'])
+    single_label = FieldProperty(IBaseDataType['single_label'])
+    seeall_label = FieldProperty(IBaseDataType['seeall_label'])
+    next_label = FieldProperty(IBaseDataType['next_label'])
+    pictogram = I18nFileProperty(IBaseDataType['pictogram'])
+
+
+@implementer(ISubType, IParagraphContainerTarget, IExtFileContainerTarget, ILinkContainerTarget)
+class SubType(BaseDataType):
+    """Data sub-type persistent class"""
+
+
+@implementer(IDataType, IParagraphContainerTarget, IExtFileContainerTarget, ILinkContainerTarget)
+class DataType(BaseDataType, OrderedContainer):
+    """Data type persistent class"""
+
+    field_names = FieldProperty(IDataType['field_names'])
+
+
+@implementer(ITypedDataManager)
+class TypedDataManager(OrderedContainer):
+    """Data types container persistent class"""
+
+
+@adapter_config(context=IBaseDataType, provides=IFormContextPermissionChecker)
+class BaseDatatypePermissionChecker(ContextAdapter):
+    """Base data type permission checker"""
+
+    edit_permission = MANAGE_TOOL_PERMISSION
+
+
+@implementer(ITypedSharedTool)
+class TypedSharedTool(SharedTool):
+    """Typed shared tool"""
+
+    shared_content_types_fields = None
+
+
+@adapter_config(context=ITypedSharedTool, provides=ITypedDataManager)
+def TypedSharedToolDataManagerFactory(context):
+    """Types shared tool data manager factory"""
+    annotations = IAnnotations(context)
+    manager = annotations.get(DATA_MANAGER_ANNOTATION_KEY)
+    if manager is None:
+        manager = annotations[DATA_MANAGER_ANNOTATION_KEY] = TypedDataManager()
+        registry = get_global_registry()
+        registry.notify(ObjectCreatedEvent(manager))
+        locate(manager, context, '++types++')
+    return manager
+
+
+@adapter_config(name='types', context=ITypedSharedTool, provides=ITraversable)
+class TypedSharedToolTypesNamespace(ContextAdapter):
+    """Typed shared tool ++types++ namespace"""
+
+    def traverse(self, name, furtherpath=None):
+        return ITypedDataManager(self.context)
+
+
+@adapter_config(name='types', context=ITypedSharedTool, provides=ISublocations)
+class TypedSharedToolSublocations(ContextAdapter):
+    """Typed shared tool sublocations adapter"""
+
+    def sublocations(self):
+        return ITypedDataManager(self.context).values()
+
+
+@vocabulary_config(name='PyAMS types interface fields')
+class TypedSharedToolDataTypesFields(SimpleVocabulary):
+    """Typed shared tool data types fields vocabulary"""
+
+    def __init__(self, context):
+        terms = []
+        parent = get_parent(context, ITypedSharedTool)
+        if parent is not None:
+            request = check_request()
+            translate = request.localizer.translate
+            terms = [SimpleTerm(name, title=translate(field.title))
+                     for name, field in getFieldsInOrder(parent.shared_content_types_fields)]
+        super(TypedSharedToolDataTypesFields, self).__init__(terms)