src/pyams_content/shared/common/types.py
changeset 276 78422a1c4228
child 501 3407e6940f6a
equal deleted inserted replaced
275:0c63253d75bd 276:78422a1c4228
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
       
    20 from pyams_content.component.links.interfaces import ILinkContainerTarget
       
    21 from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget
       
    22 from pyams_content.interfaces import MANAGE_TOOL_PERMISSION
       
    23 from pyams_content.shared.common.interfaces.types import IDataType, ISubType, IBaseDataType, ITypedSharedTool, \
       
    24     ITypedDataManager, DATA_MANAGER_ANNOTATION_KEY
       
    25 from pyams_form.interfaces.form import IFormContextPermissionChecker
       
    26 from zope.location.interfaces import ISublocations
       
    27 from zope.traversing.interfaces import ITraversable
       
    28 
       
    29 # import packages
       
    30 from persistent import Persistent
       
    31 from pyams_content.shared.common.manager import SharedTool
       
    32 from pyams_i18n.property import I18nFileProperty
       
    33 from pyams_utils.adapter import adapter_config, ContextAdapter
       
    34 from pyams_utils.registry import get_global_registry
       
    35 from pyams_utils.request import check_request
       
    36 from pyams_utils.traversing import get_parent
       
    37 from pyams_utils.vocabulary import vocabulary_config
       
    38 from zope.annotation.interfaces import IAnnotations
       
    39 from zope.container.contained import Contained
       
    40 from zope.container.ordered import OrderedContainer
       
    41 from zope.interface import implementer
       
    42 from zope.lifecycleevent import ObjectCreatedEvent
       
    43 from zope.location import locate
       
    44 from zope.schema import getFieldsInOrder
       
    45 from zope.schema.fieldproperty import FieldProperty
       
    46 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
       
    47 
       
    48 
       
    49 class BaseDataType(Persistent, Contained):
       
    50     """Base data type"""
       
    51 
       
    52     label = FieldProperty(IBaseDataType['label'])
       
    53     navigation_label = FieldProperty(IBaseDataType['navigation_label'])
       
    54     tabfolder_label = FieldProperty(IBaseDataType['tabfolder_label'])
       
    55     seealso_label = FieldProperty(IBaseDataType['seealso_label'])
       
    56     single_label = FieldProperty(IBaseDataType['single_label'])
       
    57     seeall_label = FieldProperty(IBaseDataType['seeall_label'])
       
    58     next_label = FieldProperty(IBaseDataType['next_label'])
       
    59     pictogram = I18nFileProperty(IBaseDataType['pictogram'])
       
    60 
       
    61 
       
    62 @implementer(ISubType, IParagraphContainerTarget, IExtFileContainerTarget, ILinkContainerTarget)
       
    63 class SubType(BaseDataType):
       
    64     """Data sub-type persistent class"""
       
    65 
       
    66 
       
    67 @implementer(IDataType, IParagraphContainerTarget, IExtFileContainerTarget, ILinkContainerTarget)
       
    68 class DataType(BaseDataType, OrderedContainer):
       
    69     """Data type persistent class"""
       
    70 
       
    71     field_names = FieldProperty(IDataType['field_names'])
       
    72 
       
    73 
       
    74 @implementer(ITypedDataManager)
       
    75 class TypedDataManager(OrderedContainer):
       
    76     """Data types container persistent class"""
       
    77 
       
    78 
       
    79 @adapter_config(context=IBaseDataType, provides=IFormContextPermissionChecker)
       
    80 class BaseDatatypePermissionChecker(ContextAdapter):
       
    81     """Base data type permission checker"""
       
    82 
       
    83     edit_permission = MANAGE_TOOL_PERMISSION
       
    84 
       
    85 
       
    86 @implementer(ITypedSharedTool)
       
    87 class TypedSharedTool(SharedTool):
       
    88     """Typed shared tool"""
       
    89 
       
    90     shared_content_types_fields = None
       
    91 
       
    92 
       
    93 @adapter_config(context=ITypedSharedTool, provides=ITypedDataManager)
       
    94 def TypedSharedToolDataManagerFactory(context):
       
    95     """Types shared tool data manager factory"""
       
    96     annotations = IAnnotations(context)
       
    97     manager = annotations.get(DATA_MANAGER_ANNOTATION_KEY)
       
    98     if manager is None:
       
    99         manager = annotations[DATA_MANAGER_ANNOTATION_KEY] = TypedDataManager()
       
   100         registry = get_global_registry()
       
   101         registry.notify(ObjectCreatedEvent(manager))
       
   102         locate(manager, context, '++types++')
       
   103     return manager
       
   104 
       
   105 
       
   106 @adapter_config(name='types', context=ITypedSharedTool, provides=ITraversable)
       
   107 class TypedSharedToolTypesNamespace(ContextAdapter):
       
   108     """Typed shared tool ++types++ namespace"""
       
   109 
       
   110     def traverse(self, name, furtherpath=None):
       
   111         return ITypedDataManager(self.context)
       
   112 
       
   113 
       
   114 @adapter_config(name='types', context=ITypedSharedTool, provides=ISublocations)
       
   115 class TypedSharedToolSublocations(ContextAdapter):
       
   116     """Typed shared tool sublocations adapter"""
       
   117 
       
   118     def sublocations(self):
       
   119         return ITypedDataManager(self.context).values()
       
   120 
       
   121 
       
   122 @vocabulary_config(name='PyAMS types interface fields')
       
   123 class TypedSharedToolDataTypesFields(SimpleVocabulary):
       
   124     """Typed shared tool data types fields vocabulary"""
       
   125 
       
   126     def __init__(self, context):
       
   127         terms = []
       
   128         parent = get_parent(context, ITypedSharedTool)
       
   129         if parent is not None:
       
   130             request = check_request()
       
   131             translate = request.localizer.translate
       
   132             terms = [SimpleTerm(name, title=translate(field.title))
       
   133                      for name, field in getFieldsInOrder(parent.shared_content_types_fields)]
       
   134         super(TypedSharedToolDataTypesFields, self).__init__(terms)