src/pyams_content/component/paragraph/frame.py
changeset 447 3665317b6009
parent 443 326adf3362d8
child 555 8e8a14452567
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/frame.py	Wed Mar 07 09:49:47 2018 +0100
@@ -0,0 +1,106 @@
+#
+# 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.extfile.interfaces import IExtFileContainerTarget
+from pyams_content.component.illustration.interfaces import IIllustrationTarget
+from pyams_content.component.links.interfaces import ILinkContainerTarget
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.frame import IFrameParagraph, FRAME_PARAGRAPH_TYPE, \
+    FRAME_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(IFrameParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
+class FrameParagraph(RenderedContentMixin, BaseParagraph):
+    """Framed text paragraph"""
+
+    icon_class = 'fa-list-alt'
+    icon_hint = _("Framed text")
+
+    body = FieldProperty(IFrameParagraph['body'])
+    renderer = FieldProperty(IFrameParagraph['renderer'])
+
+
+@utility_config(name=FRAME_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class FrameParagraphFactory(BaseParagraphFactory):
+    """Framed text paragraph factory"""
+
+    name = _("Framed text paragraph")
+    content_type = FrameParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IFrameParagraph, provides=IContentChecker)
+class FrameParagraphContentChecker(BaseParagraphContentChecker):
+    """Framed text 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', 'body'):
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    field_title = translate(IFrameParagraph[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))
+        return output
+
+
+@vocabulary_config(name=FRAME_PARAGRAPH_RENDERERS)
+class FrameParagraphRendererVocabulary(SimpleVocabulary):
+    """Framed text paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IFrameParagraph.providedBy(context):
+            context = FrameParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(FrameParagraphRendererVocabulary, self).__init__(terms)