Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,16 @@
_("Key %s is empty in " "server_environment_file") % (key,)
)
if cvalue != value:
# we write in db on first access;
# should we have preloaded values in database at,
# server startup, modules loading their parameters
# from data files would break on unique key error.
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
value = cvalue
if value is None:
return default
return value

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
key = vals.get("key")
if key and serv_config.has_option(SECTION, key):
# enforce value from config file
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", 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(IrConfigParameter, rec).write(newvals)
return True
def read(self, _fields=None, load="_classic_read"):
res = super().read(_fields, load=load)
environment_values = [r for r in res if r["is_environment"]]

Check warning on line 44 in server_environment_ir_config_parameter/models/ir_config_parameter.py

View check run for this annotation

Codecov / codecov/patch

server_environment_ir_config_parameter/models/ir_config_parameter.py#L43-L44

Added lines #L43 - L44 were not covered by tests
for environment_value in environment_values:
environment_value["value"] = serv_config.get(

Check warning on line 46 in server_environment_ir_config_parameter/models/ir_config_parameter.py

View check run for this annotation

Codecov / codecov/patch

server_environment_ir_config_parameter/models/ir_config_parameter.py#L46

Added line #L46 was not covered by tests
SECTION, environment_value["key"]
)
return res

Check warning on line 49 in server_environment_ir_config_parameter/models/ir_config_parameter.py

View check run for this annotation

Codecov / codecov/patch

server_environment_ir_config_parameter/models/ir_config_parameter.py#L49

Added line #L49 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ def test_get_param(self):
# read so it's created in db
value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, "config_value")
# now it's in db
res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 1)
self.assertEqual(res.value, "config_value")
# It should not be saved in db
self.assertFalse(self.ICP.search([("key", "=", "ircp_from_config")]))

def test_set_param_1(self):
"""We can't set parameters that are in config file"""
Expand All @@ -74,11 +72,11 @@ def test_set_param_1(self):
res.unlink()
res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 0)
# but the value is recreated when getting param again
# but the value should not be recreated when getting param again
value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, "config_value")
res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 1)
# It should not be saved in db
self.assertFalse(self.ICP.search([("key", "=", "ircp_from_config")]))

def test_set_param_2(self):
"""We can set parameters that are not in config file"""
Expand Down Expand Up @@ -120,27 +118,26 @@ def test_read_mail_catchall_alias(self):
):
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")
# It should not be saved in db
self.assertFalse(self.ICP.search([("key", "=", "mail.catchall.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
):
self._load_xml(
"server_environment_ir_config_parameter", "tests/config_param_test.xml"
)
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(icp1.value, "test")
self.assertEqual(icp2.value, "test")
self.assertEqual(ICP.get_param(icp1.key), "config_value")
self.assertEqual(ICP.get_param(icp2.key), "other_config_value")

Expand All @@ -165,5 +162,5 @@ def test_create(self):
# Ensures each record has its own value at create
self.assertEqual(
records.mapped("value"),
["config_value_without_record", "other_config_value_without_record"],
["NOPE", "NOPE"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="value" position="after">
<field name="is_environment" />
</field>
<field name="value" position="attributes">
<attribute name="invisible">is_environment</attribute>
</field>
</field>
</record>

Expand All @@ -27,6 +30,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
</field>
<field name="value" position="attributes">
<attribute name="readonly">is_environment</attribute>
<attribute name="invisible">is_environment</attribute>
</field>
</field>
</record>
Expand Down