src/pyams_content/component/paragraph/__init__.py
changeset 140 67bad9f880ee
parent 7 cbc55162b64e
child 157 35d11e8461e2
--- a/src/pyams_content/component/paragraph/__init__.py	Mon Sep 11 14:53:15 2017 +0200
+++ b/src/pyams_content/component/paragraph/__init__.py	Mon Sep 11 14:54:30 2017 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_utils.request import check_request
 
 __docformat__ = 'restructuredtext'
 
@@ -16,31 +17,78 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.paragraph.interfaces import IBaseParagraph, IHTMLParagraph
+from pyams_content.component.paragraph.interfaces import IBaseParagraph, IParagraphFactory, IParagraphContainerTarget, \
+    IParagraphContainer, IParagraphFactorySettings
 from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_workflow.interfaces import IWorkflowState
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 
 # import packages
 from persistent import Persistent
 from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.registry import query_utility
 from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
+from zope.component.globalregistry import getGlobalSiteManager
 from zope.container.contained import Contained
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectModifiedEvent
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
+#
+# Auto-creation of default paragraphs
+#
+
+@subscriber(IObjectAddedEvent, context_selector=IParagraphContainerTarget)
+def handle_new_paragraphs_container(event):
+    """Handle new paragraphs container"""
+    container = IParagraphContainer(event.object)
+    content = get_parent(container, IWfSharedContent)
+    version_state = IWorkflowState(content, None) if content is not None else None
+    if (version_state is None) or (version_state.version_id == 1):
+        # only apply to first version
+        settings = get_parent(container, IParagraphFactorySettings)
+        if settings is not None:
+            for factory_name in settings.auto_created_paragraphs or ():
+                factory = query_utility(IParagraphFactory, name=factory_name)
+                if factory is not None:
+                    container.append(factory.content_type())
+
+
+#
+# Base paragraph classes and subscribers
+#
+
 @implementer(IBaseParagraph)
 class BaseParagraph(Persistent, Contained):
     """Base paragraph persistent class"""
 
+    icon_class = ''
+    icon_hint = ''
+
     visible = FieldProperty(IBaseParagraph['visible'])
     title = FieldProperty(IBaseParagraph['title'])
 
 
+@vocabulary_config(name='PyAMS paragraph factories')
+class ParagraphFactoriesVocabulary(SimpleVocabulary):
+    """Paragraph factories vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        registry = request.registry
+        translate = request.localizer.translate
+        terms = sorted([SimpleTerm(name, title=translate(util.name))
+                        for name, util in registry.getUtilitiesFor(IParagraphFactory)],
+                       key=lambda x: x.title)
+        super(ParagraphFactoriesVocabulary, self).__init__(terms)
+
+
 @adapter_config(context=IBaseParagraph, provides=IFormContextPermissionChecker)
 class BaseParagraphPermissionChecker(ContextAdapter):
     """Paragraph permission checker"""