Handle content types registration
authorThierry Florac <thierry.florac@onf.fr>
Tue, 18 Apr 2017 15:59:22 +0200
changeset 72 3d0cfef1753e
parent 71 a0db8248420b
child 73 22bd0cc53c47
Handle content types registration
src/pyams_content/shared/common/__init__.py
--- a/src/pyams_content/shared/common/__init__.py	Tue Apr 18 12:00:33 2017 +0200
+++ b/src/pyams_content/shared/common/__init__.py	Tue Apr 18 15:59:22 2017 +0200
@@ -24,6 +24,7 @@
 from pyams_sequence.interfaces import ISequentialIdTarget, ISequentialIdInfo
 from pyams_utils.interfaces import VIEW_PERMISSION
 from pyams_workflow.interfaces import IWorkflowPublicationSupport, IWorkflow, IObjectClonedEvent, IWorkflowVersions
+from pyramid.interfaces import IWSGIApplicationCreatedEvent
 from zope.dublincore.interfaces import IZopeDublinCore
 from zope.intid.interfaces import IIntIds
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
@@ -35,12 +36,16 @@
 from pyams_security.security import ProtectedObject
 from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyams_utils.registry import query_utility
-from pyams_utils.request import query_request
+from pyams_utils.request import query_request, check_request
 from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
+from pyramid.settings import asbool
+from pyramid.threadlocal import get_current_registry
 from zope.container.contained import Contained
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
 #
@@ -55,6 +60,27 @@
     CONTENT_TYPES[content.content_type] = content
 
 
+@subscriber(IWSGIApplicationCreatedEvent)
+def handle_content_types(event):
+    """Check for content types to un-register"""
+    registry = get_current_registry()
+    for key, content in CONTENT_TYPES.copy().items():
+        if not asbool(registry.settings.get('pyams_content.register.{0}'.format(key), True)):
+            del CONTENT_TYPES[key]
+
+
+@vocabulary_config(name='PyAMS content types')
+class ContentTypesVocabulary(SimpleVocabulary):
+    """Content types vocabulary"""
+
+    def __init__(self, context):
+        request = check_request()
+        translate = request.localizer.translate
+        terms = [SimpleTerm(content.content_type, title=translate(content.content_name))
+                 for content in CONTENT_TYPES.values()]
+        super(ContentTypesVocabulary, self).__init__(terms)
+
+
 #
 # Workflow shared content class and adapters
 #