|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 from pyams_utils.traversing import get_parent |
|
13 |
|
14 __docformat__ = 'restructuredtext' |
|
15 |
|
16 |
|
17 # import standard library |
|
18 |
|
19 # import interfaces |
|
20 from pyams_content.component.paragraph.interfaces import IParagraphFactory |
|
21 from pyams_content.component.video.interfaces import IExternalVideoParagraph |
|
22 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE |
|
23 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator |
|
24 |
|
25 # import packages |
|
26 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphFactory |
|
27 from pyams_content.component.video import ExternalVideo, ExternalVideoContentChecker |
|
28 from pyams_utils.adapter import adapter_config |
|
29 from pyams_utils.registry import utility_config, get_utility |
|
30 from pyams_utils.request import check_request |
|
31 from zope.interface import implementer |
|
32 from zope.schema.fieldproperty import FieldProperty |
|
33 |
|
34 from pyams_content import _ |
|
35 |
|
36 |
|
37 @implementer(IExternalVideoParagraph) |
|
38 class ExternalVideoParagraph(ExternalVideo, BaseParagraph): |
|
39 """External video paragraph""" |
|
40 |
|
41 icon_class = 'fa-youtube-play' |
|
42 icon_hint = _("External video") |
|
43 |
|
44 body = FieldProperty(IExternalVideoParagraph['body']) |
|
45 |
|
46 |
|
47 @utility_config(name='External video', provides=IParagraphFactory) |
|
48 class ExternalVideoParagraphFactory(BaseParagraphFactory): |
|
49 """External video paragraph factory""" |
|
50 |
|
51 name = _("External video") |
|
52 content_type = ExternalVideoParagraph |
|
53 |
|
54 |
|
55 @adapter_config(context=IExternalVideoParagraph, provides=IContentChecker) |
|
56 class ExternalVideoParagraphContentChecker(ExternalVideoContentChecker): |
|
57 """External video paragraph content checker""" |
|
58 |
|
59 @property |
|
60 def label(self): |
|
61 request = check_request() |
|
62 translate = request.localizer.translate |
|
63 return II18n(self.context).query_attribute('title', request) or \ |
|
64 '({0})'.format(translate(self.context.icon_hint).lower()) |
|
65 |
|
66 def inner_check(self, request): |
|
67 output = super(ExternalVideoParagraphContentChecker, self).inner_check(request) |
|
68 translate = request.localizer.translate |
|
69 manager = get_parent(self.context, II18nManager) |
|
70 if manager is not None: |
|
71 langs = manager.get_languages() |
|
72 else: |
|
73 negotiator = get_utility(INegotiator) |
|
74 langs = (negotiator.server_language, ) |
|
75 missing_value = translate(MISSING_VALUE) |
|
76 missing_lang_value = translate(MISSING_LANG_VALUE) |
|
77 i18n = II18n(self.context) |
|
78 for attr in ('title', ): |
|
79 for lang in langs: |
|
80 value = i18n.get_attribute(attr, lang, request) |
|
81 if not value: |
|
82 if len(langs) == 1: |
|
83 output.insert(0, missing_value.format(field=translate(IExternalVideoParagraph[attr].title))) |
|
84 else: |
|
85 output.insert(0, missing_lang_value.format(field=translate(IExternalVideoParagraph[attr].title), |
|
86 lang=lang)) |
|
87 return output |