src/pyams_content/component/illustration/zmi/paragraph.py
changeset 140 67bad9f880ee
parent 22 c270ea8f041e
child 150 90a1cb45caf8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/illustration/zmi/paragraph.py	Mon Sep 11 14:54:30 2017 +0200
@@ -0,0 +1,215 @@
+#
+# 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, IParagraphSummary
+from pyams_content.component.illustration.interfaces import IIllustrationRenderer, IIllustration, IIllustrationParagraph
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_content.shared.common.interfaces import IWfSharedContent
+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.illustration.paragraph import Illustration
+from pyams_content.component.paragraph.zmi import BaseParagraphAJAXEditForm
+from pyams_content.component.paragraph.zmi.container import ParagraphContainerView
+from pyams_form.form import AJAXAddForm
+from pyams_form.security import ProtectedFormObjectMixin
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config
+from pyams_utils.traversing import get_parent
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer
+
+from pyams_content import _
+
+
+#
+# Illustration
+#
+
+@viewlet_config(name='add-illustration.menu', context=IParagraphContainerTarget, view=ParagraphContainerView,
+                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=60)
+class IllustrationAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem):
+    """Illustration add menu"""
+
+    label = _("Add illustration...")
+    label_css_class = 'fa fa-fw fa-file-image-o'
+    url = 'add-illustration.html'
+    modal_target = True
+
+
+@pagelet_config(name='add-illustration.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class IllustrationAddForm(AdminDialogAddForm):
+    """Illustration add form"""
+
+    legend = _("Add new illustration")
+    dialog_class = 'modal-large'
+    icon_css_class = 'fa fa-fw fa-file-image-o'
+
+    fields = field.Fields(IIllustrationParagraph).omit('__parent__', '__name__', 'visible')
+    ajax_handler = 'add-illustration.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(IllustrationAddForm, self).updateWidgets(prefix)
+        if 'description' in self.widgets:
+            self.widgets['description'].widget_css_class = 'textarea'
+
+    def create(self, data):
+        return Illustration()
+
+    def add(self, object):
+        IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-illustration.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class IllustrationAJAXAddForm(AJAXAddForm, IllustrationAddForm):
+    """HTML paragraph add form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        return {'status': 'reload',
+                'location': '#paragraphs.html'}
+
+
+@pagelet_config(name='properties.html', context=IIllustrationParagraph, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class IllustrationPropertiesEditForm(AdminDialogEditForm):
+    """Illustration properties edit form"""
+
+    @property
+    def title(self):
+        content = get_parent(self.context, IWfSharedContent)
+        return II18n(content).query_attribute('title', request=self.request)
+
+    legend = _("Edit illustration properties")
+    dialog_class = 'modal-large'
+    icon_css_class = 'fa fa-fw fa-file-image-o'
+
+    fields = field.Fields(IIllustrationParagraph).omit('__parent__', '__name__', 'visible')
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(IllustrationPropertiesEditForm, self).updateWidgets(prefix)
+        if 'description' in self.widgets:
+            self.widgets['description'].widget_css_class = 'textarea'
+
+
+@view_config(name='properties.json', context=IIllustrationParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class IllustrationPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, IllustrationPropertiesEditForm):
+    """Illustration properties edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(IllustrationPropertiesAJAXEditForm, self).get_ajax_output(changes)
+        if 'title' in changes.get(IIllustration, ()):
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'object_type': 'paragraph',
+                            'object_name': self.context.__name__,
+                            'title': II18n(self.context).query_attribute('title', request=self.request),
+                            'visible': self.context.visible}
+            })
+        return output
+
+
+@adapter_config(context=(IIllustrationParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm)
+class IllustrationInnerEditForm(IllustrationPropertiesEditForm):
+    """Illustration 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=IIllustrationParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class IllustrationInnerAJAXEditForm(BaseParagraphAJAXEditForm, IllustrationInnerEditForm):
+    """Illustration paragraph inner edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(IllustrationInnerAJAXEditForm, self).get_ajax_output(changes)
+        updated = changes.get(IIllustration, ())
+        if 'title' in updated:
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'object_type': 'paragraph',
+                            'object_name': self.context.__name__,
+                            'title': II18n(self.context).query_attribute('title', request=self.request),
+                            'visible': self.context.visible}
+            })
+        if 'data' in updated:
+            # we have to commit transaction to be able to handle blobs...
+            ITransactionManager(self.context).get().commit()
+            context = IIllustrationParagraph(self.context)
+            form = IllustrationInnerEditForm(context, self.request)
+            form.update()
+            output.setdefault('callbacks', []).append({
+                'callback': 'PyAMS_content.illustration.afterUpdateCallback',
+                'options': {'parent': '{0}_{1}_{2}'.format(self.context.__class__.__name__,
+                                                           getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+                                                           form.id),
+                            'form': form.render()}
+            })
+        return output
+
+
+#
+# Illustration summary
+#
+
+@adapter_config(context=(IIllustrationParagraph, IPyAMSLayer), provides=IParagraphSummary)
+class IllustrationSummary(ContextRequestAdapter):
+    """Illustration renderer"""
+
+    def __init__(self, context, request):
+        super(IllustrationSummary, self).__init__(context, request)
+        self.renderer = request.registry.queryMultiAdapter((context, request), IIllustrationRenderer,
+                                                           name=self.context.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 ''