Updated content paragraphs vocabulary
authorThierry Florac <thierry.florac@onf.fr>
Tue, 27 Nov 2018 15:37:24 +0100
changeset 1128 1be970640518
parent 1127 a76066afc55b
child 1129 2effaa115498
Updated content paragraphs vocabulary
src/pyams_content/component/paragraph/__init__.py
--- a/src/pyams_content/component/paragraph/__init__.py	Tue Nov 27 15:37:04 2018 +0100
+++ b/src/pyams_content/component/paragraph/__init__.py	Tue Nov 27 15:37:24 2018 +0100
@@ -12,35 +12,32 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_content.component.paragraph.interfaces import IBaseParagraph, IParagraphFactory, IParagraphContainerTarget, \
-    IParagraphContainer, IParagraphFactorySettings
-from pyams_content.features.preview.interfaces import IPreviewTarget
-from pyams_content.shared.common.interfaces import IWfSharedContent
-from pyams_form.interfaces.form import IFormContextPermissionChecker
-from pyams_i18n.interfaces import II18n
-from pyams_workflow.interfaces import IWorkflowState
-from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
-
-# import packages
 from persistent import Persistent
-from pyams_content.features.checker import BaseContentChecker
-from pyams_content.features.renderer import RenderedContentMixin
-from pyams_utils.adapter import adapter_config, ContextAdapter
-from pyams_utils.registry import query_utility
-from pyams_utils.request import check_request
-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.container.contained import Contained
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectModifiedEvent
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+from pyams_content.component.paragraph.interfaces import CONTENT_PARAGRAPHS_VOCABULARY, IBaseParagraph, \
+    IParagraphContainer, IParagraphContainerTarget, IParagraphFactory, IParagraphFactorySettings, IParagraphTitle, \
+    PARAGRAPH_FACTORIES_VOCABULARY
+from pyams_content.features.checker import BaseContentChecker
+from pyams_content.features.preview.interfaces import IPreviewTarget
+from pyams_content.features.renderer import RenderedContentMixin
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_i18n.interfaces import II18n
+from pyams_utils.adapter import ContextAdapter, adapter_config
+from pyams_utils.html import html_to_text
+from pyams_utils.registry import query_utility
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from pyams_workflow.interfaces import IWorkflowState
 
 
 #
@@ -108,7 +105,7 @@
             '({0})'.format(translate(self.context.icon_hint).lower())
 
 
-@vocabulary_config(name='PyAMS paragraph factories')
+@vocabulary_config(name=PARAGRAPH_FACTORIES_VOCABULARY)
 class ParagraphFactoriesVocabulary(SimpleVocabulary):
     """Paragraph factories vocabulary"""
 
@@ -122,19 +119,24 @@
         super(ParagraphFactoriesVocabulary, self).__init__(terms)
 
 
-@vocabulary_config(name='PyAMS content paragraphs')
+@vocabulary_config(name=CONTENT_PARAGRAPHS_VOCABULARY)
 class ContentParagraphsVocabulary(SimpleVocabulary):
     """Content paragraphs vocabulary"""
 
     def __init__(self, context):
+
+        def get_title(paragraph):
+            adapter = request.registry.queryMultiAdapter((paragraph, request), IParagraphTitle)
+            if adapter is not None:
+                return html_to_text(adapter)
+            return II18n(paragraph).query_attribute('title', request=request) or BaseParagraph.empty_title
+
         request = check_request()
         if not IParagraphContainerTarget.providedBy(context):
             context = get_parent(context, IParagraphContainerTarget)
         if context is not None:
             terms = [SimpleTerm(para.__name__,
-                                title='§{0}: {1}'.format(index+1,
-                                                         II18n(para).query_attribute('title', request=request) or
-                                                         BaseParagraph.empty_title))
+                                title='§{0}: {1}'.format(index+1, get_title(para)))
                      for index, para in enumerate(IParagraphContainer(context).values())]
         else:
             terms = []