Declare AJAX views with "ajax_config" decorator
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Jun 2018 14:41:05 +0200
changeset 56 aa31cf1c5bf6
parent 55 449e83e485be
child 57 2399139252a9
Declare AJAX views with "ajax_config" decorator
src/pyams_alchemy/zmi/engine.py
--- a/src/pyams_alchemy/zmi/engine.py	Mon Jun 11 14:40:43 2018 +0200
+++ b/src/pyams_alchemy/zmi/engine.py	Mon Jun 11 14:41:05 2018 +0200
@@ -27,7 +27,7 @@
 
 # import packages
 from pyams_alchemy.engine import PersistentAlchemyEngineUtility, get_user_session
-from pyams_form.form import AJAXEditForm, AJAXAddForm
+from pyams_form.form import AJAXAddForm, ajax_config
 from pyams_form.schema import CloseButton
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.table import BaseTable
@@ -39,7 +39,6 @@
 from pyams_zmi.form import AdminDialogEditForm, AdminDialogAddForm
 from pyams_zmi.zmi.control_panel import UtilitiesTable
 from pyramid.events import subscriber
-from pyramid.view import view_config
 from z3c.form import field, button
 from z3c.table.column import GetAttrColumn
 from zope.interface import Interface, Invalid
@@ -60,6 +59,8 @@
 
 
 @pagelet_config(name='add-sqlalchemy-engine.html', context=ISite, layer=IPyAMSLayer, permission=MANAGE_SYSTEM_PERMISSION)
+@ajax_config(name='add-sqlalchemy-engine.json', context=ISite, layer=IPyAMSLayer, permission=MANAGE_SYSTEM_PERMISSION,
+             base=AJAXAddForm)
 class AlchemyEngineAddForm(AdminDialogAddForm):
     """SQLAlchemy engine add form"""
 
@@ -68,7 +69,6 @@
     icon_css_class = 'fa fa-fw fa-database'
 
     fields = field.Fields(IAlchemyEngineUtility)
-    ajax_handler = 'add-sqlalchemy-engine.json'
     edit_permission = None
 
     def create(self, data):
@@ -95,14 +95,9 @@
         event.form.widgets.errors += (Invalid(_("An SQLAlchemy engine is already registered with this name!")), )
 
 
-@view_config(name='add-sqlalchemy-engine.json', context=ISite, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class AlchemyEngineAJAXAddForm(AJAXAddForm, AlchemyEngineAddForm):
-    """SQLAlchemy engine add form, AJAX view"""
-
-
 @pagelet_config(name='properties.html', context=IAlchemyEngineUtility, layer=IPyAMSLayer,
                 permission=VIEW_SYSTEM_PERMISSION)
+@ajax_config(name='properties.json', context=IAlchemyEngineUtility, layer=IPyAMSLayer)
 class AlchemyEnginePropertiesEditForm(AdminDialogEditForm):
     """SQLAlchemy engine properties edit form"""
 
@@ -117,7 +112,6 @@
     icon_css_class = 'fa fa-fw fa-database'
 
     fields = field.Fields(IAlchemyEngineUtility)
-    ajax_handler = 'properties.json'
     edit_permission = MANAGE_SYSTEM_PERMISSION
 
     def updateWidgets(self, prefix=None):
@@ -126,12 +120,6 @@
         self.widgets['encoding'].addClass('select2')
 
 
-@view_config(name='properties.json', context=IAlchemyEngineUtility, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class AlchemyEnginePropertiesAJAXEditForm(AJAXEditForm, AlchemyEnginePropertiesEditForm):
-    """SQLAlchemy engine properties edit form, AJAX view"""
-
-
 @viewlet_config(name='test-engine.menu', context=IAlchemyEngineUtility, layer=IAdminLayer,
                 view=UtilitiesTable, manager=ITableItemColumnActionsMenu, permission=MANAGE_SYSTEM_PERMISSION)
 class AlchemyEngineTestMenu(ToolbarMenuItem):
@@ -160,6 +148,7 @@
 
 @pagelet_config(name='test-sqlalchemy-engine.html', context=IAlchemyEngineUtility, layer=IPyAMSLayer,
                 permission=MANAGE_SYSTEM_PERMISSION)
+@ajax_config(name='test-sqlalchemy-engine.json', context=IAlchemyEngineUtility, layer=IPyAMSLayer, base=AJAXAddForm)
 class AlchemyEngineTestForm(AdminDialogAddForm):
     """SQLAlchemy engine test form"""
 
@@ -173,7 +162,6 @@
 
     fields = field.Fields(IAlchemyEngineTestFields)
     buttons = button.Buttons(IAlchemyEngineTestButtons)
-    ajax_handler = 'test-sqlalchemy-engine.json'
     edit_permission = MANAGE_SYSTEM_PERMISSION
 
     @property
@@ -195,6 +183,17 @@
         session = get_user_session(self.context.name)
         return session.execute(data.get('query'))
 
+    def get_ajax_output(self, changes):
+        result = AlchemyEngineTestResults(self.context, self.request, changes)
+        result.update()
+        return {
+            'status': 'success',
+            'content': {
+                'html': result.render()
+            },
+            'close_form': False
+        }
+
 
 @viewlet_config(name='engine-test-suffix', layer=IAdminLayer, manager=IWidgetsSuffixViewletsManager,
                 view=AlchemyEngineTestForm, weight=50)
@@ -237,20 +236,3 @@
                 column.attrName = col
                 columns.append(column)
             self.columns = columns
-
-
-@view_config(name='test-sqlalchemy-engine.json', context=IAlchemyEngineUtility, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class AlchemyEngineAJAXTestForm(AJAXAddForm, AlchemyEngineTestForm):
-    """Alchemy engine test form, AJAX view"""
-
-    def get_ajax_output(self, changes):
-        result = AlchemyEngineTestResults(self.context, self.request, changes)
-        result.update()
-        return {
-            'status': 'success',
-            'content': {
-                'html': result.render()
-            },
-            'close_form': False
-        }