src/pyams_content/component/paragraph/zmi/illustration.py
changeset 0 7c0001cacf8e
child 7 cbc55162b64e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/illustration.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,192 @@
+#
+# 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.
+#
+from pyramid.view import view_config
+from pyams_content.component.paragraph.illustration import Illustration
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IIllustrationParagraph, \
+    IParagraphContainer, IBaseParagraph, IParagraphSummary, IIllustrationRenderer
+from pyams_content.component.paragraph.zmi.container import ParagraphContainerView
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_form.form import AJAXAddForm, AJAXEditForm
+from pyams_form.security import ProtectedFormObjectMixin
+from pyams_i18n.interfaces import II18n
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_template.template import template_config, get_view_template
+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
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+
+# import packages
+from z3c.form import field
+
+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__')
+    ajax_handler = 'add-illustration.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def create(self, data):
+        return Illustration()
+
+    def add(self, object):
+        IParagraphContainer(self.context)['none'] = 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__')
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+
+@view_config(name='properties.json', context=IIllustrationParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class IllustrationPropertiesAJAXEditForm(AJAXEditForm, IllustrationPropertiesEditForm):
+    """HTML paragraph properties edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        if 'title' in changes.get(IBaseParagraph, ()):
+            return {'status': 'reload',
+                    'location': '#paragraphs.html'}
+        else:
+            return super(IllustrationPropertiesAJAXEditForm, self).get_ajax_output(changes)
+
+
+#
+# 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 ''
+
+
+#
+# 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('legend', self.language, request=self.request)
+        else:
+            self.legend = i18n.query_attribute('legend', request=self.request)
+
+    render = get_view_template()
+
+
+@adapter_config(name='default', context=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/illustration.pt', layer=IPyAMSLayer)
+class DefaultIllustrationRenderer(BaseIllustrationRenderer):
+    """Default illustration renderer"""
+
+    label = _("Centered illustration")
+    weight = 1
+
+
+@adapter_config(name='left+zoom', context=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/illustration-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=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer)
+@template_config(template='templates/illustration-right.pt', layer=IPyAMSLayer)
+class RightIllustrationWithZoomRenderer(BaseIllustrationRenderer):
+    """Illustrtaion renderer with small image and zoom"""
+
+    label = _("Small illustration on the right with zoom")
+    weight = 3