--- a/src/pyams_alchemy/zmi/engine.py Tue Mar 03 17:12:58 2015 +0100
+++ b/src/pyams_alchemy/zmi/engine.py Wed Mar 04 14:10:32 2015 +0100
@@ -9,6 +9,10 @@
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
+from z3c.table.column import GetAttrColumn
+from pyams_form.interfaces.form import IWidgetsSuffixViewletsManager
+from pyams_skin.table import BaseTable
+from pyams_template.template import template_config
__docformat__ = 'restructuredtext'
@@ -17,26 +21,28 @@
# import interfaces
from pyams_alchemy.interfaces import IAlchemyEngineUtility
-from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, ITableItemColumnActionsMenu
from pyams_skin.layer import IPyAMSLayer
from pyams_zmi.layer import IAdminLayer
from z3c.form.interfaces import IDataExtractedEvent, DISPLAY_MODE
from zope.component.interfaces import ISite
# import packages
-from pyams_alchemy.engine import PersistentAlchemyEngineUtility
+from pyams_alchemy.engine import PersistentAlchemyEngineUtility, get_user_session
from pyams_form.form import AJAXEditForm, AJAXAddForm
+from pyams_form.schema import CloseButton
from pyams_pagelet.pagelet import pagelet_config
from pyams_skin.viewlet.toolbar import ToolbarMenuItem
from pyams_utils.registry import query_utility
from pyams_utils.url import absolute_url
-from pyams_viewlet.viewlet import viewlet_config
+from pyams_viewlet.viewlet import viewlet_config, Viewlet
from pyams_zmi.control_panel import UtilitiesTable
from pyams_zmi.form import AdminDialogEditForm, AdminDialogAddForm
from pyramid.events import subscriber
from pyramid.view import view_config
-from z3c.form import field
-from zope.interface import Invalid
+from z3c.form import field, button
+from zope.interface import Interface, Invalid
+from zope.schema import Text
from pyams_alchemy import _
@@ -120,3 +126,120 @@
permission='system.manage', 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='system.manage')
+class AlchemyEngineTestMenu(ToolbarMenuItem):
+ """SQLAlchemy engine test menu"""
+
+ label = _("Test connection...")
+ url = 'test-sqlalchemy-engine.html'
+ modal_target = True
+ stop_propagation = True
+
+
+class IAlchemyEngineTestFields(Interface):
+ """SQLAlchemy engine test fields"""
+
+ query = Text(title=_("SQL query"),
+ required=True)
+
+
+class IAlchemyEngineTestButtons(Interface):
+ """SQLAlchemy engine test form buttons"""
+
+ close = CloseButton(name='close', title=_("Close"))
+ execute = button.Button(name='execute', title=_("Execute SQL"))
+
+
+@pagelet_config(name='test-sqlalchemy-engine.html', context=IAlchemyEngineUtility, layer=IPyAMSLayer,
+ permission='system.manage')
+class AlchemyEngineTestForm(AdminDialogAddForm):
+ """SQLAlchemy engine test form"""
+
+ @property
+ def title(self):
+ translate = self.request.localizer.translate
+ return translate(_("SQLAlchemy engine: {0}")).format(self.context.name)
+
+ legend = _("Test SQLAlchemy engine")
+ icon_css_class = 'fa fa-fw fa-database'
+
+ fields = field.Fields(IAlchemyEngineTestFields)
+ buttons = button.Buttons(IAlchemyEngineTestButtons)
+ ajax_handler = 'test-sqlalchemy-engine.json'
+ edit_permission = 'system.manage'
+
+ @property
+ def form_target(self):
+ return '#{0}_test_result'.format(self.id)
+
+ def updateWidgets(self, prefix=None):
+ super(AlchemyEngineTestForm, self).updateWidgets(prefix)
+ self.widgets['query'].label_css_class = 'textarea'
+
+ def updateActions(self):
+ super(AlchemyEngineTestForm, self).updateActions()
+ if 'execute' in self.actions:
+ self.actions['execute'].addClass('btn-primary')
+
+ def createAndAdd(self, data):
+ session = get_user_session(self.context.name)
+ return session.execute(data.get('query'))
+
+
+@viewlet_config(name='engine-test-suffix', layer=IAdminLayer, manager=IWidgetsSuffixViewletsManager,
+ view=AlchemyEngineTestForm, weight=50)
+@template_config(template='templates/engine-test.pt')
+class AlchemyEngineTestSuffix(Viewlet):
+ """SQLAlchemy engine test suffix"""
+
+
+class AlchemyEngineTestResults(BaseTable):
+ """Alchemy engine test results table"""
+
+ title = _("Query results")
+
+ sortOn = None
+ values = None
+
+ def __init__(self, context, request, values):
+ super(AlchemyEngineTestResults, self).__init__(context, request)
+ self.values = list(values)
+
+ @property
+ def data_attributes(self):
+ attrs = super(AlchemyEngineTestResults, self).data_attributes
+ attrs['table'] = {'data-ams-datatable-global-filter': 'false',
+ 'data-ams-datatable-pagination-size': 'false'}
+ return attrs
+
+ def initColumns(self):
+ if not self.values:
+ self.columns = None
+ else:
+ columns = []
+ row = self.values[0]
+ for col in row.keys():
+ column = GetAttrColumn(self.context, self.request, self)
+ column.__name__ = col
+ column.header = col
+ column.attrName = col
+ columns.append(column)
+ self.columns = columns
+
+
+@view_config(name='test-sqlalchemy-engine.json', context=IAlchemyEngineUtility, request_type=IPyAMSLayer,
+ permission='system.manage', 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}