[FIX] server_environmnet_ir_config_parameter. Do not allow users using restored database to see the production values
This commit is contained in:
parent
77fe523569
commit
2676c6def3
|
|
@ -8,3 +8,5 @@ ged_folder = /my_mounting_point/ged/
|
||||||
[wkhtml2pdf]
|
[wkhtml2pdf]
|
||||||
lib_path = /xxx/xxx/lib/wkhtmltopdf-linux-i386-0-9-9
|
lib_path = /xxx/xxx/lib/wkhtmltopdf-linux-i386-0-9-9
|
||||||
|
|
||||||
|
[ir.config_parameter]
|
||||||
|
ircp_from_config=default_config_value
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,5 @@ ged_folder = /tmp/ged/
|
||||||
lib_path = /myHome/lib/wkhtmltopdf-linux-i386-0-9-9
|
lib_path = /myHome/lib/wkhtmltopdf-linux-i386-0-9-9
|
||||||
|
|
||||||
[ir.config_parameter]
|
[ir.config_parameter]
|
||||||
ircp_from_config=config_value
|
ircp_from_config=test_config_value
|
||||||
ircp_empty=
|
ircp_empty=
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
Override System Parameters from server environment file""",
|
Override System Parameters from server environment file""",
|
||||||
'version': '12.0.1.0.0',
|
'version': '12.0.1.0.0',
|
||||||
'license': 'AGPL-3',
|
'license': 'AGPL-3',
|
||||||
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)',
|
'author': 'ACSONE SA/NV, GRAP, Odoo Community Association (OCA)',
|
||||||
'website': 'https://github.com/OCA/server-env/',
|
'website': 'https://github.com/OCA/server-env/',
|
||||||
'depends': [
|
'depends': [
|
||||||
'server_environment',
|
'server_environment',
|
||||||
|
|
|
||||||
|
|
@ -15,53 +15,36 @@ class IrConfigParameter(models.Model):
|
||||||
|
|
||||||
is_environment = fields.Boolean(
|
is_environment = fields.Boolean(
|
||||||
string="Defined by environment",
|
string="Defined by environment",
|
||||||
compute="_compute_is_environment",
|
compute="_compute_environment",
|
||||||
help="If check, the value in the database will be ignored"
|
help="If check, the value in the database will be ignored"
|
||||||
" and alternatively, the system will use the key defined"
|
" and alternatively, the system will use the key defined"
|
||||||
" in your odoo.cfg environment file.")
|
" in your odoo.cfg environment file.")
|
||||||
|
|
||||||
|
value = fields.Text(string="Database Value")
|
||||||
|
|
||||||
|
environment_value = fields.Text(
|
||||||
|
string="Environment Value",
|
||||||
|
compute="_compute_environment",
|
||||||
|
help="Alternative value, set in your odoo.cfg environment file.")
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def _compute_is_environment(self):
|
def _compute_environment(self):
|
||||||
for parameter in self:
|
for parameter in self:
|
||||||
parameter.is_environment = serv_config.has_option(
|
parameter.is_environment = serv_config.has_option(
|
||||||
SECTION, parameter.key)
|
SECTION, parameter.key)
|
||||||
|
if parameter.is_environment:
|
||||||
|
parameter.environment_value = serv_config.get(
|
||||||
|
SECTION, parameter.key)
|
||||||
|
else:
|
||||||
|
parameter.environment_value = False
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_param(self, key, default=False):
|
def get_param(self, key, default=False):
|
||||||
value = super().get_param(key, default=None)
|
|
||||||
if serv_config.has_option(SECTION, key):
|
if serv_config.has_option(SECTION, key):
|
||||||
cvalue = serv_config.get(SECTION, key)
|
cvalue = serv_config.get(SECTION, key)
|
||||||
if not cvalue:
|
if not cvalue:
|
||||||
raise UserError(_("Key %s is empty in "
|
raise UserError(_("Key %s is empty in "
|
||||||
"server_environment_file") %
|
"server_environment_file") %
|
||||||
(key, ))
|
(key, ))
|
||||||
if cvalue != value:
|
return cvalue
|
||||||
# we write in db on first access;
|
return super().get_param(key, default=default)
|
||||||
# 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 value is None:
|
|
||||||
return default
|
|
||||||
return value
|
|
||||||
|
|
||||||
@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)
|
|
||||||
|
|
||||||
@api.multi
|
|
||||||
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
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1,6 @@
|
||||||
Override System Parameters from server environment file.
|
Override System Parameters from server environment file.
|
||||||
|
|
||||||
|
Displays both columns in the ``ir.config_parameter`` form and tree views,
|
||||||
|
the "Database Value" and the "Environment Value", if set.
|
||||||
|
|
||||||
|
.. figure:: ../static/description/ir_config_parameter_tree.png
|
||||||
|
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
|
|
@ -1,6 +1,12 @@
|
||||||
<odoo>
|
<odoo>
|
||||||
<record model="ir.config_parameter" id="some_record_id">
|
<record model="ir.config_parameter" id="some_record_id_1">
|
||||||
<field name="key">ircp_from_config</field>
|
<field name="key">ircp_from_xml</field>
|
||||||
<field name="value">value_from_xml</field>
|
<field name="value">value_1_from_xml</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
<record model="ir.config_parameter" id="some_record_id_2">
|
||||||
|
<field name="key">ircp_from_config</field>
|
||||||
|
<field name="value">value_2_from_xml</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|
|
||||||
|
|
@ -19,62 +19,28 @@ class TestEnv(common.TransactionCase):
|
||||||
get_resource_path(module, filepath),
|
get_resource_path(module, filepath),
|
||||||
{}, mode='init', noupdate=False, kind='test')
|
{}, mode='init', noupdate=False, kind='test')
|
||||||
|
|
||||||
def test_get_param(self):
|
|
||||||
""" Get system parameter from config """
|
|
||||||
# it's not in db
|
|
||||||
res = self.ICP.search([('key', '=', 'ircp_from_config')])
|
|
||||||
self.assertFalse(res)
|
|
||||||
# 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')
|
|
||||||
|
|
||||||
def test_set_param_1(self):
|
|
||||||
""" We can't set parameters that are in config file """
|
|
||||||
# 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')
|
|
||||||
self.assertEqual(value, 'config_value')
|
|
||||||
# when writing, the value is overridden by config file
|
|
||||||
res = self.ICP.search([('key', '=', 'ircp_from_config')])
|
|
||||||
self.assertEqual(len(res), 1)
|
|
||||||
res.write({'value': 'new_value'})
|
|
||||||
value = self.ICP.get_param('ircp_from_config')
|
|
||||||
self.assertEqual(value, 'config_value')
|
|
||||||
# unlink works normally...
|
|
||||||
res = self.ICP.search([('key', '=', 'ircp_from_config')])
|
|
||||||
self.assertEqual(len(res), 1)
|
|
||||||
res.unlink()
|
|
||||||
res = self.ICP.search([('key', '=', 'ircp_from_config')])
|
|
||||||
self.assertEqual(len(res), 0)
|
|
||||||
# but the value is 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)
|
|
||||||
|
|
||||||
def test_set_param_2(self):
|
|
||||||
""" We can set parameters that are not in config file """
|
|
||||||
self.ICP.set_param('some.param', 'new_value')
|
|
||||||
self.assertEqual(self.ICP.get_param('some.param'), 'new_value')
|
|
||||||
res = self.ICP.search([('key', '=', 'some.param')])
|
|
||||||
res.unlink()
|
|
||||||
res = self.ICP.search([('key', '=', 'some.param')])
|
|
||||||
self.assertFalse(res)
|
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
""" Empty config values cause error """
|
""" Empty config values cause error """
|
||||||
with self.assertRaises(UserError):
|
with self.assertRaises(UserError):
|
||||||
self.ICP.get_param('ircp_empty')
|
self.ICP.get_param('ircp_empty')
|
||||||
self.assertEqual(self.ICP.get_param('ircp_nonexistant'), False)
|
self.assertEqual(self.ICP.get_param('ircp_nonexistant'), False)
|
||||||
|
|
||||||
|
def test_regular_case(self):
|
||||||
|
""" if a parameter is set only in the ir_config_parameter it should
|
||||||
|
be returned"""
|
||||||
|
self._load_xml(
|
||||||
|
'server_environment_ir_config_parameter',
|
||||||
|
'tests/config_param_test.xml'
|
||||||
|
)
|
||||||
|
value = self.ICP.get_param('ircp_from_xml')
|
||||||
|
self.assertEqual(value, 'value_1_from_xml')
|
||||||
|
|
||||||
def test_override_xmldata(self):
|
def test_override_xmldata(self):
|
||||||
|
""" if a parameter is in ir_config_parameter table AND
|
||||||
|
in the odoo.cfg file, the file value should be used."""
|
||||||
self._load_xml(
|
self._load_xml(
|
||||||
'server_environment_ir_config_parameter',
|
'server_environment_ir_config_parameter',
|
||||||
'tests/config_param_test.xml'
|
'tests/config_param_test.xml'
|
||||||
)
|
)
|
||||||
value = self.ICP.get_param('ircp_from_config')
|
value = self.ICP.get_param('ircp_from_config')
|
||||||
self.assertEqual(value, 'config_value')
|
self.assertEqual(value, 'test_config_value')
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,11 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
<field name="inherit_id" ref="base.view_ir_config_list"/>
|
<field name="inherit_id" ref="base.view_ir_config_list"/>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<field name="value" position="after">
|
<field name="value" position="after">
|
||||||
<field name="is_environment"/>
|
<field name="environment_value"/>
|
||||||
|
<field name="is_environment" invisible="1"/>
|
||||||
|
</field>
|
||||||
|
<field name="value" position="attributes">
|
||||||
|
<attribute name="attrs">{'invisible': [('is_environment', '=', True)]}</attribute>
|
||||||
</field>
|
</field>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
@ -21,13 +25,14 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
<field name="inherit_id" ref="base.view_ir_config_form"/>
|
<field name="inherit_id" ref="base.view_ir_config_form"/>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<field name="value" position="after">
|
<field name="value" position="after">
|
||||||
|
<field name="environment_value" attrs="{'invisible': [('is_environment', '=', False)]}"/>
|
||||||
<field name="is_environment"/>
|
<field name="is_environment"/>
|
||||||
</field>
|
</field>
|
||||||
<field name="key" position="attributes">
|
<field name="key" position="attributes">
|
||||||
<attribute name="attrs">{'readonly': [('is_environment', '=', True)]}</attribute>
|
<attribute name="attrs">{'readonly': [('is_environment', '=', True)]}</attribute>
|
||||||
</field>
|
</field>
|
||||||
<field name="value" position="attributes">
|
<field name="value" position="attributes">
|
||||||
<attribute name="attrs">{'readonly': [('is_environment', '=', True)]}</attribute>
|
<attribute name="attrs">{'invisible': [('is_environment', '=', True)]}</attribute>
|
||||||
</field>
|
</field>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue