Skip to content

Commit aa4738f

Browse files
committed
[FIX] server_environment_ir_config_parameter: Don't save values in DB
1 parent 03d8a03 commit aa4738f

3 files changed

Lines changed: 26 additions & 51 deletions

File tree

server_environment_ir_config_parameter/models/ir_config_parameter.py

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,16 @@ def get_param(self, key, default=False):
3434
_("Key %s is empty in " "server_environment_file") % (key,)
3535
)
3636
if cvalue != value:
37-
# we write in db on first access;
38-
# should we have preloaded values in database at,
39-
# server startup, modules loading their parameters
40-
# from data files would break on unique key error.
41-
if not self.env.context.get("_from_get_param", 0):
42-
# the check is to avoid recursion, for instance the mail
43-
# addon has an override in ir.config_parameter::write which
44-
# calls get_param if we are setting mail.catchall.alias and
45-
# this will cause an infinite recursion. We cut that
46-
# recursion by using the context check.
47-
#
48-
# The mail addon call to get_param expects to get the value
49-
# *before* the change, so we have to return the database
50-
# value in that case
51-
self.sudo().with_context(_from_get_param=1).set_param(key, cvalue)
52-
value = cvalue
37+
value = cvalue
5338
if value is None:
5439
return default
5540
return value
5641

57-
@api.model_create_multi
58-
def create(self, vals_list):
59-
for vals in vals_list:
60-
key = vals.get("key")
61-
if key and serv_config.has_option(SECTION, key):
62-
# enforce value from config file
63-
vals.update(value=serv_config.get(SECTION, key))
64-
return super().create(vals_list)
65-
66-
def write(self, vals):
67-
for rec in self:
68-
key = vals.get("key", rec.key)
69-
if serv_config.has_option(SECTION, key):
70-
# enforce value from config file
71-
newvals = dict(vals, value=serv_config.get(SECTION, key))
72-
else:
73-
newvals = vals
74-
super(IrConfigParameter, rec).write(newvals)
75-
return True
42+
def read(self, _fields=None, load="_classic_read"):
43+
res = super().read(_fields, load=load)
44+
environment_values = [r for r in res if r["is_environment"]]
45+
for environment_value in environment_values:
46+
environment_value["value"] = serv_config.get(
47+
SECTION, environment_value["key"]
48+
)
49+
return res

server_environment_ir_config_parameter/tests/test_server_environment_ircp.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ def test_get_param(self):
4848
# read so it's created in db
4949
value = self.ICP.get_param("ircp_from_config")
5050
self.assertEqual(value, "config_value")
51-
# now it's in db
52-
res = self.ICP.search([("key", "=", "ircp_from_config")])
53-
self.assertEqual(len(res), 1)
54-
self.assertEqual(res.value, "config_value")
51+
# It should not be saved in db
52+
self.assertFalse(self.ICP.search([("key", "=", "ircp_from_config")]))
5553

5654
def test_set_param_1(self):
5755
"""We can't set parameters that are in config file"""
@@ -74,11 +72,11 @@ def test_set_param_1(self):
7472
res.unlink()
7573
res = self.ICP.search([("key", "=", "ircp_from_config")])
7674
self.assertEqual(len(res), 0)
77-
# but the value is recreated when getting param again
75+
# but the value should not be recreated when getting param again
7876
value = self.ICP.get_param("ircp_from_config")
7977
self.assertEqual(value, "config_value")
80-
res = self.ICP.search([("key", "=", "ircp_from_config")])
81-
self.assertEqual(len(res), 1)
78+
# It should not be saved in db
79+
self.assertFalse(self.ICP.search([("key", "=", "ircp_from_config")]))
8280

8381
def test_set_param_2(self):
8482
"""We can set parameters that are not in config file"""
@@ -120,27 +118,26 @@ def test_read_mail_catchall_alias(self):
120118
):
121119
value = self.ICP.get_param("mail.catchall.alias")
122120
self.assertEqual(value, "my_alias")
123-
res = self.ICP.search([("key", "=", "mail.catchall.alias")])
124-
self.assertEqual(len(res), 1)
125-
self.assertEqual(res.value, "my_alias")
121+
# It should not be saved in db
122+
self.assertFalse(self.ICP.search([("key", "=", "mail.catchall.alias")]))
126123

127124
def test_write(self):
128125
# there's a write override, test it here
129-
self._load_xml(
130-
"server_environment_ir_config_parameter", "tests/config_param_test.xml"
131-
)
132126
with self.load_config(
133127
public=self.env_config, serv_config_class=ir_config_parameter
134128
):
129+
self._load_xml(
130+
"server_environment_ir_config_parameter", "tests/config_param_test.xml"
131+
)
135132
ICP = self.ICP
136133
icp1 = ICP.search([("key", "=", "ircp_from_config")])
137134
self.assertEqual(icp1.value, "value_from_xml")
138135
icp2 = ICP.search([("key", "=", "other_ircp_from_config")])
139136
self.assertEqual(icp2.value, "other_value_from_xml")
140137
# Ensures that each record has its own value at write
141138
(icp1 | icp2).write({"value": "test"})
142-
self.assertEqual(icp1.value, "config_value")
143-
self.assertEqual(icp2.value, "other_config_value")
139+
self.assertEqual(icp1.value, "test")
140+
self.assertEqual(icp2.value, "test")
144141
self.assertEqual(ICP.get_param(icp1.key), "config_value")
145142
self.assertEqual(ICP.get_param(icp2.key), "other_config_value")
146143

@@ -165,5 +162,5 @@ def test_create(self):
165162
# Ensures each record has its own value at create
166163
self.assertEqual(
167164
records.mapped("value"),
168-
["config_value_without_record", "other_config_value_without_record"],
165+
["NOPE", "NOPE"],
169166
)

server_environment_ir_config_parameter/views/view_ir_config_parameter.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
1212
<field name="value" position="after">
1313
<field name="is_environment" />
1414
</field>
15+
<field name="value" position="attributes">
16+
<attribute name="invisible">is_environment</attribute>
17+
</field>
1518
</field>
1619
</record>
1720

@@ -27,6 +30,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
2730
</field>
2831
<field name="value" position="attributes">
2932
<attribute name="readonly">is_environment</attribute>
33+
<attribute name="invisible">is_environment</attribute>
3034
</field>
3135
</field>
3236
</record>

0 commit comments

Comments
 (0)