server_environment_ir_config_param: silently replace values by config values
This is much more robust that raising an error, and let modules load ir.config_parameter from xml data files, while still enforcing values from the config files.
This commit is contained in:
parent
8d18b72bcf
commit
c238e599ab
|
|
@ -26,8 +26,8 @@ When first using a value, the system will read it from the configuration file
|
||||||
and override any value that would be present in the database, so the configuration
|
and override any value that would be present in the database, so the configuration
|
||||||
file has precedence.
|
file has precedence.
|
||||||
|
|
||||||
The user cannot write, create, or delete System Parameters that are defined in the
|
When creating or modifying values that are in the configuration file, the
|
||||||
configuration files.
|
module replace changes, enforcing the configuration value.
|
||||||
|
|
||||||
For example you can use this module in combination with web_environment_ribbon:
|
For example you can use this module in combination with web_environment_ribbon:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ from openerp.addons.server_environment import serv_config
|
||||||
|
|
||||||
|
|
||||||
SECTION = 'ir.config_parameter'
|
SECTION = 'ir.config_parameter'
|
||||||
CTX_NO_CHECK = 'icp_no_check'
|
|
||||||
|
|
||||||
|
|
||||||
class IrConfigParameter(models.Model):
|
class IrConfigParameter(models.Model):
|
||||||
|
|
@ -29,36 +28,27 @@ class IrConfigParameter(models.Model):
|
||||||
# should we have preloaded values in database at,
|
# should we have preloaded values in database at,
|
||||||
# server startup, modules loading their parameters
|
# server startup, modules loading their parameters
|
||||||
# from data files would break on unique key error.
|
# from data files would break on unique key error.
|
||||||
self.set_param(
|
self.set_param(cr, SUPERUSER_ID, key, cvalue)
|
||||||
cr, SUPERUSER_ID, key, cvalue,
|
|
||||||
context={CTX_NO_CHECK: True})
|
|
||||||
value = cvalue
|
value = cvalue
|
||||||
if value is None:
|
if value is None:
|
||||||
return default
|
return default
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _check_not_in_config(self, keys):
|
|
||||||
if self.env.context.get(CTX_NO_CHECK):
|
|
||||||
return
|
|
||||||
if not serv_config.has_section(SECTION):
|
|
||||||
return
|
|
||||||
config_icp_keys = set(serv_config.options(SECTION)) & set(keys)
|
|
||||||
if config_icp_keys:
|
|
||||||
raise UserError(_("System Parameter(s) %s is/are defined "
|
|
||||||
"in server_environment_files.") %
|
|
||||||
(config_icp_keys, ))
|
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def create(self, vals):
|
def create(self, vals):
|
||||||
self._check_not_in_config([vals.get('key')])
|
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(IrConfigParameter, self).create(vals)
|
return super(IrConfigParameter, self).create(vals)
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def write(self, vals):
|
def write(self, vals):
|
||||||
self._check_not_in_config(self.mapped('key'))
|
for rec in self:
|
||||||
return super(IrConfigParameter, self).write(vals)
|
key = vals.get('key') or rec.key
|
||||||
|
if serv_config.has_option(SECTION, key):
|
||||||
@api.multi
|
# enforce value from config file
|
||||||
def unlink(self):
|
newvals = dict(vals, value=serv_config.get(SECTION, key))
|
||||||
self._check_not_in_config(self.mapped('key'))
|
else:
|
||||||
return super(IrConfigParameter, self).unlink()
|
newvals = vals
|
||||||
|
super(IrConfigParameter, rec).write(newvals)
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,14 @@
|
||||||
# Copyright 2016 ACSONE SA/NV
|
# Copyright 2016 ACSONE SA/NV
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from cStringIO import StringIO
|
||||||
|
|
||||||
from openerp.exceptions import UserError
|
from openerp.exceptions import UserError
|
||||||
from openerp.tests import common
|
from openerp.tests import common
|
||||||
|
from openerp.tools import convert
|
||||||
|
|
||||||
|
|
||||||
class TestEnv(common.SavepointCase):
|
class TestEnv(common.TransactionCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestEnv, self).setUp()
|
super(TestEnv, self).setUp()
|
||||||
|
|
@ -27,19 +30,27 @@ class TestEnv(common.SavepointCase):
|
||||||
|
|
||||||
def test_set_param_1(self):
|
def test_set_param_1(self):
|
||||||
""" We can't set parameters that are in config file """
|
""" We can't set parameters that are in config file """
|
||||||
# create
|
# when creating, the value is overridden by config file
|
||||||
with self.assertRaises(UserError):
|
self.ICP.set_param('ircp_from_config', 'new_value')
|
||||||
self.ICP.set_param('ircp_from_config', 'new_value')
|
value = self.ICP.get_param('ircp_from_config')
|
||||||
# read so it's created in db
|
self.assertEqual(value, 'config_value')
|
||||||
self.ICP.get_param('ircp_from_config')
|
# when writing, the value is overridden by config file
|
||||||
# write
|
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')])
|
res = self.ICP.search([('key', '=', 'ircp_from_config')])
|
||||||
self.assertEqual(len(res), 1)
|
self.assertEqual(len(res), 1)
|
||||||
with self.assertRaises(UserError):
|
|
||||||
res.write({'ircp_from_config': 'new_value'})
|
|
||||||
# unlink
|
|
||||||
with self.assertRaises(UserError):
|
|
||||||
res.unlink()
|
|
||||||
|
|
||||||
def test_set_param_2(self):
|
def test_set_param_2(self):
|
||||||
""" We can set parameters that are not in config file """
|
""" We can set parameters that are not in config file """
|
||||||
|
|
@ -55,3 +66,16 @@ class TestEnv(common.SavepointCase):
|
||||||
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_override_xmldata(self):
|
||||||
|
xml = """<odoo>
|
||||||
|
<data>
|
||||||
|
<record model="ir.config_parameter" id="some_record_id">
|
||||||
|
<field name="key">ircp_from_config</field>
|
||||||
|
<field name="value">value_from_xml</field>
|
||||||
|
</record>
|
||||||
|
</data>
|
||||||
|
</odoo>"""
|
||||||
|
convert.convert_xml_import(self.env.cr, 'testmodule', StringIO(xml))
|
||||||
|
value = self.ICP.get_param('ircp_from_config')
|
||||||
|
self.assertEqual(value, 'config_value')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue