From 77a229e9141bee04a37e337bc51644842f503c70 Mon Sep 17 00:00:00 2001 From: Alexandre Fayolle Date: Wed, 22 Dec 2021 16:14:53 +0100 Subject: [PATCH 1/2] [FIX] server_environment_ir_config_parameter fix a conflict between this module and mail which overrides the reading of some mail related system parameters --- .../models/ir_config_parameter.py | 14 +++++++++++-- .../tests/test_server_environment_ircp.py | 20 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) 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 3c7aa9c..541aafe 100644 --- a/server_environment_ir_config_parameter/models/ir_config_parameter.py +++ b/server_environment_ir_config_parameter/models/ir_config_parameter.py @@ -39,8 +39,18 @@ class IrConfigParameter(models.Model): # should we have preloaded values in database at, # server startup, modules loading their parameters # from data files would break on unique key error. - self.sudo().set_param(key, cvalue) - value = cvalue + if not self.env.context.get("_from_get_param", 0): + # the check is to avoid recursion, for instance the mail + # addon has an override in ir.config_parameter::write which + # calls get_param if we are setting mail.catchall.alias and + # this will cause an infinite recursion. We cut that + # recursion by using the context check. + # + # The mail addon call to get_param expects to get the value + # *before* the change, so we have to return the database + # value in that case + self.sudo().with_context(_from_get_param=1).set_param(key, cvalue) + value = cvalue if value is None: return default return value 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..b8e9485 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 @@ -14,9 +14,10 @@ class TestEnv(ServerEnvironmentCase): def setUp(self): 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" - ) + self.env_config = """[ir.config_parameter] +ircp_from_config=config_value +ircp_empty= +mail.catchall.alias=my_alias""" def _load_xml(self, module, filepath): convert_file( @@ -102,3 +103,16 @@ class TestEnv(ServerEnvironmentCase): ) value = self.ICP.get_param("ircp_from_config") self.assertEqual(value, "config_value") + + def test_read_mail_catchall_alias(self): + """read mail.catchall.alias from server env: + + this must not break the mail addon's overload""" + with self.load_config( + public=self.env_config, serv_config_class=ir_config_parameter + ): + value = self.ICP.get_param("mail.catchall.alias") + self.assertEqual(value, "my_alias") + res = self.ICP.search([("key", "=", "mail.catchall.alias")]) + self.assertEqual(len(res), 1) + self.assertEqual(res.value, "my_alias") From e012f20969b0d28c86752547daa4dffdc24dd444 Mon Sep 17 00:00:00 2001 From: Mmequignon Date: Thu, 15 Jun 2023 13:49:36 +0200 Subject: [PATCH 2/2] server_environment_ir_config_parameter: Fix create/write overrides --- .../models/ir_config_parameter.py | 8 +-- .../tests/config_param_test.xml | 5 ++ .../tests/test_server_environment_ircp.py | 59 +++++++++++++++++-- 3 files changed, 64 insertions(+), 8 deletions(-) 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 541aafe..513d880 100644 --- a/server_environment_ir_config_parameter/models/ir_config_parameter.py +++ b/server_environment_ir_config_parameter/models/ir_config_parameter.py @@ -59,18 +59,18 @@ class IrConfigParameter(models.Model): def create(self, vals_list): for vals in vals_list: key = vals.get("key") - if serv_config.has_option(SECTION, key): + if key and serv_config.has_option(SECTION, key): # enforce value from config file - vals = dict(vals, value=serv_config.get(SECTION, key)) + vals.update(value=serv_config.get(SECTION, key)) return super().create(vals_list) def write(self, vals): for rec in self: - key = vals.get("key") or rec.key + key = vals.get("key", 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) + super(IrConfigParameter, rec).write(newvals) return True diff --git a/server_environment_ir_config_parameter/tests/config_param_test.xml b/server_environment_ir_config_parameter/tests/config_param_test.xml index 29d2a99..297a14e 100644 --- a/server_environment_ir_config_parameter/tests/config_param_test.xml +++ b/server_environment_ir_config_parameter/tests/config_param_test.xml @@ -3,4 +3,9 @@ ircp_from_config value_from_xml + + + other_ircp_from_config + other_value_from_xml + 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 b8e9485..867cea7 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 @@ -9,15 +9,22 @@ from odoo.addons.server_environment.tests.common import ServerEnvironmentCase from ..models import ir_config_parameter +CONFIG = """ + [ir.config_parameter] + ircp_from_config=config_value + other_ircp_from_config=other_config_value + ircp_without_record=config_value_without_record + other_ircp_without_record=other_config_value_without_record + ircp_empty= + mail.catchall.alias=my_alias +""" + class TestEnv(ServerEnvironmentCase): def setUp(self): super().setUp() self.ICP = self.env["ir.config_parameter"] - self.env_config = """[ir.config_parameter] -ircp_from_config=config_value -ircp_empty= -mail.catchall.alias=my_alias""" + self.env_config = CONFIG def _load_xml(self, module, filepath): convert_file( @@ -116,3 +123,47 @@ mail.catchall.alias=my_alias""" res = self.ICP.search([("key", "=", "mail.catchall.alias")]) self.assertEqual(len(res), 1) self.assertEqual(res.value, "my_alias") + + def test_write(self): + # there's a write override, test it here + self._load_xml( + "server_environment_ir_config_parameter", "tests/config_param_test.xml" + ) + with self.load_config( + public=self.env_config, serv_config_class=ir_config_parameter + ): + ICP = self.ICP + icp1 = ICP.search([("key", "=", "ircp_from_config")]) + self.assertEqual(icp1.value, "value_from_xml") + icp2 = ICP.search([("key", "=", "other_ircp_from_config")]) + self.assertEqual(icp2.value, "other_value_from_xml") + # Ensures that each record has its own value at write + (icp1 | icp2).write({"value": "test"}) + self.assertEqual(icp1.value, "config_value") + self.assertEqual(icp2.value, "other_config_value") + self.assertEqual(ICP.get_param(icp1.key), "config_value") + self.assertEqual(ICP.get_param(icp2.key), "other_config_value") + + def test_create(self): + self._load_xml( + "server_environment_ir_config_parameter", "tests/config_param_test.xml" + ) + with self.load_config( + public=self.env_config, serv_config_class=ir_config_parameter + ): + vals = [ + { + "key": "ircp_without_record", + "value": "NOPE", + }, + { + "key": "other_ircp_without_record", + "value": "NOPE", + }, + ] + records = self.ICP.create(vals) + # Ensures each record has its own value at create + self.assertEqual( + records.mapped("value"), + ["config_value_without_record", "other_config_value_without_record"], + )