From ac624eb7fc9641aa12dea3a2ee953835ab9e4fac Mon Sep 17 00:00:00 2001 From: KKamaa Date: Thu, 9 Feb 2023 01:26:44 +0300 Subject: [PATCH] [FIX] use key/value pair for support branding --- support_branding/models/res_company.py | 35 ++++++------- .../static/src/js/res_config_edition.js | 35 ++++++------- .../static/src/js/support_branding.js | 50 ++++++------------- support_branding/static/src/js/user_menu.js | 11 ++-- .../tests/test_support_branding.py | 41 ++++++--------- 5 files changed, 65 insertions(+), 107 deletions(-) diff --git a/support_branding/models/res_company.py b/support_branding/models/res_company.py index 670598e4c..ab7ecee42 100644 --- a/support_branding/models/res_company.py +++ b/support_branding/models/res_company.py @@ -2,30 +2,23 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import _, api, models -from odoo.exceptions import AccessError, UserError +from odoo.exceptions import UserError, AccessError class ResCompany(models.Model): _inherit = "res.company" @api.model - def get_ir_config_param_data(self, key): - if not self.env.user.has_group("base.group_user"): - raise AccessError( - _( - "You are not allowed to access this " - "functionality, please contact Admin for " - "more support" - ) - ) - try: - self.env.cr.execute( - "select value from ir_config_parameter where " "key=(%s);", (key,) - ) - res = self.env.cr.fetchone() - except Exception as e: - raise UserError(_("Error: %s" % e)) - else: - if res: - return "%s" % res - return "" + def get_support_branding_config_param_data(self): + if not self.env.user.has_group('base.group_user'): + raise AccessError(_('You are not allowed to access this ' + 'functionality, please contact Admin for ' + 'more support')) + self.env.cr.execute( + "select key, value from ir_config_parameter where key ilike " + "'%support_%';" + ) + res = self.env.cr.dictfetchall() + if any(res): + support_vals = {x['key']: x['value'] for x in res} + return support_vals diff --git a/support_branding/static/src/js/res_config_edition.js b/support_branding/static/src/js/res_config_edition.js index 0db5961cb..cabe19aa7 100644 --- a/support_branding/static/src/js/res_config_edition.js +++ b/support_branding/static/src/js/res_config_edition.js @@ -8,26 +8,23 @@ odoo.define("support_branding.ResConfigEdition", function (require) { var self = this; var def_1 = this._rpc({ model: "res.company", - method: "get_ir_config_param_data", - args: ["support_company"], - }).then(function (name) { - self.support_cp_name = name; + method: "get_support_branding_config_param_data", + args: [], + }).then(function (result) { + if (result && 'support_company' in result) + self.support_cp_name = result['support_company']; + if (result && 'support_company_url' in result) + self.support_cp_url = result['support_company_url']; + if (result && 'support_email' in result) + self.support_cp_email = result['support_email']; + if (result && 'support_release' in result) + self.support_cp_release = result['support_release']; + if (result && 'support_branding_color' in result) + self.support_branding_color = result[ + 'support_branding_color']; }); - var def_2 = this._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_company_url"], - }).then(function (url) { - self.support_cp_url = url; - }); - var def_3 = this._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_email"], - }).then(function (email) { - self.support_cp_email = email; - }); - return $.when(this._super.apply(this, arguments), def_1, def_2, def_3); + + return $.when(this._super.apply(this, arguments), def_1); }, }); }); diff --git a/support_branding/static/src/js/support_branding.js b/support_branding/static/src/js/support_branding.js index e86e5a572..702c73820 100644 --- a/support_branding/static/src/js/support_branding.js +++ b/support_branding/static/src/js/support_branding.js @@ -20,42 +20,20 @@ odoo.define("support_branding.CrashManager", function (require) { $.when(this._super.apply(this, arguments)).then(function () { self._rpc({ model: "res.company", - method: "get_ir_config_param_data", - args: ["support_company"], - }).then(function (name) { - self.support_cp_name = name; - }); - - self._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_company_url"], - }).then(function (url) { - self.support_cp_url = url; - }); - - self._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_email"], - }).then(function (email) { - self.support_cp_email = email; - }); - - self._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_release"], - }).then(function (release) { - self.support_cp_release = release; - }); - - self._rpc({ - model: "res.company", - method: "get_ir_config_param_data", - args: ["support_branding_color"], - }).then(function (color) { - self.support_cp_color = color; + method: "get_support_branding_config_param_data", + args: [], + }).then(function (result) { + if (result && 'support_company' in result) + self.support_cp_name = result['support_company']; + if (result && 'support_company_url' in result) + self.support_cp_url = result['support_company_url']; + if (result && 'support_email' in result) + self.support_cp_email = result['support_email']; + if (result && 'support_release' in result) + self.support_cp_release = result['support_release']; + if (result && 'support_branding_color' in result) + self.support_branding_color = result[ + 'support_branding_color']; }); }); }, diff --git a/support_branding/static/src/js/user_menu.js b/support_branding/static/src/js/user_menu.js index bee36e532..118d3834c 100644 --- a/support_branding/static/src/js/user_menu.js +++ b/support_branding/static/src/js/user_menu.js @@ -13,12 +13,13 @@ odoo.define("support_branding.UserMenu", function (require) { var def = self ._rpc({ model: "res.company", - method: "get_ir_config_param_data", - args: ["support_company_url"], + method: "get_support_branding_config_param_data", + args: [], }) - .then(function (site) { - if (site && site !== "") { - self.support_url = site; + .then(function (result) { + if (result && 'support_company_url' in result && + result['support_company_url'] !== "") { + self.support_url = result['support_company_url']; } }); return $.when(this._super.apply(this, arguments), def); diff --git a/support_branding/tests/test_support_branding.py b/support_branding/tests/test_support_branding.py index c836d596c..1cbd0ea7c 100644 --- a/support_branding/tests/test_support_branding.py +++ b/support_branding/tests/test_support_branding.py @@ -25,42 +25,31 @@ class TestSupportBranding(TransactionCase): # Check if user has the right access rights e.g. portal user not allowed with self.assertRaises(AccessError): self.ir_config_obj.with_user(self.portal_user).get_param( - self.demo_support_company_branding_url.key + self.demo_support_branding_company_name.key ) # Check if demo user is able to access. # NB: ir.config_parameter model requires admin access rights. with self.assertRaises(AccessError): self.ir_config_obj.with_user(self.demo_user).get_param( - self.demo_support_company_branding_url.key + self.demo_support_branding_company_name.key ) - value = self.company_obj.with_user(self.demo_user).get_ir_config_param_data( - self.demo_support_company_branding_url.key - ) + vals = self.company_obj.with_user( + self.demo_user).get_support_branding_config_param_data() - self.assertEquals(value, self.demo_support_company_branding_url.value) + self.assertEquals(vals['support_company'], + self.demo_support_branding_company_name.value) # Check if admin user is able to access # admin has access all through - value_1 = self.company_obj.with_user(self.admin_user).get_ir_config_param_data( - self.demo_support_company_branding_url.key - ) - value_2 = self.company_obj.with_user(self.admin_user).get_ir_config_param_data( - self.demo_support_company_branding_url.key - ) - self.assertEquals(value_1, value_2) - self.assertEquals(value_1, self.demo_support_company_branding_url.value) - self.assertEquals(value_2, self.demo_support_company_branding_url.value) + vals_1 = self.company_obj.with_user( + self.admin_user).get_support_branding_config_param_data() + vals_2 = self.company_obj.with_user( + self.admin_user).get_support_branding_config_param_data() + self.assertEquals(vals_1, vals_2) + self.assertEquals(vals_1['support_company_url'], + self.demo_support_company_branding_url.value) + self.assertEquals(vals_2['support_company_url'], + self.demo_support_company_branding_url.value) - # check if return if key is invalid - empty_val = self.company_obj.with_user( - self.admin_user - ).get_ir_config_param_data("testing") - self.assertEquals(empty_val, "") - - # check if return of key if invalid - with self.assertRaises(UserError): - empty_val = self.company_obj.with_user( - self.admin_user - ).get_ir_config_param_data(True)