src/pyams_content/component/video/paragraph.py
changeset 404 0ba2bb1a692e
child 407 0ef5de2d5674
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/video/paragraph.py	Wed Feb 21 17:18:04 2018 +0100
@@ -0,0 +1,87 @@
+#
+# 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.
+#
+from pyams_utils.traversing import get_parent
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.video.interfaces import IExternalVideoParagraph
+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, BaseParagraphFactory
+from pyams_content.component.video import ExternalVideo, ExternalVideoContentChecker
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.request import check_request
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_content import _
+
+
+@implementer(IExternalVideoParagraph)
+class ExternalVideoParagraph(ExternalVideo, BaseParagraph):
+    """External video paragraph"""
+
+    icon_class = 'fa-youtube-play'
+    icon_hint = _("External video")
+
+    body = FieldProperty(IExternalVideoParagraph['body'])
+
+
+@utility_config(name='External video', provides=IParagraphFactory)
+class ExternalVideoParagraphFactory(BaseParagraphFactory):
+    """External video paragraph factory"""
+
+    name = _("External video")
+    content_type = ExternalVideoParagraph
+
+
+@adapter_config(context=IExternalVideoParagraph, provides=IContentChecker)
+class ExternalVideoParagraphContentChecker(ExternalVideoContentChecker):
+    """External video paragraph content checker"""
+
+    @property
+    def label(self):
+        request = check_request()
+        translate = request.localizer.translate
+        return II18n(self.context).query_attribute('title', request) or \
+            '({0})'.format(translate(self.context.icon_hint).lower())
+
+    def inner_check(self, request):
+        output = super(ExternalVideoParagraphContentChecker, self).inner_check(request)
+        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, )
+        missing_value = translate(MISSING_VALUE)
+        missing_lang_value = translate(MISSING_LANG_VALUE)
+        i18n = II18n(self.context)
+        for attr in ('title', ):
+            for lang in langs:
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    if len(langs) == 1:
+                        output.insert(0, missing_value.format(field=translate(IExternalVideoParagraph[attr].title)))
+                    else:
+                        output.insert(0, missing_lang_value.format(field=translate(IExternalVideoParagraph[attr].title),
+                                                                   lang=lang))
+        return output