diff --git a/server_environment_ir_config_parameter/__init__.py b/server_environment_ir_config_parameter/__init__.py index 0650744..cf33a41 100644 --- a/server_environment_ir_config_parameter/__init__.py +++ b/server_environment_ir_config_parameter/__init__.py @@ -1 +1,2 @@ from . import models +from .hook import post_init_keep_parameter_value diff --git a/server_environment_ir_config_parameter/__manifest__.py b/server_environment_ir_config_parameter/__manifest__.py index 3dbb8a0..1653d67 100644 --- a/server_environment_ir_config_parameter/__manifest__.py +++ b/server_environment_ir_config_parameter/__manifest__.py @@ -10,7 +10,6 @@ "author": "ACSONE SA/NV, Odoo Community Association (OCA)", "website": "https://github.com/OCA/server-env", "depends": ["server_environment"], - "data": [ - "views/view_ir_config_parameter.xml", - ], + "post_init_hook": "post_init_keep_parameter_value", + "data": [], } diff --git a/server_environment_ir_config_parameter/hook.py b/server_environment_ir_config_parameter/hook.py new file mode 100644 index 0000000..d80c611 --- /dev/null +++ b/server_environment_ir_config_parameter/hook.py @@ -0,0 +1,13 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import SUPERUSER_ID, api + + +def post_init_keep_parameter_value(cr, registry): + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + env.cr.execute("""SELECT id, value FROM ir_config_parameter""") + result = env.cr.fetchall() + for config_id, value in result: + env["ir.config_parameter"].browse(config_id).write({"value": value}) + return True diff --git a/server_environment_ir_config_parameter/models/ir_config_parameter.py b/server_environment_ir_config_parameter/models/ir_config_parameter.py index 506a0ed..2d5fe3a 100644 --- a/server_environment_ir_config_parameter/models/ir_config_parameter.py +++ b/server_environment_ir_config_parameter/models/ir_config_parameter.py @@ -1,7 +1,7 @@ # Copyright 2016-2018 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import _, api, fields, models +from odoo import _, api, models from odoo.exceptions import UserError from odoo.addons.server_environment.server_env import serv_config @@ -10,26 +10,34 @@ SECTION = "ir.config_parameter" class IrConfigParameter(models.Model): + _name = "ir.config_parameter" + _inherit = ["ir.config_parameter", "server.env.mixin"] - _inherit = "ir.config_parameter" + _server_env_section_name_field = "key" - is_environment = fields.Boolean( - string="Defined by environment", - compute="_compute_is_environment", - help="If check, the value in the database will be ignored" - " and alternatively, the system will use the key defined" - " in your odoo.cfg environment file.", - ) + @property + def _server_env_fields(self): + base_fields = super()._server_env_fields + parameter_fields = { + "value": {}, + } + parameter_fields.update(base_fields) + return parameter_fields - def _compute_is_environment(self): - for parameter in self: - parameter.is_environment = serv_config.has_option(SECTION, parameter.key) + @api.model + def _server_env_global_section_name(self): + """Name of the global section in the configuration files + + Can be customized in your model + """ + return SECTION @api.model def get_param(self, key, default=False): value = super().get_param(key, default=None) - if serv_config.has_option(SECTION, key): - cvalue = serv_config.get(SECTION, key) + section = "%s.%s" % (SECTION, key) + if serv_config.has_option(section, "value"): + cvalue = serv_config.get(section, "value") if not cvalue: raise UserError( _("Key %s is empty in " "server_environment_file") % (key,) @@ -47,19 +55,24 @@ class IrConfigParameter(models.Model): @api.model def create(self, vals): - key = vals.get("key") - if serv_config.has_option(SECTION, key): - # enforce value from config file - vals = dict(vals, value=serv_config.get(SECTION, key)) - return super().create(vals) + record = super().create(vals) + # in case of creation of a param which is in config file but with another value + # the value is in cache after creation and then a get_param will give back the + # cache value instead of reading the field from the config... + # so if there is a config value, we clean the cache so it will be taken into + # account of the next read + if record._server_env_has_key_defined("value"): + record.invalidate_cache(fnames=["value"], ids=record.ids) + return record def write(self, vals): - for rec in self: - key = vals.get("key") or rec.key - if serv_config.has_option(SECTION, key): - # enforce value from config file - newvals = dict(vals, value=serv_config.get(SECTION, key)) - else: - newvals = vals - super().write(newvals) - return True + res = super().write(vals) + # in case of creation of a param which is in config file but with another value + # the value is in cache after creation and then a get_param will give back the + # cache value instead of reading the field from the config... + # so if there is a config value, we clean the cache so it will be taken into + # account of the next read + for record in self: + if record._server_env_has_key_defined("value"): + record.invalidate_cache(fnames=["value"], ids=record.ids) + return res diff --git a/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py b/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py index ba5bb1b..31f0ba8 100644 --- a/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py +++ b/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py @@ -15,7 +15,10 @@ class TestEnv(ServerEnvironmentCase): super().setUp() self.ICP = self.env["ir.config_parameter"] self.env_config = ( - "[ir.config_parameter]\n" "ircp_from_config=config_value\n" "ircp_empty=\n" + "[ir.config_parameter.ircp_from_config]\n" + "value=config_value\n\n" + "[ir.config_parameter.ircp_empty]\n" + "value=\n\n" ) def _load_xml(self, module, filepath): @@ -49,7 +52,7 @@ class TestEnv(ServerEnvironmentCase): """We can't set parameters that are in config file""" with self.load_config( public=self.env_config, serv_config_class=ir_config_parameter - ): + ), self.load_config(public=self.env_config): # when creating, the value is overridden by config file self.ICP.set_param("ircp_from_config", "new_value") value = self.ICP.get_param("ircp_from_config")