src/pyams_content/component/paragraph/html.py
changeset 355 5dce53509832
parent 241 50452584f7ae
child 407 0ef5de2d5674
equal deleted inserted replaced
354:871c7cb35fd6 355:5dce53509832
    20 from pyams_content.component.association.interfaces import IAssociationContainer
    20 from pyams_content.component.association.interfaces import IAssociationContainer
    21 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget, IBaseExtFile
    21 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget, IBaseExtFile
    22 from pyams_content.component.illustration.interfaces import IIllustrationTarget
    22 from pyams_content.component.illustration.interfaces import IIllustrationTarget
    23 from pyams_content.component.links.interfaces import ILinkContainerTarget, IInternalLink, IExternalLink, IMailtoLink
    23 from pyams_content.component.links.interfaces import ILinkContainerTarget, IInternalLink, IExternalLink, IMailtoLink
    24 from pyams_content.component.paragraph.interfaces import IParagraphFactory
    24 from pyams_content.component.paragraph.interfaces import IParagraphFactory
    25 from pyams_content.component.paragraph.interfaces.html import IHTMLParagraph
    25 from pyams_content.component.paragraph.interfaces.html import IRawParagraph, IHTMLParagraph
    26 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
    26 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
    27 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
    27 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
    28 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
    28 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
    29 
    29 
    30 # import packages
    30 # import packages
    31 from pyams_content.component.links import InternalLink, ExternalLink, MailtoLink
    31 from pyams_content.component.links import InternalLink, ExternalLink, MailtoLink
    32 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker
    32 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
    33 from pyams_utils.adapter import adapter_config
    33 from pyams_utils.adapter import adapter_config
    34 from pyams_utils.registry import utility_config, get_utility
    34 from pyams_utils.registry import utility_config, get_utility
    35 from pyams_utils.request import check_request
    35 from pyams_utils.request import check_request
    36 from pyams_utils.traversing import get_parent
    36 from pyams_utils.traversing import get_parent
    37 from pyams_utils.url import absolute_url
    37 from pyams_utils.url import absolute_url
    44 
    44 
    45 from pyams_content import _
    45 from pyams_content import _
    46 
    46 
    47 
    47 
    48 #
    48 #
       
    49 # Raw HTML paragraph
       
    50 #
       
    51 
       
    52 @implementer(IRawParagraph)
       
    53 class RawParagraph(BaseParagraph):
       
    54     """Raw HTML paragraph"""
       
    55 
       
    56     icon_class = 'fa-code'
       
    57     icon_hint = _("Raw HTML ")
       
    58 
       
    59     body = FieldProperty(IRawParagraph['body'])
       
    60 
       
    61 
       
    62 @utility_config(name='raw', provides=IParagraphFactory)
       
    63 class RawParagraphFactory(BaseParagraphFactory):
       
    64     """Raw paragraph factory"""
       
    65 
       
    66     name = _("Raw HTML paragraph")
       
    67     content_type = RawParagraph
       
    68     custom_menu = True
       
    69 
       
    70 
       
    71 @adapter_config(context=IRawParagraph, provides=IContentChecker)
       
    72 class RawParagraphContentChecker(BaseParagraphContentChecker):
       
    73     """Raw HTML paragraph content checker"""
       
    74 
       
    75     def inner_check(self, request):
       
    76         output = []
       
    77         translate = request.localizer.translate
       
    78         manager = get_parent(self.context, II18nManager)
       
    79         if manager is not None:
       
    80             langs = manager.get_languages()
       
    81         else:
       
    82             negotiator = get_utility(INegotiator)
       
    83             langs = (negotiator.server_language, )
       
    84         i18n = II18n(self.context)
       
    85         for lang in langs:
       
    86             value = i18n.get_attribute('body', lang, request)
       
    87             if not value:
       
    88                 field_title = translate(IRawParagraph['body'].title)
       
    89                 if len(langs) == 1:
       
    90                     output.append(translate(MISSING_VALUE).format(field=field_title))
       
    91                 else:
       
    92                     output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
       
    93         return output
       
    94 
       
    95 
       
    96 #
    49 # HTML paragraph
    97 # HTML paragraph
    50 #
    98 #
    51 
    99 
    52 @implementer(IHTMLParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
   100 @implementer(IHTMLParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
    53 class HTMLParagraph(BaseParagraph):
   101 class HTMLParagraph(BaseParagraph):
    54     """HTML paragraph"""
   102     """HTML paragraph"""
    55 
   103 
    56     icon_class = 'fa-html5'
   104     icon_class = 'fa-html5'
    57     icon_hint = _("HTML paragraph")
   105     icon_hint = _("Rich text")
    58 
   106 
    59     body = FieldProperty(IHTMLParagraph['body'])
   107     body = FieldProperty(IHTMLParagraph['body'])
    60 
   108 
    61 
   109 
    62 @utility_config(name='HTML', provides=IParagraphFactory)
   110 @utility_config(name='HTML', provides=IParagraphFactory)
    63 class HTMLParagraphFactory(object):
   111 class HTMLParagraphFactory(BaseParagraphFactory):
    64     """HTML paragraph factory"""
   112     """HTML paragraph factory"""
    65 
   113 
    66     name = _("HTML paragraph")
   114     name = _("Rich text paragraph")
    67     content_type = HTMLParagraph
   115     content_type = HTMLParagraph
    68 
   116 
    69 
   117 
    70 FULL_EMAIL = re.compile('(.*) \<(.*)\>')
   118 FULL_EMAIL = re.compile('(.*) \<(.*)\>')
    71 
   119