Added "ajax_config" decorator to declare AJAX views
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Jun 2018 15:31:15 +0200
changeset 97 926d36ad8b8c
parent 96 43a99de860f2
child 98 c2bd8816b93d
Added "ajax_config" decorator to declare AJAX views
src/pyams_portal/zmi/container.py
src/pyams_portal/zmi/layout.py
src/pyams_portal/zmi/page.py
src/pyams_portal/zmi/template.py
--- a/src/pyams_portal/zmi/container.py	Mon Jun 11 15:30:26 2018 +0200
+++ b/src/pyams_portal/zmi/container.py	Mon Jun 11 15:31:15 2018 +0200
@@ -28,7 +28,7 @@
 from zope.component.interfaces import ISite
 
 # import packages
-from pyams_form.form import AJAXEditForm
+from pyams_form.form import ajax_config
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.container import ContainerView, delete_container_element
 from pyams_skin.page import DefaultPageHeaderAdapter
@@ -184,6 +184,7 @@
 
 @pagelet_config(name='properties.html', context=IPortalTemplateContainer, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='properties.json', context=IPortalTemplateContainer, layer=IPyAMSLayer)
 class PortalTemplateContainerPropertiesEditForm(AdminDialogEditForm):
     """Portal template container properties edit form"""
 
@@ -194,14 +195,7 @@
     icon_css_class = 'fa fa-fw fa-thumb-tack'
 
     fields = field.Fields(IPortalTemplateContainerConfiguration)
-    ajax_handler = 'properties.json'
     edit_permission = MANAGE_TEMPLATE_PERMISSION
 
     label_css_class = 'control-label col-md-4'
     input_css_class = 'col-md-8'
-
-
-@view_config(name='properties.json', context=IPortalTemplateContainer, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalTemplateContainerPropertiesAJAXEditForm(AJAXEditForm, PortalTemplateContainerPropertiesEditForm):
-    """Portal template container properties edit form, JSON renderer"""
--- a/src/pyams_portal/zmi/layout.py	Mon Jun 11 15:30:26 2018 +0200
+++ b/src/pyams_portal/zmi/layout.py	Mon Jun 11 15:31:15 2018 +0200
@@ -31,7 +31,7 @@
 from z3c.form.interfaces import IDataExtractedEvent, HIDDEN_MODE
 
 # import packages
-from pyams_form.form import AJAXAddForm, AJAXEditForm
+from pyams_form.form import AJAXAddForm, AJAXEditForm, ajax_config
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_portal.zmi.template import PortalTemplateHeaderAdapter
 from pyams_skin.viewlet.menu import MenuItem
@@ -127,10 +127,14 @@
                        for portlet_name in configuration.toolbar_portlets or ()])
 
     def get_portlet(self, name):
-        return self.request.registry.getUtility(IPortlet, name=name)
+        return self.request.registry.queryUtility(IPortlet, name=name)
 
     def get_portlet_label(self, name):
-        return self.request.localizer.translate(self.get_portlet(name).label)
+        portlet = self.get_portlet(name)
+        if portlet is not None:
+            return self.request.localizer.translate(portlet.label)
+        else:
+            return self.request.localizer.translate(_("{{ missing portlet }}"))
 
     def get_portlet_preview(self, portlet_id):
         settings = self.portlet_configuration.get_portlet_configuration(portlet_id).settings
@@ -211,6 +215,8 @@
 
 @pagelet_config(name='add-template-slot.html', context=IPortalTemplate, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='add-template-slot.json', context=IPortalTemplate, layer=IPyAMSLayer,
+             permission=MANAGE_TEMPLATE_PERMISSION, base=AJAXAddForm)
 class PortalTemplateSlotAddForm(AdminDialogAddForm):
     """Portal template slot add form"""
 
@@ -223,7 +229,6 @@
     icon_css_class = 'fa fa-fw fa-columns'
 
     fields = field.Fields(ISlot)
-    ajax_handler = 'add-template-slot.json'
     edit_permission = None
 
     def updateWidgets(self, prefix=None):
@@ -237,21 +242,6 @@
         config = IPortalTemplateConfiguration(self.context)
         return config.add_slot(data.get('name'), data.get('row_id'))
 
-
-@subscriber(IDataExtractedEvent, form_selector=PortalTemplateSlotAddForm)
-def handle_new_slot_data_extraction(event):
-    """Handle new slot form data extraction"""
-    config = IPortalTemplateConfiguration(event.form.context)
-    name = event.data.get('name')
-    if name in config.slot_names:
-        event.form.widgets.errors += (Invalid(_("Specified name is already used!")),)
-
-
-@view_config(name='add-template-slot.json', context=IPortalTemplate, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalTemplateSlotAJAXAddForm(AJAXAddForm, PortalTemplateSlotAddForm):
-    """Portal template slot add form, AJAX handler"""
-
     def get_ajax_output(self, changes):
         return {
             'status': 'callback',
@@ -263,6 +253,15 @@
         }
 
 
+@subscriber(IDataExtractedEvent, form_selector=PortalTemplateSlotAddForm)
+def handle_new_slot_data_extraction(event):
+    """Handle new slot form data extraction"""
+    config = IPortalTemplateConfiguration(event.form.context)
+    name = event.data.get('name')
+    if name in config.slot_names:
+        event.form.widgets.errors += (Invalid(_("Specified name is already used!")),)
+
+
 @view_config(name='set-template-slot-order.json', context=IPortalTemplate, request_type=IPyAMSLayer,
              permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
 def set_template_slot_order(request):
@@ -296,6 +295,7 @@
 
 @pagelet_config(name='slot-properties.html', context=IPortalTemplate, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='slot-properties.json', context=IPortalTemplate, layer=IPyAMSLayer)
 class PortalTemplateSlotPropertiesEditForm(AdminDialogEditForm):
     """Slot properties edit form"""
 
@@ -313,7 +313,6 @@
     label_css_class = 'control-label col-md-5'
     input_css_class = 'col-md-7'
 
-    ajax_handler = 'slot-properties.json'
     edit_permission = MANAGE_TEMPLATE_PERMISSION
 
     def __init__(self, context, request):
@@ -328,12 +327,6 @@
         super(PortalTemplateSlotPropertiesEditForm, self).updateWidgets(prefix)
         self.widgets['slot_name'].mode = HIDDEN_MODE
 
-
-@view_config(name='slot-properties.json', context=IPortalTemplate, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalTemplateSlotPropertiesAJAXEditForm(AJAXEditForm, PortalTemplateSlotPropertiesEditForm):
-    """Slot properties edit form, AJAX renderer"""
-
     def get_ajax_output(self, changes):
         if changes:
             slot_name = self.widgets['slot_name'].value
@@ -347,7 +340,7 @@
                 }
             }
         else:
-            return super(PortalTemplateSlotPropertiesAJAXEditForm, self).get_ajax_output(changes)
+            return super(self.__class__, self).get_ajax_output(changes)
 
 
 @view_config(name='delete-template-slot.json', context=IPortalTemplate, request_type=IPyAMSLayer,
@@ -384,6 +377,8 @@
 
 @pagelet_config(name='add-template-portlet.html', context=IPortalTemplate, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='add-template-portlet.json', context=IPortalTemplate, layer=IPyAMSLayer,
+             permission=MANAGE_TEMPLATE_PERMISSION, base=AJAXAddForm)
 class PortalTemplatePortletAddForm(AdminDialogAddForm):
     """Portal template portlet add form"""
 
@@ -396,7 +391,6 @@
     icon_css_class = 'fa fa-fw fa-columns'
 
     fields = field.Fields(IPortletAddingInfo)
-    ajax_handler = 'add-template-portlet.json'
     edit_permission = None
 
     def createAndAdd(self, data):
@@ -404,12 +398,6 @@
         config = IPortalTemplateConfiguration(self.context)
         return config.add_portlet(data.get('portlet_name'), data.get('slot_name'))
 
-
-@view_config(name='add-template-portlet.json', context=IPortalTemplate, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalTemplatePortletAJAXAddForm(AJAXAddForm, PortalTemplatePortletAddForm):
-    """Portal template portlet add form, AJAX handler"""
-
     def get_ajax_output(self, changes):
         configuration = IPortalPortletsConfiguration(self.context)
         settings = configuration.get_portlet_configuration(changes['portlet_id']).settings
--- a/src/pyams_portal/zmi/page.py	Mon Jun 11 15:30:26 2018 +0200
+++ b/src/pyams_portal/zmi/page.py	Mon Jun 11 15:31:15 2018 +0200
@@ -28,8 +28,7 @@
 from z3c.form.interfaces import IDataExtractedEvent
 
 # import packages
-from pyramid.view import view_config
-from pyams_form.form import AJAXEditForm
+from pyams_form.form import ajax_config
 from pyams_form.group import NamedWidgetsGroup, FormWidgetsGroup
 from pyams_form.help import FormHelp
 from pyams_pagelet.pagelet import pagelet_config
@@ -44,6 +43,7 @@
 from pyams_zmi.form import AdminEditForm, InnerAdminEditForm
 from pyams_zmi.zmi.site import PropertiesEditFormHeaderAdapter
 from pyramid.events import subscriber
+from pyramid.view import view_config
 from z3c.form import field
 from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
 from zope.interface import implementer, Interface, Invalid
@@ -112,6 +112,7 @@
 
 @pagelet_config(name='template-properties.html', context=IPortalContext, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='template-properties.json', context=IPortalContext, layer=IPyAMSLayer)
 @implementer(IWidgetForm, IInnerPage)
 class PortalContextTemplatePropertiesEditForm(AdminEditForm):
     """Portal context template properties edit form"""
@@ -132,7 +133,6 @@
             fields['use_shared_template'].widgetFactory = SingleCheckBoxFieldWidget
         return fields
 
-    ajax_handler = 'template-properties.json'
     edit_permission = MANAGE_TEMPLATE_PERMISSION
 
     def getContent(self):
@@ -160,6 +160,18 @@
         self.add_group(group)
         super(PortalContextTemplatePropertiesEditForm, self).updateGroups()
 
+    def get_ajax_output(self, changes):
+        output = super(self.__class__, self).get_ajax_output(changes)
+        if changes:
+            output.update({
+                'status': 'redirect',
+                'smallbox': {
+                    'status': 'success',
+                    'message': self.request.localizer.translate(self.successMessage)
+                }
+            })
+        return output
+
 
 @subscriber(IDataExtractedEvent, form_selector=PortalContextTemplatePropertiesEditForm)
 def handle_template_proterties_extract_event(event):
@@ -171,21 +183,6 @@
             form.widgets.errors += (Invalid(_("You must choose to use a local template or select a shared one!")),)
 
 
-@view_config(name='template-properties.json', context=IPortalContext, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalContextTemplatePropertiesAJAXEditForm(AJAXEditForm, PortalContextTemplatePropertiesEditForm):
-    """Portal context template properties edit form, JSON renderer"""
-
-    def get_ajax_output(self, changes):
-        output = super(PortalContextTemplatePropertiesAJAXEditForm, self).get_ajax_output(changes)
-        if changes:
-            output.update({
-                'status': 'redirect',
-                'message': self.request.localizer.translate(self.successMessage)
-            })
-        return output
-
-
 @adapter_config(context=(Interface, IPyAMSLayer, PortalContextTemplatePropertiesEditForm), provides=IPageHeader)
 class PortalContextPropertiesEditFormHeaderAdapter(PropertiesEditFormHeaderAdapter):
     """Portal context template properties edit form header adapter"""
--- a/src/pyams_portal/zmi/template.py	Mon Jun 11 15:30:26 2018 +0200
+++ b/src/pyams_portal/zmi/template.py	Mon Jun 11 15:31:15 2018 +0200
@@ -19,19 +19,19 @@
 from pyams_portal.interfaces import IPortalTemplateContainer, IPortalTemplate, IPortalContext, \
     MANAGE_TEMPLATE_PERMISSION
 from pyams_skin.interfaces import IPageHeader, IContentTitle
-from pyams_skin.interfaces.viewlet import IWidgetTitleViewletManager
+from pyams_skin.interfaces.viewlet import IWidgetTitleViewletManager, IBreadcrumbItem
 from pyams_skin.layer import IPyAMSLayer
 from pyams_zmi.layer import IAdminLayer
 from z3c.form.interfaces import IDataExtractedEvent
 from zope.component.interfaces import ISite
 
 # import packages
-from pyams_form.form import AJAXAddForm
+from pyams_form.form import AJAXAddForm, ajax_config
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_portal.template import PortalTemplate
 from pyams_portal.zmi.container import PortalTemplateContainerTable
-from pyams_skin.container import delete_container_element
 from pyams_skin.page import DefaultPageHeaderAdapter
+from pyams_skin.viewlet.breadcrumb import BreadcrumbItem
 from pyams_skin.viewlet.toolbar import ToolbarAction
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
@@ -41,13 +41,19 @@
 from pyams_viewlet.viewlet import viewlet_config
 from pyams_zmi.form import AdminDialogAddForm
 from pyramid.events import subscriber
-from pyramid.view import view_config
 from z3c.form import field
 from zope.interface import Interface, Invalid
 
 from pyams_portal import _
 
 
+@adapter_config(context=(IPortalTemplate, IPyAMSLayer), provides=IBreadcrumbItem)
+class PortalTemplateBreadcrumbAdapter(BreadcrumbItem):
+    """Portal template breadcrumb adapter"""
+
+    label = _("Portal template")
+
+
 @adapter_config(context=(IPortalTemplate, IPyAMSLayer, Interface), provides=IPageHeader)
 class PortalTemplateHeaderAdapter(DefaultPageHeaderAdapter):
     """Portal template header adapter"""
@@ -88,6 +94,8 @@
 
 @pagelet_config(name='add-portal-template.html', context=ISite, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@ajax_config(name='add-portal-template.json', context=ISite, layer=IPyAMSLayer,
+             permission=MANAGE_TEMPLATE_PERMISSION, base=AJAXAddForm)
 class PortalTemplateAddForm(AdminDialogAddForm):
     """Portal template add form"""
 
@@ -117,9 +125,3 @@
     name = event.data.get('name')
     if name in container:
         event.form.widgets.errors += (Invalid(_("Specified name is already used!")),)
-
-
-@view_config(name='add-portal-template.json', context=ISite, request_type=IPyAMSLayer,
-             permission=MANAGE_TEMPLATE_PERMISSION, renderer='json', xhr=True)
-class PortalTemplateAJAXAddForm(AJAXAddForm, PortalTemplateAddForm):
-    """Portal template add form, AJAX handler"""