[IMP] server_environment_ir_config_parameter: black, isort

This commit is contained in:
Akim Juillerat 2020-01-28 16:32:00 +01:00 committed by Benoit Aimont
parent 783f41d1cb
commit e33a69183e
8 changed files with 56 additions and 55 deletions

View File

@ -2,14 +2,12 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Server Environment Ir Config Parameter', "name": "Server Environment Ir Config Parameter",
'summary': """ "summary": """
Override System Parameters from server environment file""", Override System Parameters from server environment file""",
'version': '13.0.1.0.0', "version": "13.0.1.0.0",
'license': 'AGPL-3', "license": "AGPL-3",
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)', "author": "ACSONE SA/NV, Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/server-env/', "website": "https://github.com/OCA/server-env/",
'depends': [ "depends": ["server_environment"],
'server_environment',
],
} }

View File

@ -1,17 +1,17 @@
# Copyright 2016-2018 ACSONE SA/NV # Copyright 2016-2018 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 odoo import api, models, _ from odoo import _, api, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.addons.server_environment import serv_config from odoo.addons.server_environment import serv_config
SECTION = "ir.config_parameter"
SECTION = 'ir.config_parameter'
class IrConfigParameter(models.Model): class IrConfigParameter(models.Model):
_inherit = 'ir.config_parameter' _inherit = "ir.config_parameter"
@api.model @api.model
def get_param(self, key, default=False): def get_param(self, key, default=False):
@ -19,9 +19,9 @@ class IrConfigParameter(models.Model):
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(
"server_environment_file") % _("Key %s is empty in " "server_environment_file") % (key,)
(key, )) )
if cvalue != value: if cvalue != value:
# we write in db on first access; # we write in db on first access;
# should we have preloaded values in database at, # should we have preloaded values in database at,
@ -35,7 +35,7 @@ class IrConfigParameter(models.Model):
@api.model @api.model
def create(self, vals): def create(self, vals):
key = vals.get('key') key = vals.get("key")
if serv_config.has_option(SECTION, key): if serv_config.has_option(SECTION, key):
# enforce value from config file # enforce value from config file
vals = dict(vals, value=serv_config.get(SECTION, key)) vals = dict(vals, value=serv_config.get(SECTION, key))
@ -43,7 +43,7 @@ class IrConfigParameter(models.Model):
def write(self, vals): def write(self, vals):
for rec in self: for rec in self:
key = vals.get('key') or rec.key key = vals.get("key") or rec.key
if serv_config.has_option(SECTION, key): if serv_config.has_option(SECTION, key):
# enforce value from config file # enforce value from config file
newvals = dict(vals, value=serv_config.get(SECTION, key)) newvals = dict(vals, value=serv_config.get(SECTION, key))

View File

@ -14,4 +14,4 @@ For example you can use this module in combination with web_environment_ribbon:
.. code:: .. code::
[ir.config_parameter] [ir.config_parameter]
ribbon.name=DEV ribbon.name=DEV

View File

@ -1,2 +1,2 @@
This module is maintained by: This module is maintained by:
* Odoo Community Association * Odoo Community Association

View File

@ -1 +1 @@
Override System Parameters from server environment file. Override System Parameters from server environment file.

View File

@ -1,4 +1,4 @@
When the user modifies System Parameters that are defined in the config When the user modifies System Parameters that are defined in the config
file, the changes are ignored. It would be nice to display which system file, the changes are ignored. It would be nice to display which system
parameters come from the config file and possibly make their key and value parameters come from the config file and possibly make their key and value
readonly in the user interface. readonly in the user interface.

View File

@ -1,2 +1,2 @@
Before using this module, you must be familiar with the Before using this module, you must be familiar with the
server_environment module. server_environment module.

View File

@ -2,79 +2,82 @@
# 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 odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.modules.module import get_resource_path
from odoo.tests import common from odoo.tests import common
from odoo.tools import convert_file from odoo.tools import convert_file
from odoo.modules.module import get_resource_path
class TestEnv(common.TransactionCase): class TestEnv(common.TransactionCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.ICP = self.env['ir.config_parameter'] self.ICP = self.env["ir.config_parameter"]
def _load_xml(self, module, filepath): def _load_xml(self, module, filepath):
convert_file( convert_file(
self.env.cr, module, self.env.cr,
module,
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): def test_get_param(self):
""" Get system parameter from config """ """ Get system parameter from config """
# it's not in db # it's not in db
res = self.ICP.search([('key', '=', 'ircp_from_config')]) res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertFalse(res) self.assertFalse(res)
# read so it's created in db # read so it's created in db
value = self.ICP.get_param('ircp_from_config') value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, 'config_value') self.assertEqual(value, "config_value")
# now it's in db # now it's in db
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)
self.assertEqual(res.value, 'config_value') self.assertEqual(res.value, "config_value")
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 """
# when creating, the value is overridden by config file # when creating, the value is overridden by config file
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') value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, 'config_value') self.assertEqual(value, "config_value")
# when writing, the value is overridden by config file # when writing, the value is overridden by config file
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)
res.write({'value': 'new_value'}) res.write({"value": "new_value"})
value = self.ICP.get_param('ircp_from_config') value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, 'config_value') self.assertEqual(value, "config_value")
# unlink works normally... # unlink works normally...
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)
res.unlink() res.unlink()
res = self.ICP.search([('key', '=', 'ircp_from_config')]) res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 0) self.assertEqual(len(res), 0)
# but the value is recreated when getting param again # but the value is recreated when getting param again
value = self.ICP.get_param('ircp_from_config') value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, 'config_value') 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)
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 """
self.ICP.set_param('some.param', 'new_value') self.ICP.set_param("some.param", "new_value")
self.assertEqual(self.ICP.get_param('some.param'), 'new_value') self.assertEqual(self.ICP.get_param("some.param"), "new_value")
res = self.ICP.search([('key', '=', 'some.param')]) res = self.ICP.search([("key", "=", "some.param")])
res.unlink() res.unlink()
res = self.ICP.search([('key', '=', 'some.param')]) res = self.ICP.search([("key", "=", "some.param")])
self.assertFalse(res) 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_override_xmldata(self): def test_override_xmldata(self):
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, "config_value")