src/pyams_content/component/paragraph/text.py
changeset 443 326adf3362d8
parent 407 0ef5de2d5674
equal deleted inserted replaced
442:5dc8421cbcfc 443:326adf3362d8
       
     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.extfile.interfaces import IExtFileContainerTarget
       
    20 from pyams_content.component.illustration.interfaces import IIllustrationTarget
       
    21 from pyams_content.component.links.interfaces import ILinkContainerTarget
       
    22 from pyams_content.component.paragraph.interfaces import IParagraphFactory
       
    23 from pyams_content.component.paragraph.interfaces.text import ITextParagraph, TEXT_PARAGRAPH_TYPE, \
       
    24     TEXT_PARAGRAPH_RENDERERS
       
    25 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
       
    26 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
       
    27 
       
    28 # import packages
       
    29 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
       
    30 from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
       
    31 from pyams_utils.adapter import adapter_config
       
    32 from pyams_utils.registry import utility_config, get_utility
       
    33 from pyams_utils.request import check_request
       
    34 from pyams_utils.traversing import get_parent
       
    35 from pyams_utils.vocabulary import vocabulary_config
       
    36 from zope.interface import implementer
       
    37 from zope.schema.fieldproperty import FieldProperty
       
    38 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
       
    39 
       
    40 from pyams_content import _
       
    41 
       
    42 
       
    43 #
       
    44 # Text paragraph
       
    45 #
       
    46 
       
    47 @implementer(ITextParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
       
    48 class TextParagraph(RenderedContentMixin, BaseParagraph):
       
    49     """Framed text paragraph"""
       
    50 
       
    51     icon_class = 'fa-list-alt'
       
    52     icon_hint = _("Framed text")
       
    53 
       
    54     body = FieldProperty(ITextParagraph['body'])
       
    55     renderer = FieldProperty(ITextParagraph['renderer'])
       
    56 
       
    57 
       
    58 @utility_config(name=TEXT_PARAGRAPH_TYPE, provides=IParagraphFactory)
       
    59 class TextParagraphFactory(BaseParagraphFactory):
       
    60     """Framed text paragraph factory"""
       
    61 
       
    62     name = _("Framed text paragraph")
       
    63     content_type = TextParagraph
       
    64     secondary_menu = True
       
    65 
       
    66 
       
    67 @adapter_config(context=ITextParagraph, provides=IContentChecker)
       
    68 class TextParagraphContentChecker(BaseParagraphContentChecker):
       
    69     """Framed text paragraph content checker"""
       
    70 
       
    71     def inner_check(self, request):
       
    72         output = []
       
    73         translate = request.localizer.translate
       
    74         manager = get_parent(self.context, II18nManager)
       
    75         if manager is not None:
       
    76             langs = manager.get_languages()
       
    77         else:
       
    78             negotiator = get_utility(INegotiator)
       
    79             langs = (negotiator.server_language, )
       
    80         i18n = II18n(self.context)
       
    81         for lang in langs:
       
    82             for attr in ('title', 'body'):
       
    83                 value = i18n.get_attribute(attr, lang, request)
       
    84                 if not value:
       
    85                     field_title = translate(ITextParagraph[attr].title)
       
    86                     if len(langs) == 1:
       
    87                         output.append(translate(MISSING_VALUE).format(field=field_title))
       
    88                     else:
       
    89                         output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
       
    90         return output
       
    91 
       
    92 
       
    93 @vocabulary_config(name=TEXT_PARAGRAPH_RENDERERS)
       
    94 class TextParagraphRendererVocabulary(SimpleVocabulary):
       
    95     """Framed text paragraph renderers vocabulary"""
       
    96 
       
    97     def __init__(self, context=None):
       
    98         request = check_request()
       
    99         translate = request.localizer.translate
       
   100         registry = request.registry
       
   101         if not ITextParagraph.providedBy(context):
       
   102             context = TextParagraph()
       
   103         terms = [SimpleTerm(name, title=translate(adapter.label))
       
   104                  for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
       
   105                                              key=lambda x: x[1].weight)]
       
   106         super(TextParagraphRendererVocabulary, self).__init__(terms)