Added vocabulary to get all data types
authorThierry Florac <thierry.florac@onf.fr>
Tue, 17 Jul 2018 17:59:54 +0200
changeset 854 d9c216572b6b
parent 853 384d5f914612
child 855 b83aca9d6e2b
Added vocabulary to get all data types
src/pyams_content/shared/common/types.py
--- a/src/pyams_content/shared/common/types.py	Tue Jul 17 17:59:30 2018 +0200
+++ b/src/pyams_content/shared/common/types.py	Tue Jul 17 17:59:54 2018 +0200
@@ -9,6 +9,9 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_content.shared.common.interfaces import ISharedContentFactory
+from pyams_utils.interfaces import ICacheKeyValue
+from pyams_utils.registry import get_local_registry
 
 __docformat__ = 'restructuredtext'
 
@@ -23,7 +26,7 @@
 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, DATA_TYPES_VOCABULARY, DATA_TYPE_FIELDS_VOCABULARY, \
-    IWfTypedSharedContent, DATA_SUBTYPES_VOCABULARY
+    IWfTypedSharedContent, DATA_SUBTYPES_VOCABULARY, ALL_DATA_TYPES_VOCABULARY
 from pyams_form.interfaces.form import IFormContextPermissionChecker
 from pyams_i18n.interfaces import II18n
 from zope.lifecycleevent.interfaces import IObjectAddedEvent
@@ -32,7 +35,7 @@
 
 # import packages
 from persistent import Persistent
-from pyams_content.shared.common import WfSharedContent
+from pyams_content.shared.common import WfSharedContent, IWfSharedContentFactory
 from pyams_content.shared.common.manager import SharedTool
 from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.request import check_request
@@ -147,6 +150,43 @@
 # Data types vocabularies
 #
 
+@vocabulary_config(name=ALL_DATA_TYPES_VOCABULARY)
+class AllTypedSharedToolDataTypesVocabulary(SimpleVocabulary):
+    """Vocabulary consolidating all data types"""
+
+    def __init__(self, context):
+        terms = []
+        request = check_request()
+        registry = get_local_registry()
+        for tool in registry.getAllUtilitiesRegisteredFor(ITypedSharedTool):
+            manager = ITypedDataManager(tool)
+            terms.extend([SimpleTerm(datatype.__name__,
+                                     title=II18n(datatype).query_attribute('label', request=request))
+                          for datatype in manager.values()])
+        terms.sort(key=lambda x: x.title)
+        super(AllTypedSharedToolDataTypesVocabulary, self).__init__(terms)
+
+
+def get_all_data_types(request):
+    """Get list of all registered data types"""
+    results = []
+    registry = get_local_registry()
+    for tool in sorted(registry.getAllUtilitiesRegisteredFor(ITypedSharedTool),
+                       key=lambda x: II18n(x).query_attribute('title', request=request)):
+        content_factory = IWfSharedContentFactory(ISharedContentFactory(tool))
+        manager = ITypedDataManager(tool)
+        terms = [{
+            'id': datatype.__name__,
+            'text': II18n(datatype).query_attribute('label', request=request)
+        } for datatype in manager.values()]
+        results.append({
+            'text': request.localizer.translate(content_factory.content_name),
+            'disabled': True,
+            'children': terms
+        })
+    return results
+
+
 @vocabulary_config(name=DATA_TYPES_VOCABULARY)
 class TypedSharedToolDataTypesVocabulary(SimpleVocabulary):
     """Typed shared tool data types vocabulary"""