src/pyams_content/component/paragraph/contact.py
changeset 412 b5a33146bd3d
child 419 824fef808845
equal deleted inserted replaced
411:d2425a3c3a49 412:b5a33146bd3d
       
     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.contact import IContactParagraph, CONTACT_PARAGRAPH_TYPE
       
    21 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
       
    22 from pyams_file.interfaces import IImage, IResponsiveImage
       
    23 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
       
    24 
       
    25 # import packages
       
    26 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphFactory, BaseParagraphContentChecker
       
    27 from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
       
    28 from pyams_file.property import FileProperty
       
    29 from pyams_utils.adapter import adapter_config
       
    30 from pyams_utils.registry import utility_config, get_utility
       
    31 from pyams_utils.request import check_request
       
    32 from pyams_utils.traversing import get_parent
       
    33 from pyams_utils.vocabulary import vocabulary_config
       
    34 from zope.interface import implementer, alsoProvides
       
    35 from zope.schema.fieldproperty import FieldProperty
       
    36 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
       
    37 
       
    38 from pyams_content import _
       
    39 
       
    40 
       
    41 @implementer(IContactParagraph)
       
    42 class ContactParagraph(RenderedContentMixin, BaseParagraph):
       
    43     """Contact paragraph"""
       
    44 
       
    45     icon_class = 'fa-id-card-o'
       
    46     icon_hint = _("Contact card")
       
    47 
       
    48     name = FieldProperty(IContactParagraph['name'])
       
    49     charge = FieldProperty(IContactParagraph['charge'])
       
    50     _photo = FileProperty(IContactParagraph['photo'])
       
    51     gps_location = FieldProperty(IContactParagraph['gps_location'])
       
    52     address = FieldProperty(IContactParagraph['address'])
       
    53     renderer = FieldProperty(IContactParagraph['renderer'])
       
    54 
       
    55     @property
       
    56     def photo(self):
       
    57         return self._photo
       
    58 
       
    59     @photo.setter
       
    60     def photo(self, value):
       
    61         self._photo = value
       
    62         if IImage.providedBy(self._photo):
       
    63             alsoProvides(self._photo, IResponsiveImage)
       
    64 
       
    65 
       
    66 @utility_config(name=CONTACT_PARAGRAPH_TYPE, provides=IParagraphFactory)
       
    67 class ContactParagraphFactory(BaseParagraphFactory):
       
    68     """Contact paragraph factory"""
       
    69 
       
    70     name = _("Contact card")
       
    71     content_type = ContactParagraph
       
    72     secondary_menu = True
       
    73 
       
    74 
       
    75 @adapter_config(context=IContactParagraph, provides=IContentChecker)
       
    76 class ContactParagraphContentChecker(BaseParagraphContentChecker):
       
    77     """Contact paragraph content checker"""
       
    78 
       
    79     def inner_check(self, request):
       
    80         output = []
       
    81         translate = request.localizer.translate
       
    82         manager = get_parent(self.context, II18nManager)
       
    83         if manager is not None:
       
    84             langs = manager.get_languages()
       
    85         else:
       
    86             negotiator = get_utility(INegotiator)
       
    87             langs = (negotiator.server_language, )
       
    88         i18n = II18n(self.context)
       
    89         for lang in langs:
       
    90             value = i18n.get_attribute('charge', lang, request)
       
    91             if not value:
       
    92                 field_title = translate(IContactParagraph['charge'].title)
       
    93                 if len(langs) == 1:
       
    94                     output.append(translate(MISSING_VALUE).format(field=field_title))
       
    95                 else:
       
    96                     output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
       
    97         for attr in ('name', 'photo', 'gps_location', 'address'):
       
    98             value = getattr(self.context, attr, None)
       
    99             if not value:
       
   100                 field_title = translate(IContactParagraph[attr].title)
       
   101                 output.append(translate(MISSING_VALUE).format(field=field_title))
       
   102         return output
       
   103 
       
   104 
       
   105 @vocabulary_config(name='PyAMS contact renderers')
       
   106 class ContactParagraphRendererVocabulary(SimpleVocabulary):
       
   107     """Contact paragraph renderers vocabulary"""
       
   108 
       
   109     def __init__(self, context=None):
       
   110         request = check_request()
       
   111         translate = request.localizer.translate
       
   112         registry = request.registry
       
   113         if not IContactParagraph.providedBy(context):
       
   114             context = ContactParagraph()
       
   115         terms = [SimpleTerm(name, title=translate(adapter.label))
       
   116                  for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
       
   117                                              key=lambda x: x[1].weight)]
       
   118         super(ContactParagraphRendererVocabulary, self).__init__(terms)