Browse Source

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.
pull/640/head
Stéphane Bidoul (ACSONE) 8 years ago
parent
commit
2d847a833f
No known key found for this signature in database GPG Key ID: 866D394B4986F82D
  1. 4
      server_environment_ir_config_parameter/README.rst
  2. 36
      server_environment_ir_config_parameter/models/ir_config_parameter.py
  3. 48
      server_environment_ir_config_parameter/tests/test_server_environment_ircp.py

4
server_environment_ir_config_parameter/README.rst

@ -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
file has precedence.
The user cannot write, create, or delete System Parameters that are defined in the
configuration files.
When creating or modifying values that are in the configuration file, the
module replace changes, enforcing the configuration value.
For example you can use this module in combination with web_environment_ribbon:

36
server_environment_ir_config_parameter/models/ir_config_parameter.py

@ -8,7 +8,6 @@ from openerp.addons.server_environment import serv_config
SECTION = 'ir.config_parameter'
CTX_NO_CHECK = 'icp_no_check'
class IrConfigParameter(models.Model):
@ -29,36 +28,27 @@ 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.set_param(
cr, SUPERUSER_ID, key, cvalue,
context={CTX_NO_CHECK: True})
self.set_param(cr, SUPERUSER_ID, key, cvalue)
value = cvalue
if value is None:
return default
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
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)
@api.multi
def write(self, vals):
self._check_not_in_config(self.mapped('key'))
return super(IrConfigParameter, self).write(vals)
@api.multi
def unlink(self):
self._check_not_in_config(self.mapped('key'))
return super(IrConfigParameter, self).unlink()
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(IrConfigParameter, rec).write(newvals)

48
server_environment_ir_config_parameter/tests/test_server_environment_ircp.py

@ -2,11 +2,14 @@
# Copyright 2016 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from cStringIO import StringIO
from openerp.exceptions import UserError
from openerp.tests import common
from openerp.tools import convert
class TestEnv(common.SavepointCase):
class TestEnv(common.TransactionCase):
def setUp(self):
super(TestEnv, self).setUp()
@ -27,19 +30,27 @@ class TestEnv(common.SavepointCase):
def test_set_param_1(self):
""" We can't set parameters that are in config file """
# create
with self.assertRaises(UserError):
self.ICP.set_param('ircp_from_config', 'new_value')
# read so it's created in db
self.ICP.get_param('ircp_from_config')
# write
# 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)
with self.assertRaises(UserError):
res.write({'ircp_from_config': 'new_value'})
# unlink
with self.assertRaises(UserError):
res.unlink()
def test_set_param_2(self):
""" We can set parameters that are not in config file """
@ -55,3 +66,16 @@ class TestEnv(common.SavepointCase):
with self.assertRaises(UserError):
self.ICP.get_param('ircp_empty')
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…
Cancel
Save