From a64bd4ba52345d09b4e747e2f075011b2a701572 Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Fri, 18 Nov 2022 07:56:20 +0000 Subject: [PATCH] [FIX] base: add ir_config_parameter with default value Steps to reproduce: - install sale_management module; - go to settings of sale_management and check "Automatic invoice" parameter; - in debug mode, we can see the template selected (do not change it); - save the settings; (This is just one example) Issue: The parameter is not added to the "ir_config_parameter" table. Cause: The improvement which consists in comparing the settings which one wishes to record with those which already exist in order to set the parameters which are different creates a problem. Indeed, the current settings are retrieved directly from the model with their default value if there is one. These are the values that will be used for the comparison. Because of this, when we want to save a parameter which is set to its default value (on first save), the logic will not notice a difference. Therefore, the parameter will not be saved in the "ir_config_parameter" table. Consequence: In the code, when we wanted to look up the value of a parameter, if it is not saved, we use its default value instead of saving it with its default value. Example: ``` field_example = fields.Integer(string='Example', default=1000, config_parameter='model.field_example') value_field_example = params.get_param('model.field_example', default=1000) ``` The default value is repeated. Solution: To know whether or not we add a new parameter in the "ir_config_parameter" table, we must look at what already exists in the table and not in the settings defined in the model. opw-3057306 closes odoo/odoo#106188 X-original-commit: d881dfaaa163b409a94494e35afa2cbda7cf2aa2 Related: odoo/enterprise#34258 Signed-off-by: Victor Feyens (vfe) --- odoo/addons/base/models/res_config.py | 10 +++------- .../test_testing_utilities/ir.model.access.csv | 1 + odoo/addons/test_testing_utilities/models.py | 15 +++++++++++++++ .../test_testing_utilities/tests/__init__.py | 1 + .../tests/test_res_config.py | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 odoo/addons/test_testing_utilities/tests/test_res_config.py diff --git a/odoo/addons/base/models/res_config.py b/odoo/addons/base/models/res_config.py index 520af598eb1..9b9c76d2189 100644 --- a/odoo/addons/base/models/res_config.py +++ b/odoo/addons/base/models/res_config.py @@ -559,12 +559,8 @@ class ResConfigSettings(models.TransientModel, ResConfigModuleInstallationMixin) for name, icp in classified['config']: field = self._fields[name] value = self[name] - current_value = current_settings[name] - if not field.relational and value == current_value: - # pre-check before the value is formatted - # because the values in current_settings are - # in field format, not in str/False parameter format - continue + current_value = IrConfigParameter.get_param(icp) + if field.type == 'char': # storing developer keys as ir.config_parameter may lead to nasty # bugs when users leave spaces around them @@ -575,7 +571,7 @@ class ResConfigSettings(models.TransientModel, ResConfigModuleInstallationMixin) # value is a (possibly empty) recordset value = value.id - if current_value == value: + if current_value == str(value) or current_value == value: continue IrConfigParameter.set_param(icp, value) diff --git a/odoo/addons/test_testing_utilities/ir.model.access.csv b/odoo/addons/test_testing_utilities/ir.model.access.csv index 5f7c8a1777c..a8a46acb17a 100644 --- a/odoo/addons/test_testing_utilities/ir.model.access.csv +++ b/odoo/addons/test_testing_utilities/ir.model.access.csv @@ -27,3 +27,4 @@ access_ttu_product,access_ttu_product,model_ttu_product,base.group_user,1,0,0,0 access_ttu_root,access_ttu_root,model_ttu_root,base.group_user,1,0,0,0 access_ttu_child,access_ttu_child,model_ttu_child,base.group_user,1,0,0,0 access_ttu_grandchild,access_ttu_grandchild,model_ttu_grandchild,base.group_user,1,0,0,0 +access_res_config_test,access_res_config_test,test_testing_utilities.model_res_config_test,base.group_user,1,0,0,0 diff --git a/odoo/addons/test_testing_utilities/models.py b/odoo/addons/test_testing_utilities/models.py index e5777ab801b..2c6207d0335 100644 --- a/odoo/addons/test_testing_utilities/models.py +++ b/odoo/addons/test_testing_utilities/models.py @@ -314,3 +314,18 @@ class O2MChangesChildrenLines(models.Model): parent_id = fields.Many2one('o2m_changes_children') v = fields.Integer() vv = fields.Integer() + +class ResConfigTest(models.Model): + _inherit = 'res.config.settings' + + _name = 'res.config.test' + _description = 'Config test' + + param1 = fields.Integer( + string='Test parameter 1', + config_parameter='resConfigTest.parameter1', + default=1000) + + param2 = fields.Many2one( + 'res.config', + config_parameter="resConfigTest.parameter2") diff --git a/odoo/addons/test_testing_utilities/tests/__init__.py b/odoo/addons/test_testing_utilities/tests/__init__.py index 78398a30bce..ddbdacb243c 100644 --- a/odoo/addons/test_testing_utilities/tests/__init__.py +++ b/odoo/addons/test_testing_utilities/tests/__init__.py @@ -3,3 +3,4 @@ from . import test_methods from . import test_lxml from . import test_form_impl from . import test_xml_tools +from . import test_res_config diff --git a/odoo/addons/test_testing_utilities/tests/test_res_config.py b/odoo/addons/test_testing_utilities/tests/test_res_config.py new file mode 100644 index 00000000000..3cfaac18c47 --- /dev/null +++ b/odoo/addons/test_testing_utilities/tests/test_res_config.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from unittest.mock import patch +from odoo.tests.common import TransactionCase + +class TestResConfig(TransactionCase): + + def test_00_add_parameter_with_default_value(self): + """ Check if parameters with a default value are saved in the ir_config_parameter table """ + + self.env['res.config.test'].create({}).execute() + self.assertEqual(self.env['ir.config_parameter'].sudo().get_param('resConfigTest.parameter1'), str(1000), + "The parameter is not saved with its default value") + + with patch('odoo.addons.base.models.ir_config_parameter.IrConfigParameter.set_param') as set_param_mock: + self.env['res.config.test'].create({}).execute() + + set_param_mock.assert_not_called()