From aab1264c820a64b94822de0964d0a031273d14bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 10 Jul 2015 17:04:49 +0200 Subject: [PATCH] Feature #3748: Fix user settings Use user template append, fix page refresh after settings change --- .../models/OpenNebulaJSON/UserJSON.rb | 6 +- src/sunstone/public/app/opennebula/user.js | 4 + .../tabs/settings-tab/panels/user-config.js | 81 ++--------------- .../public/app/tabs/users-tab/actions.js | 46 ++++++++-- .../public/app/tabs/users-tab/panels/info.js | 91 ++----------------- .../public/app/utils/tab-datatable.js | 19 +--- src/sunstone/sunstone-server.rb | 2 +- 7 files changed, 64 insertions(+), 185 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/UserJSON.rb b/src/sunstone/models/OpenNebulaJSON/UserJSON.rb index fbedb4cc85..5df7a96c43 100644 --- a/src/sunstone/models/OpenNebulaJSON/UserJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/UserJSON.rb @@ -65,7 +65,11 @@ module OpenNebulaJSON end def update(params=Hash.new) - super(params['template_raw']) + if !params['append'].nil? + super(params['template_raw'], params['append']) + else + super(params['template_raw']) + end end def set_quota(params=Hash.new) diff --git a/src/sunstone/public/app/opennebula/user.js b/src/sunstone/public/app/opennebula/user.js index f7e7f9bdf1..267e9a9f60 100644 --- a/src/sunstone/public/app/opennebula/user.js +++ b/src/sunstone/public/app/opennebula/user.js @@ -72,6 +72,10 @@ define(function(require) { var action_obj = {"template_raw" : params.data.extra_param}; OpenNebulaAction.simple_action(params, RESOURCE, "update", action_obj); }, + "append": function(params) { + var action_obj = {"template_raw" : params.data.extra_param, append : true}; + OpenNebulaAction.simple_action(params, RESOURCE, "update", action_obj); + }, "fetch_template" : function(params) { OpenNebulaAction.show(params, RESOURCE, "template"); }, diff --git a/src/sunstone/public/app/tabs/settings-tab/panels/user-config.js b/src/sunstone/public/app/tabs/settings-tab/panels/user-config.js index 1e7d71be18..f940c372c7 100644 --- a/src/sunstone/public/app/tabs/settings-tab/panels/user-config.js +++ b/src/sunstone/public/app/tabs/settings-tab/panels/user-config.js @@ -7,6 +7,8 @@ define(function(require) { var Locale = require('utils/locale'); var Config = require('sunstone-config'); var OpenNebula = require('opennebula'); + var Sunstone = require('sunstone'); + var Notifier = require('utils/notifier'); /* TEMPLATES @@ -96,93 +98,26 @@ define(function(require) { return false; } - OpenNebula.User.show({ - data : { - id: "-1" - }, - success: function(request, user_json) { - var template = user_json.USER.TEMPLATE; + var template_str = 'SSH_PUBLIC_KEY = "'+keypair+'"'; - template["SSH_PUBLIC_KEY"] = keypair; + Sunstone.runAction("User.append_template", "-1", template_str); - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_template", "-1", template_str); - } - }) return false; }); $("#provision_change_view_form").submit(function() { - var view = $('#provision_user_views_select', this).val(); + var template_str = 'DEFAULT_VIEW = "'+$('#provision_user_views_select', this).val()+'"'; - OpenNebula.User.show({ - data : { - id: "-1" - }, - success: function(request, user_json) { - var template = user_json.USER.TEMPLATE; + Sunstone.runAction("User.append_template_refresh", "-1", template_str); - template["DEFAULT_VIEW"] = view; - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - var data = OpenNebula.Helper.action('update', {"template_raw" : template_str}); - - $.ajax({ - url: 'config', - type: "POST", - dataType: "json", - data: JSON.stringify(data), - success: function() { - window.location.href = "."; - }, - error: function(response) { - } - }); - } - }) return false; }); $("#provision_change_language_form").submit(function() { - var lang = $('#provision_new_language', this).val(); + var template_str = 'LANG = "'+$('#provision_new_language', this).val()+'"'; - OpenNebula.User.show({ - data : { - id: "-1" - }, - success: function(request, user_json) { - var template = user_json.USER.TEMPLATE; + Sunstone.runAction("User.append_template_refresh", "-1", template_str); - template["LANG"] = lang; - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - var data = OpenNebula.Helper.action('update', {"template_raw" : template_str}); - - $.ajax({ - url: 'config', - type: "POST", - dataType: "json", - data: JSON.stringify(data), - success: function() { - window.location.href = "."; - }, - error: function(response) { - } - }); - } - }) return false; }); diff --git a/src/sunstone/public/app/tabs/users-tab/actions.js b/src/sunstone/public/app/tabs/users-tab/actions.js index 8b48217c05..8937a09b56 100644 --- a/src/sunstone/public/app/tabs/users-tab/actions.js +++ b/src/sunstone/public/app/tabs/users-tab/actions.js @@ -152,6 +152,17 @@ define(function(require) { type: "single", call: OpenNebulaResource.update, callback: function(request) { + $.ajax({ + url: 'config', + type: "POST", + dataType: "json", + success: function() { + return false; + }, + error: function(response) { + } + }); + Sunstone.runAction(RESOURCE+'.show',request.request.data[0][0]); if (request.request.data[0][0] == config['user_id']) { Sunstone.runAction('Settings.refresh'); @@ -160,28 +171,43 @@ define(function(require) { error: Notifier.onError }, - "User.update_language" : { + "User.append_template" : { type: "single", - call: OpenNebulaResource.update, + call: OpenNebulaResource.append, callback: function(request) { + $.ajax({ + url: 'config', + type: "POST", + dataType: "json", + success: function() { + return false; + }, + error: function(response) { + } + }); + Sunstone.runAction(RESOURCE+'.show',request.request.data[0][0]); if (request.request.data[0][0] == config['user_id']) { Sunstone.runAction('Settings.refresh'); } - Notifier.notifyMessage(Locale.tr("The user must refresh the page for the change to take effect")); }, error: Notifier.onError }, - "User.update_view" : { + "User.append_template_refresh" : { type: "single", - call: OpenNebulaResource.update, + call: OpenNebulaResource.append, callback: function(request) { - Sunstone.runAction(RESOURCE+'.show',request.request.data[0][0]); - if (request.request.data[0][0] == config['user_id']) { - Sunstone.runAction('Settings.refresh'); - } - Notifier.notifyMessage(Locale.tr("The user must refresh the page for the change to take effect")); + $.ajax({ + url: 'config', + type: "POST", + dataType: "json", + success: function() { + window.location.href = "."; + }, + error: function(response) { + } + }); }, error: Notifier.onError }, diff --git a/src/sunstone/public/app/tabs/users-tab/panels/info.js b/src/sunstone/public/app/tabs/users-tab/panels/info.js index 1282eb5926..e3109e2aa8 100644 --- a/src/sunstone/public/app/tabs/users-tab/panels/info.js +++ b/src/sunstone/public/app/tabs/users-tab/panels/info.js @@ -146,27 +146,9 @@ define(function(require) { context.off("change", "#user_ssh_public_key_textarea"); context.on("change", "#user_ssh_public_key_textarea", function() { - var user_id = that.element.ID; + var template_str = 'SSH_PUBLIC_KEY = "'+$(this).val()+'"'; - // TODO: use update --append instead of a show + update - - OpenNebulaUser.show({ - data : { - id: user_id - }, - success: function(request, user_json) { - var template = that.element.TEMPLATE; - - template["SSH_PUBLIC_KEY"] = $("#user_ssh_public_key_textarea", context).val(); - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_template", user_id, template_str); - } - }); + Sunstone.runAction("User.append_template", that.element.ID, template_str); }); context.off("focusout", "#user_ssh_public_key_textarea"); @@ -190,24 +172,9 @@ define(function(require) { context.off("change", "#table_order_select") context.on("change", "#table_order_select", function() { - var user_id = that.element.ID; - OpenNebulaUser.show({ - data : { - id: user_id - }, - success: function(request, user_json) { - var template = that.element.TEMPLATE; + var template_str = 'TABLE_ORDER = "'+$(this).val()+'"'; - template["TABLE_ORDER"] = $("#table_order_select", context).val(); - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_template", user_id, template_str); - } - }); + Sunstone.runAction("User.append_template", that.element.ID, template_str); }); // Change language @@ -224,36 +191,9 @@ define(function(require) { context.off("change", "#language_select") context.on("change", "#language_select", function() { - var user_id = that.element.ID; - OpenNebulaUser.show({ - data : { - id: user_id - }, - success: function(request, user_json) { - var template = that.element.TEMPLATE; + var template_str = 'LANG = "'+$(this).val()+'"'; - template["LANG"] = $("#language_select", context).val(); - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_language", user_id, template_str); - - $.ajax({ - url: 'config', - type: "POST", - dataType: "json", - data: JSON.stringify(template_str), - success: function() { - window.location.href = "."; - }, - error: function(response) { - } - }); - } - }); + Sunstone.runAction("User.append_template_refresh", that.element.ID, template_str); }); // Change view @@ -275,24 +215,9 @@ define(function(require) { context.off("change", "#view_select") context.on("change", "#view_select", function() { - var user_id = that.element.ID; - OpenNebulaUser.show({ - data : { - id: user_id - }, - success: function(request, user_json) { - var template = that.element.TEMPLATE; + var template_str = 'DEFAULT_VIEW = "'+$(this).val()+'"'; - template["DEFAULT_VIEW"] = $("#view_select", context).val(); - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_view", user_id, template_str); - } - }); + Sunstone.runAction("User.append_template_refresh", that.element.ID, template_str); }); return false; diff --git a/src/sunstone/public/app/utils/tab-datatable.js b/src/sunstone/public/app/utils/tab-datatable.js index 24dc8b553f..2a8bb59416 100644 --- a/src/sunstone/public/app/utils/tab-datatable.js +++ b/src/sunstone/public/app/utils/tab-datatable.js @@ -162,26 +162,11 @@ define(function(require) { // Remember page length only for non selectable datatables if (!this.conf.select) { this.dataTable.on( 'length.dt', function ( e, settings, len ) { - config['page_length'] = len; - OpenNebulaUser.show({ - data : { - id: config['user_id'] - }, - success: function(request, response) { - var template = response.USER.TEMPLATE; + var template_str = 'TABLE_DEFAULT_PAGE_LENGTH = "'+len+'"'; - template["TABLE_DEFAULT_PAGE_LENGTH"] = len; - - template_str = ""; - $.each(template, function(key, value) { - template_str += (key + '=' + '"' + value + '"\n'); - }); - - Sunstone.runAction("User.update_template", config['user_id'], template_str); - } - }); + Sunstone.runAction("User.append_template", config['user_id'], template_str); }); } diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index a453fbe7e0..c77d0991e3 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -454,7 +454,7 @@ post '/config' do session[:table_order] = user['TEMPLATE/TABLE_ORDER'] if user['TEMPLATE/TABLE_ORDER'] session[:display_name] = user[DISPLAY_NAME_XPATH] || user['NAME'] - [200, ""] + [204, ""] end get '/vm/:id/log' do