From e071a1afbc032de3001d1508c096e7a22a227de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez?= Date: Fri, 6 Dec 2013 03:23:08 +0000 Subject: [PATCH] Advancing on test buttons --- server/src/uds/REST/model.py | 15 +++++++ server/src/uds/static/adm/js/api.js | 25 ++++++++++- .../src/uds/static/adm/js/gui-definition.js | 41 +++++++++---------- server/src/uds/static/adm/js/gui.js | 37 +++++++++++++---- 4 files changed, 88 insertions(+), 30 deletions(-) diff --git a/server/src/uds/REST/model.py b/server/src/uds/REST/model.py index f79039cc..4307cc65 100644 --- a/server/src/uds/REST/model.py +++ b/server/src/uds/REST/model.py @@ -229,6 +229,12 @@ class DetailHandler(BaseModelHandler): return self.saveItem(parent, item) + def post(self): + ''' + Post will be used for, for example, testing + ''' + raise NotFound('TODO: do it :-)') + def delete(self): ''' Put is delegated to specific implementation @@ -404,6 +410,15 @@ class ModelHandler(BaseModelHandler): return self.processDetail() raise RequestError('invalid request') + + def post(self): + # right now + logger.debug('method POST for {0}, {1}'.format(self.__class__.__name__, self._args)) + if len(self._args) == 2: + if self._args[0] == 'test': + return 'tested' + + raise NotFound('Method not found') def put(self): logger.debug('method PUT for {0}, {1}'.format(self.__class__.__name__, self._args)) diff --git a/server/src/uds/static/adm/js/api.js b/server/src/uds/static/adm/js/api.js index f2e87a55..68e0068c 100644 --- a/server/src/uds/static/adm/js/api.js +++ b/server/src/uds/static/adm/js/api.js @@ -54,7 +54,7 @@ api.doLog('Ajax GET Json for "' + url + '"'); $.ajax({ url : url, - type : "GET", + type : options.method || "GET", // Will use GET if no method provided dataType : "json", success : function(data) { api.doLog('Success on GET "' + url + '".'); @@ -80,7 +80,7 @@ api.doLog('Ajax PUT Json for "' + url + '"'); $.ajax({ url : url, - type : "PUT", + type : options.method || "PUT", // Will use PUT if no method provided dataType : "json", data: JSON.stringify(data), success: function(data) { @@ -170,6 +170,7 @@ function BasicModelRest(path, options) { this.path = path; this.getPath = options.getPath || path; this.putPath = options.putPath || path; + this.testPath = options.testPath || (path + '/test'); this.delPath = options.delPath || path; this.typesPath = options.typesPath || (path + '/types'); this.guiPath = options.guiPath || (path + '/gui'); @@ -284,6 +285,20 @@ BasicModelRest.prototype = { }); }, + // Testing + test: function(type, data, success_fnc, fail_fnc) { + "use strict"; + + var path = this.testPath + '/' + type; + + api.putJson(path, data, { + success: success_fnc, + fail: fail_fnc, + method: 'POST' + }); + }, + + // -------------- // Delete // -------------- @@ -380,6 +395,12 @@ DetailModelRestApi.prototype = { fail: fail_fnc }); }, + // Testing + test: function(type, data, success_fnc, fail_fnc) { + "use strict"; + + return this.base.test(type, data, success_fnc, fail_fnc); + }, // -------------- // Delete // -------------- diff --git a/server/src/uds/static/adm/js/gui-definition.js b/server/src/uds/static/adm/js/gui-definition.js index 8d7fc79d..b19edb55 100644 --- a/server/src/uds/static/adm/js/gui-definition.js +++ b/server/src/uds/static/adm/js/gui-definition.js @@ -29,7 +29,15 @@ gui.dashboard.link = function(event) { gui.providers = new GuiElement(api.providers, 'provi'); gui.providers.link = function(event) { "use strict"; - + + // Button definition to trigger "Test" action + var testButton = { + testButton: { + text: gettext('Test provider'), + css: 'btn-info', + }, + }; + api.templates.get('providers', function(tmpl) { gui.clearWorkspace(); gui.appendToWorkspace(api.templates.evaluate(tmpl, { @@ -83,8 +91,8 @@ gui.providers.link = function(event) { return false; }, buttons : [ 'new', 'edit', 'delete', 'xls' ], - onNew : gui.methods.typedNew(gui.providers, gettext('New provider'), gettext('Error creating provider')), - onEdit: gui.methods.typedEdit(gui.providers, gettext('Edit provider'), gettext('Error processing provider')), + onNew : gui.methods.typedNew(gui.providers, gettext('New provider'), gettext('Error creating provider'), testButton), + onEdit: gui.methods.typedEdit(gui.providers, gettext('Edit provider'), gettext('Error processing provider'), testButton), onDelete: gui.methods.del(gui.providers, gettext('Delete provider'), gettext('Error deleting provider')), }); }); @@ -99,10 +107,15 @@ gui.authenticators = new GuiElement(api.authenticators, 'auth'); gui.authenticators.link = function(event) { "use strict"; - // Cleans up memory used by other datatables - $.each($.fn.dataTable.fnTables(), function(undefined, tbl){ - $(tbl).dataTable().fnDestroy(); - }); + + // Button definition to trigger "Test" action + var testButton = { + testButton: { + text: gettext('Test authenticator'), + css: 'btn-info', + }, + }; + gui.doLog('enter auths'); api.templates.get('authenticators', function(tmpl) { gui.clearWorkspace(); @@ -113,20 +126,6 @@ gui.authenticators.link = function(event) { })); gui.setLinksEvents(); - - // Button definition to trigger "Test" action - var testButton = { - buttons: [ - { - text: gettext('Test authenticator'), - css: 'btn-info', - action: function(event, form_selector, closeFnc) { - var fields = gui.forms.read(form_selector); - } - }, - ] - }; - gui.authenticators.table({ container : 'auths-placeholder', rowSelect : 'single', diff --git a/server/src/uds/static/adm/js/gui.js b/server/src/uds/static/adm/js/gui.js index da356a4e..baaa34ff 100644 --- a/server/src/uds/static/adm/js/gui.js +++ b/server/src/uds/static/adm/js/gui.js @@ -247,24 +247,43 @@ gui.methods = {}; + gui.methods.typedTestButton = function(rest, text, css, type) { + return [ + { + text: text, + css: css, + action: function(event, form_selector, closeFnc) { + var fields = gui.forms.read(form_selector); + gui.doLog('Fields: ', fields); + rest.test(type, fields, function(data){ + gui.launchModal(gettext('Test result'), data, { actionButton: ' '}); + }, gui.failRequestModalFnc(gettext('Test error'))) + } + }, + ]; + }; + // "Generic" edit method to set onEdit table gui.methods.typedEdit = function(parent, modalTitle, modalErrorMsg, options) { - options = options || {} - var self = parent; + options = options || {}; return function(value, event, table, refreshFnc) { - self.rest.gui(value.type, function(guiDefinition) { + parent.rest.gui(value.type, function(guiDefinition) { + var buttons; + if( options.testButton ) { + buttons = gui.methods.typedTestButton(parent.rest, options.testButton.text, options.testButton.css, value.type); + } var tabs = options.guiProcessor ? options.guiProcessor(guiDefinition) : guiDefinition; // Preprocess fields (probably generate tabs...) - self.rest.item(value.id, function(item) { + parent.rest.item(value.id, function(item) { gui.forms.launchModal({ title: modalTitle+' '+value.name+'', fields: tabs, item: item, - buttons: options.buttons, + buttons: buttons, success: function(form_selector, closeFnc) { var fields = gui.forms.read(form_selector); fields.data_type = value.type; fields = options.fieldsProcessor ? options.fieldsProcessor(fields) : fields; - self.rest.save(fields, function(data) { // Success on put + parent.rest.save(fields, function(data) { // Success on put closeFnc(); refreshFnc(); gui.alert(gettext('Edition successfully done'), 'success'); @@ -282,12 +301,16 @@ var self = parent; return function(type, table, refreshFnc) { self.rest.gui(type, function(guiDefinition) { + var buttons; + if( options.testButton ) { + buttons = gui.methods.typedTestButton(parent.rest, options.testButton.text, options.testButton.css, type); + } var tabs = options.guiProcessor ? options.guiProcessor(guiDefinition) : guiDefinition; // Preprocess fields (probably generate tabs...) gui.forms.launchModal({ title: modalTitle, fields: tabs, item: undefined, - buttons: options.buttons, + buttons: buttons, success: function(form_selector, closeFnc) { var fields = gui.forms.read(form_selector); fields.data_type = type;