src/pyams_content/component/illustration/zmi/__init__.py
changeset 140 67bad9f880ee
child 146 221775601fe7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/illustration/zmi/__init__.py	Mon Sep 11 14:54:30 2017 +0200
@@ -0,0 +1,131 @@
+#
+# 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.illustration.interfaces import IIllustration, IIllustrationRenderer, IIllustrationTarget
+from pyams_form.interfaces.form import IInnerSubForm
+from pyams_i18n.interfaces import II18n
+from pyams_skin.layer import IPyAMSLayer
+from pyams_zmi.interfaces import IPropertiesEditForm
+from transaction.interfaces import ITransactionManager
+
+# import packages
+from pyams_template.template import get_view_template, template_config
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config
+from pyams_zmi.form import InnerAdminEditForm
+from z3c.form import field
+
+from pyams_content import _
+
+
+#
+# Illustration renderers
+#
+
+class BaseIllustrationRenderer(ContextRequestAdapter):
+    """Base illustration renderer"""
+
+    language = None
+
+    def update(self):
+        i18n = II18n(self.context)
+        if self.language:
+            self.legend = i18n.get_attribute('alt_title', self.language, request=self.request)
+        else:
+            self.legend = i18n.query_attribute('alt_title', request=self.request)
+
+    render = get_view_template()
+
+
+@adapter_config(name='default', context=(IIllustration, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/renderer-default.pt', layer=IPyAMSLayer)
+class DefaultIllustrationRenderer(BaseIllustrationRenderer):
+    """Default illustration renderer"""
+
+    label = _("Centered illustration")
+    weight = 1
+
+
+@adapter_config(name='left+zoom', context=(IIllustration, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/renderer-left.pt', layer=IPyAMSLayer)
+class LeftIllustrationWithZoomRenderer(BaseIllustrationRenderer):
+    """Illustrtaion renderer with small image and zoom"""
+
+    label = _("Small illustration on the left with zoom")
+    weight = 2
+
+
+@adapter_config(name='right+zoom', context=(IIllustration, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/renderer-right.pt', layer=IPyAMSLayer)
+class RightIllustrationWithZoomRenderer(BaseIllustrationRenderer):
+    """Illustrtaion renderer with small image and zoom"""
+
+    label = _("Small illustration on the right with zoom")
+    weight = 3
+
+
+#
+# Illustration properties inner edit form
+#
+
+@adapter_config(name='illustration', context=(IIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
+                provides=IInnerSubForm)
+class IllustrationPropertiesInnerEditForm(InnerAdminEditForm):
+    """Illustration properties inner edit form"""
+
+    prefix = 'illustration_form.'
+
+    css_class = 'form-group'
+    padding_class = ''
+    fieldset_class = 'margin-top-10 padding-top-5 padding-bottom-5'
+
+    legend = _("Illustration")
+    legend_class = 'inner switcher padding-right-10 no-y-padding pull-left'
+
+    fields = field.Fields(IIllustration).omit('__parent__', '__name__')
+    weight = 10
+
+    def getContent(self):
+        return IIllustration(self.context)
+
+    def updateWidgets(self, prefix=None):
+        super(IllustrationPropertiesInnerEditForm, self).updateWidgets(prefix)
+        if 'description' in self.widgets:
+            self.widgets['description'].widget_css_class = 'textarea'
+
+    @property
+    def switcher_state(self):
+        for lang, data in self.getContent().data:
+            if data:
+                return 'open'
+
+    def get_ajax_output(self, changes):
+        output = super(IllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
+        if 'data' in changes.get(IIllustration, ()):
+            # we have to commit transaction to be able to handle blobs...
+            ITransactionManager(self.context).get().commit()
+            context = IIllustration(self.context)
+            form = IllustrationPropertiesInnerEditForm(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