Added contact card
authorThierry Florac <tflorac@ulthar.net>
Thu, 01 Mar 2018 17:53:34 +0100
changeset 412 b5a33146bd3d
parent 411 d2425a3c3a49
child 413 052c4fd067f8
Added contact card
src/pyams_content/component/paragraph/contact.py
src/pyams_content/component/paragraph/interfaces/contact.py
src/pyams_content/component/paragraph/zmi/contact.py
--- /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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/interfaces/contact.py	Thu Mar 01 17:53:34 2018 +0100
@@ -0,0 +1,62 @@
+#
+# 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 import IBaseParagraph
+from pyams_content.features.renderer import IRenderedContent
+
+# import packages
+from pyams_file.schema import ImageField
+from pyams_gis.schema import GeoPointField
+from pyams_i18n.schema import I18nTextLineField
+from zope.schema import TextLine, Text, Choice
+
+from pyams_content import _
+
+
+#
+# Contact paragraph
+#
+
+CONTACT_PARAGRAPH_TYPE = 'Contact'
+
+
+class IContactParagraph(IRenderedContent, IBaseParagraph):
+    """Contact paragraph interface"""
+
+    name = TextLine(title=_("Contact identity"),
+                    description=_("Name of the contact"),
+                    required=True)
+
+    charge = I18nTextLineField(title=_("In charge of"),
+                               description=_("Label of contact function"),
+                               required=False)
+
+    photo = ImageField(title=_("Photo"),
+                       description=_("Use 'browse' button to select contact picture"),
+                       required=False)
+
+    gps_location = GeoPointField(title=_("GPS location"),
+                                 description=_("GPS coordinates used to locate contact"),
+                                 required=False)
+
+    address = Text(title=_("Address"),
+                   required=False)
+
+    renderer = Choice(title=_("Contact template"),
+                      description=_("Presentation template used for this contact"),
+                      vocabulary='PyAMS contact renderers',
+                      default='hidden')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/contact.py	Thu Mar 01 17:53:34 2018 +0100
@@ -0,0 +1,191 @@
+#
+# 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 IParagraphContainerTarget, IParagraphContainer, \
+    IBaseParagraph, IParagraphPreview
+from pyams_content.component.paragraph.interfaces.contact import CONTACT_PARAGRAPH_TYPE, IContactParagraph
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphContainerView, IParagraphInnerEditor
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_form.interfaces.form import IInnerForm, IEditFormButtons
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from transaction.interfaces import ITransactionManager
+from z3c.form.interfaces import INPUT_MODE
+
+# import packages
+from pyams_content.component.paragraph.contact import ContactParagraph
+from pyams_content.component.paragraph.zmi import BaseParagraphAddMenu, BaseParagraphAJAXAddForm, \
+    BaseParagraphPropertiesEditForm, BaseParagraphAJAXEditForm
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_utils.adapter import adapter_config
+from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
+from pyams_zmi.form import AdminDialogAddForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer
+
+from pyams_content import _
+
+
+@viewlet_config(name='add-contact-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=650)
+class ContactParagraphAddMenu(BaseParagraphAddMenu):
+    """Contact paragraph add menu"""
+
+    label = _("Add contact card...")
+    label_css_class = 'fa fa-fw fa-id-card-o'
+    url = 'add-contact-paragraph.html'
+    paragraph_type = CONTACT_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-contact-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class ContactParagraphAddForm(AdminDialogAddForm):
+    """Contact paragraph add form"""
+
+    legend = _("Add new contact card")
+    dialog_class = 'modal-large'
+    icon_css_class = 'fa fa-fw fa-id-card-o'
+
+    fields = field.Fields(IContactParagraph).omit('__parent__', '__name__', 'visible')
+    ajax_handler = 'add-contact-paragraph.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(ContactParagraphAddForm, self).updateWidgets(prefix)
+        if 'address' in self.widgets:
+            self.widgets['address'].widget_css_class = 'textarea'
+
+    def create(self, data):
+        return ContactParagraph()
+
+    def add(self, object):
+        IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-contact-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class ContactParagraphAJAXAddForm(BaseParagraphAJAXAddForm, ContactParagraphAddForm):
+    """Contact paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IContactParagraph, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class ContactParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+    """Contact paragraph properties edit form"""
+
+    legend = _("Edit contact card properties")
+    icon_css_class = 'fa fa-fw fa-id-card-o'
+
+    fields = field.Fields(IContactParagraph).omit('__parent__', '__name__', 'visible')
+    fields['renderer'].widgetFactory = RendererFieldWidget
+
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(ContactParagraphPropertiesEditForm, self).updateWidgets(prefix)
+        if 'address' in self.widgets:
+            self.widgets['address'].widget_css_class = 'textarea'
+
+
+@view_config(name='properties.json', context=IContactParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class ContactParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphPropertiesEditForm):
+    """Contact paragraph properties edit form, JSON renderer"""
+
+
+@adapter_config(context=(IContactParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm)
+class ContactParagraphInnerEditForm(ContactParagraphPropertiesEditForm):
+    """Contact paragraph inner edit form"""
+
+    legend = None
+    ajax_handler = 'inner-properties.json'
+
+    @property
+    def buttons(self):
+        if self.mode == INPUT_MODE:
+            return button.Buttons(IEditFormButtons)
+        else:
+            return button.Buttons()
+
+
+@view_config(name='inner-properties.json', context=IContactParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class ContactParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphInnerEditForm):
+    """Contact paragraph inner edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(ContactParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
+        updated = changes.get(IBaseParagraph, ())
+        if 'title' in updated:
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'PyAMS_content.paragraphs.refreshParagraph',
+                    'object_name': self.context.__name__,
+                    'title': II18n(self.context).query_attribute('title', request=self.request),
+                    'visible': self.context.visible
+                }
+            })
+        updated = changes.get(IContactParagraph, ())
+        if ('photo' in updated) or ('renderer' in updated):
+            # we have to commit transaction to be able to handle blobs...
+            ITransactionManager(self.context).get().commit()
+            form = ContactParagraphInnerEditForm(self.context, self.request)
+            form.update()
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'object_id': '{0}_{1}_{2}'.format(
+                        self.context.__class__.__name__,
+                        getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+                        form.id),
+                    'content': form.render()
+                }
+            })
+        return output
+
+
+#
+# Contact paragraph preview
+#
+
+@adapter_config(context=(IContactParagraph, IPyAMSLayer), provides=IParagraphPreview)
+class ContactParagraphPreview(BaseContentProvider):
+    """Contact paragraph preview"""
+
+    def __init__(self, context, request):
+        super(ContactParagraphPreview, self).__init__(context, request)
+        self.renderer = self.context.get_renderer()
+
+    language = None
+
+    def update(self):
+        if self.renderer is not None:
+            self.renderer.language = self.language
+            self.renderer.update()
+
+    def render(self):
+        if self.renderer is not None:
+            return self.renderer.render()
+        else:
+            return ''