src/pyams_content/component/paragraph/keypoint.py
changeset 416 c848b7ceefb7
parent 408 e1d40ed27899
child 421 20a2b671ade1
equal deleted inserted replaced
415:43f42d74cdb8 416:c848b7ceefb7
       
     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 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_content.component.paragraph.interfaces import IParagraphFactory
       
    20 from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE
       
    21 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
       
    22 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
       
    23 
       
    24 # import packages
       
    25 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
       
    26 from pyams_utils.adapter import adapter_config
       
    27 from pyams_utils.registry import utility_config, get_utility
       
    28 from pyams_utils.text import get_text_start
       
    29 from pyams_utils.traversing import get_parent
       
    30 from zope.interface import implementer
       
    31 from zope.schema.fieldproperty import FieldProperty
       
    32 
       
    33 from pyams_content import _
       
    34 
       
    35 
       
    36 @implementer(IKeypointsParagraph)
       
    37 class KeypointsParagraph(BaseParagraph):
       
    38     """Key points paragraph"""
       
    39 
       
    40     icon_class = 'fa-key'
       
    41     icon_hint = _("Key points")
       
    42 
       
    43     @property
       
    44     def title(self):
       
    45         body = II18n(self).query_attribute('body')
       
    46         return get_text_start(body, 50, 10)
       
    47 
       
    48     body = FieldProperty(IKeypointsParagraph['body'])
       
    49 
       
    50 
       
    51 @utility_config(name=KEYPOINTS_PARAGRAPH_TYPE, provides=IParagraphFactory)
       
    52 class KeypointsParagraphFactory(BaseParagraphFactory):
       
    53     """Key points paragraph factory"""
       
    54 
       
    55     name = _("Key points paragraph")
       
    56     content_type = KeypointsParagraph
       
    57     secondary_menu = True
       
    58 
       
    59 
       
    60 @adapter_config(context=IKeypointsParagraph, provides=IContentChecker)
       
    61 class KeypointsParagraphContentChecker(BaseParagraphContentChecker):
       
    62     """Key points paragraph content checker"""
       
    63 
       
    64     def inner_check(self, request):
       
    65         output = []
       
    66         translate = request.localizer.translate
       
    67         manager = get_parent(self.context, II18nManager)
       
    68         if manager is not None:
       
    69             langs = manager.get_languages()
       
    70         else:
       
    71             negotiator = get_utility(INegotiator)
       
    72             langs = (negotiator.server_language, )
       
    73         i18n = II18n(self.context)
       
    74         for lang in langs:
       
    75             value = i18n.get_attribute('body', lang, request)
       
    76             if not value:
       
    77                 field_title = translate(IKeypointsParagraph['body'].title)
       
    78                 if len(langs) == 1:
       
    79                     output.append(translate(MISSING_VALUE).format(field=field_title))
       
    80                 else:
       
    81                     output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
       
    82         return output