src/pyams_content/component/paragraph/keypoint.py
changeset 416 c848b7ceefb7
parent 408 e1d40ed27899
child 421 20a2b671ade1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/keypoint.py	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,82 @@
+#
+# 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.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE
+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_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.text import get_text_start
+from pyams_utils.traversing import get_parent
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_content import _
+
+
+@implementer(IKeypointsParagraph)
+class KeypointsParagraph(BaseParagraph):
+    """Key points paragraph"""
+
+    icon_class = 'fa-key'
+    icon_hint = _("Key points")
+
+    @property
+    def title(self):
+        body = II18n(self).query_attribute('body')
+        return get_text_start(body, 50, 10)
+
+    body = FieldProperty(IKeypointsParagraph['body'])
+
+
+@utility_config(name=KEYPOINTS_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class KeypointsParagraphFactory(BaseParagraphFactory):
+    """Key points paragraph factory"""
+
+    name = _("Key points paragraph")
+    content_type = KeypointsParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IKeypointsParagraph, provides=IContentChecker)
+class KeypointsParagraphContentChecker(BaseParagraphContentChecker):
+    """Key points 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:
+            value = i18n.get_attribute('body', lang, request)
+            if not value:
+                field_title = translate(IKeypointsParagraph['body'].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