diff -r 351dab28bb15 -r cde349864894 src/pyams_utils/zmi/zeo.py --- a/src/pyams_utils/zmi/zeo.py Thu Apr 21 17:14:48 2016 +0200 +++ b/src/pyams_utils/zmi/zeo.py Tue May 10 16:44:27 2016 +0200 @@ -14,10 +14,14 @@ # import standard library +import pprint +import sys +import traceback # import interfaces +from pyams_form.interfaces.form import IWidgetsSuffixViewletsManager from pyams_skin.interfaces.container import ITableElementName -from pyams_skin.interfaces.viewlet import IToolbarAddingMenu +from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, ITableItemColumnActionsMenu from pyams_skin.layer import IPyAMSLayer from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION from pyams_utils.interfaces.zeo import IZEOConnection @@ -27,19 +31,21 @@ # import packages from pyams_form.form import AJAXAddForm, AJAXEditForm +from pyams_form.schema import CloseButton from pyams_pagelet.pagelet import pagelet_config from pyams_skin.viewlet.toolbar import ToolbarMenuItem +from pyams_template.template import template_config from pyams_utils.adapter import adapter_config, ContextRequestAdapter from pyams_utils.registry import query_utility from pyams_utils.url import absolute_url from pyams_utils.zodb import ZEOConnectionUtility -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 AdminDialogAddForm, AdminDialogEditForm 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 pyams_utils import _ @@ -133,3 +139,93 @@ permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True) class ZEOConnectionPropertiesAJAXEditForm(AJAXEditForm, ZEOConnectionPropertiesEditForm): """ZEO connection properties edit form, AJAX view""" + + +# +# ZEO connection test form +# + +@viewlet_config(name='test-connection.menu', context=IZEOConnection, layer=IAdminLayer, + view=UtilitiesTable, manager=ITableItemColumnActionsMenu, permission=MANAGE_SYSTEM_PERMISSION) +class ZEOConnectionTestMenu(ToolbarMenuItem): + """ZEO connection test menu""" + + label = _("Test ZEO connection...") + label_css_class = 'fa fa-fw fa-database' + url = 'test-connection.html' + modal_target = True + stop_propagation = True + + +class IZEOConnectionTestButtons(Interface): + """ZEO connection test buttons""" + + close = CloseButton(name='close', title=_("Close")) + test = button.Button(name='test', title=_("Test connection")) + + +@pagelet_config(name='test-connection.html', context=IZEOConnection, layer=IPyAMSLayer, + permission=MANAGE_SYSTEM_PERMISSION) +class ZEOConnectionTestForm(AdminDialogAddForm): + """ZEO connection test form""" + + @property + def title(self): + translate = self.request.localizer.translate + return translate(_("ZEO connection: {0}")).format(self.context.name) + + legend = _("Test ZEO database connection") + icon_css_class = 'fa fa-fw fa-database' + + prefix = 'test_form.' + fields = field.Fields(Interface) + buttons = button.Buttons(IZEOConnectionTestButtons) + ajax_handler = 'test-connection.json' + edit_permission = MANAGE_SYSTEM_PERMISSION + + @property + def form_target(self): + return '#{0}_test_result'.format(self.id) + + def updateActions(self): + super(ZEOConnectionTestForm, self).updateActions() + if 'test' in self.actions: + self.actions['test'].addClass('btn-primary') + + def createAndAdd(self, data): + try: + self.context.get_connection() + except: + typ, value, tb = sys.exc_info() + return 500, (typ, value, tb) + else: + return 200, 'OK - ZEO connection successful' + + +@viewlet_config(name='test-connection.suffix', layer=IAdminLayer, manager=IWidgetsSuffixViewletsManager, + view=ZEOConnectionTestForm, weight=50) +@template_config(template='templates/connection-test.pt') +class ZEOConnectionTestFormSuffix(Viewlet): + """ZEO connection test form suffix""" + + +@view_config(name='test-connection.json', context=IZEOConnection, request_type=IPyAMSLayer, + permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True) +class ZEOConnectionAJAXTestForm(AJAXAddForm, ZEOConnectionTestForm): + """ZEO connection test form, JSON renderer""" + + def get_ajax_output(self, changes): + settings = '{0}\n\n'.format(pprint.pformat(self.context.get_settings())) + status, arguments = changes + if status == 200: + return {'status': 'success', + 'content': {'html': '{0}
{1}'.format(settings, arguments)}, + 'close_form': False} + else: + typ, value, tb = arguments + message = 'An error occurred: {type}
{message}'.format(type=typ, + message='
'.join( + traceback.format_exception(typ, value, tb))) + return {'status': 'info', + 'content': {'html': '{0}
{1}'.format(settings, message)}, + 'close_form': False}