src/pyams_content/component/paragraph/contact.py
changeset 412 b5a33146bd3d
child 419 824fef808845
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/contact.py	Thu Mar 01 17:53:34 2018 +0100
@@ -0,0 +1,118 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.contact import IContactParagraph, CONTACT_PARAGRAPH_TYPE
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
+from pyams_file.interfaces import IImage, IResponsiveImage
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
+
+# import packages
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphFactory, BaseParagraphContentChecker
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
+from pyams_file.property import FileProperty
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from zope.interface import implementer, alsoProvides
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from pyams_content import _
+
+
+@implementer(IContactParagraph)
+class ContactParagraph(RenderedContentMixin, BaseParagraph):
+    """Contact paragraph"""
+
+    icon_class = 'fa-id-card-o'
+    icon_hint = _("Contact card")
+
+    name = FieldProperty(IContactParagraph['name'])
+    charge = FieldProperty(IContactParagraph['charge'])
+    _photo = FileProperty(IContactParagraph['photo'])
+    gps_location = FieldProperty(IContactParagraph['gps_location'])
+    address = FieldProperty(IContactParagraph['address'])
+    renderer = FieldProperty(IContactParagraph['renderer'])
+
+    @property
+    def photo(self):
+        return self._photo
+
+    @photo.setter
+    def photo(self, value):
+        self._photo = value
+        if IImage.providedBy(self._photo):
+            alsoProvides(self._photo, IResponsiveImage)
+
+
+@utility_config(name=CONTACT_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class ContactParagraphFactory(BaseParagraphFactory):
+    """Contact paragraph factory"""
+
+    name = _("Contact card")
+    content_type = ContactParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IContactParagraph, provides=IContentChecker)
+class ContactParagraphContentChecker(BaseParagraphContentChecker):
+    """Contact paragraph content checker"""
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        manager = get_parent(self.context, II18nManager)
+        if manager is not None:
+            langs = manager.get_languages()
+        else:
+            negotiator = get_utility(INegotiator)
+            langs = (negotiator.server_language, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            value = i18n.get_attribute('charge', lang, request)
+            if not value:
+                field_title = translate(IContactParagraph['charge'].title)
+                if len(langs) == 1:
+                    output.append(translate(MISSING_VALUE).format(field=field_title))
+                else:
+                    output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        for attr in ('name', 'photo', 'gps_location', 'address'):
+            value = getattr(self.context, attr, None)
+            if not value:
+                field_title = translate(IContactParagraph[attr].title)
+                output.append(translate(MISSING_VALUE).format(field=field_title))
+        return output
+
+
+@vocabulary_config(name='PyAMS contact renderers')
+class ContactParagraphRendererVocabulary(SimpleVocabulary):
+    """Contact paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IContactParagraph.providedBy(context):
+            context = ContactParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(ContactParagraphRendererVocabulary, self).__init__(terms)