Merge PR #158 into 16.0

Signed-off-by gurneyalex
This commit is contained in:
OCA-git-bot 2023-10-23 11:55:03 +00:00
commit 3ef40463a8
3 changed files with 89 additions and 9 deletions

View File

@ -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
@ -49,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

View File

@ -3,4 +3,9 @@
<field name="key">ircp_from_config</field>
<field name="value">value_from_xml</field>
</record>
<record model="ir.config_parameter" id="some_other_record_id">
<field name="key">other_ircp_from_config</field>
<field name="value">other_value_from_xml</field>
</record>
</odoo>

View File

@ -9,14 +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]\n" "ircp_from_config=config_value\n" "ircp_empty=\n"
)
self.env_config = CONFIG
def _load_xml(self, module, filepath):
convert_file(
@ -102,3 +110,60 @@ 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")
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"],
)