src/pyams_content/component/paragraph/verbatim.py
changeset 450 9a6afbc6ab4f
parent 447 3665317b6009
child 555 8e8a14452567
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/verbatim.py	Wed Mar 07 14:46:15 2018 +0100
@@ -0,0 +1,111 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.illustration.interfaces import IIllustrationTarget
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.verbatim import IVerbatimParagraph, VERBATIM_PARAGRAPH_TYPE, \
+    VERBATIM_PARAGRAPH_RENDERERS
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
+
+# import packages
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from pyams_content import _
+
+
+#
+# Frame paragraph
+#
+
+@implementer(IVerbatimParagraph, IIllustrationTarget)
+class VerbatimParagraph(RenderedContentMixin, BaseParagraph):
+    """Verbatim paragraph"""
+
+    icon_class = 'fa-quote-right'
+    icon_hint = _("Verbatim")
+
+    quote = FieldProperty(IVerbatimParagraph['quote'])
+    author = FieldProperty(IVerbatimParagraph['author'])
+    charge = FieldProperty(IVerbatimParagraph['charge'])
+    renderer = FieldProperty(IVerbatimParagraph['renderer'])
+
+
+@utility_config(name=VERBATIM_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class VerbatimParagraphFactory(BaseParagraphFactory):
+    """Verbatim paragraph factory"""
+
+    name = _("Verbatim paragraph")
+    content_type = VerbatimParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IVerbatimParagraph, provides=IContentChecker)
+class VerbatimParagraphContentChecker(BaseParagraphContentChecker):
+    """Verbatim paragraph content checker"""
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        manager = get_parent(self.context, II18nManager)
+        if manager is not None:
+            langs = manager.get_languages()
+        else:
+            negotiator = get_utility(INegotiator)
+            langs = (negotiator.server_language, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            for attr in ('title', 'quote', 'charge'):
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    field_title = translate(IVerbatimParagraph[attr].title)
+                    if len(langs) == 1:
+                        output.append(translate(MISSING_VALUE).format(field=field_title))
+                    else:
+                        output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        for attr in ('author', ):
+            value = getattr(self.context, attr, None)
+            if not value:
+                field_title = translate(IVerbatimParagraph[attr].title)
+                output.append(translate(MISSING_VALUE).format(field=field_title))
+        return output
+
+
+@vocabulary_config(name=VERBATIM_PARAGRAPH_RENDERERS)
+class VerbatimParagraphRendererVocabulary(SimpleVocabulary):
+    """Verbatim paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IVerbatimParagraph.providedBy(context):
+            context = VerbatimParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(VerbatimParagraphRendererVocabulary, self).__init__(terms)