[REF] make server_environment_ir_config_parameter depends on server.env.mixin

This way it is compatible with other modules like server_environment_data_encryption
This commit is contained in:
Florian da Costa 2022-06-30 16:37:41 +02:00
parent b2c1a2184c
commit 360a6ce583
5 changed files with 62 additions and 33 deletions

View File

@ -1 +1,2 @@
from . import models from . import models
from .hook import post_init_keep_parameter_value

View File

@ -10,7 +10,6 @@
"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": ["server_environment"], "depends": ["server_environment"],
"data": [ "post_init_hook": "post_init_keep_parameter_value",
"views/view_ir_config_parameter.xml", "data": [],
],
} }

View File

@ -0,0 +1,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import SUPERUSER_ID, api
def post_init_keep_parameter_value(cr, registry):
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
env.cr.execute("""SELECT id, value FROM ir_config_parameter""")
result = env.cr.fetchall()
for config_id, value in result:
env["ir.config_parameter"].browse(config_id).write({"value": value})
return True

View File

@ -1,7 +1,7 @@
# 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, fields, models from odoo import _, api, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.addons.server_environment.server_env import serv_config from odoo.addons.server_environment.server_env import serv_config
@ -10,26 +10,34 @@ SECTION = "ir.config_parameter"
class IrConfigParameter(models.Model): class IrConfigParameter(models.Model):
_name = "ir.config_parameter"
_inherit = ["ir.config_parameter", "server.env.mixin"]
_inherit = "ir.config_parameter" _server_env_section_name_field = "key"
is_environment = fields.Boolean( @property
string="Defined by environment", def _server_env_fields(self):
compute="_compute_is_environment", base_fields = super()._server_env_fields
help="If check, the value in the database will be ignored" parameter_fields = {
" and alternatively, the system will use the key defined" "value": {},
" in your odoo.cfg environment file.", }
) parameter_fields.update(base_fields)
return parameter_fields
def _compute_is_environment(self): @api.model
for parameter in self: def _server_env_global_section_name(self):
parameter.is_environment = serv_config.has_option(SECTION, parameter.key) """Name of the global section in the configuration files
Can be customized in your model
"""
return SECTION
@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) value = super().get_param(key, default=None)
if serv_config.has_option(SECTION, key): section = "%s.%s" % (SECTION, key)
cvalue = serv_config.get(SECTION, key) if serv_config.has_option(section, "value"):
cvalue = serv_config.get(section, "value")
if not cvalue: if not cvalue:
raise UserError( raise UserError(
_("Key %s is empty in " "server_environment_file") % (key,) _("Key %s is empty in " "server_environment_file") % (key,)
@ -47,19 +55,24 @@ class IrConfigParameter(models.Model):
@api.model @api.model
def create(self, vals): def create(self, vals):
key = vals.get("key") record = super().create(vals)
if serv_config.has_option(SECTION, key): # in case of creation of a param which is in config file but with another value
# enforce value from config file # the value is in cache after creation and then a get_param will give back the
vals = dict(vals, value=serv_config.get(SECTION, key)) # cache value instead of reading the field from the config...
return super().create(vals) # so if there is a config value, we clean the cache so it will be taken into
# account of the next read
if record._server_env_has_key_defined("value"):
record.invalidate_cache(fnames=["value"], ids=record.ids)
return record
def write(self, vals): def write(self, vals):
for rec in self: res = super().write(vals)
key = vals.get("key") or rec.key # in case of creation of a param which is in config file but with another value
if serv_config.has_option(SECTION, key): # the value is in cache after creation and then a get_param will give back the
# enforce value from config file # cache value instead of reading the field from the config...
newvals = dict(vals, value=serv_config.get(SECTION, key)) # so if there is a config value, we clean the cache so it will be taken into
else: # account of the next read
newvals = vals for record in self:
super().write(newvals) if record._server_env_has_key_defined("value"):
return True record.invalidate_cache(fnames=["value"], ids=record.ids)
return res

View File

@ -15,7 +15,10 @@ class TestEnv(ServerEnvironmentCase):
super().setUp() super().setUp()
self.ICP = self.env["ir.config_parameter"] self.ICP = self.env["ir.config_parameter"]
self.env_config = ( self.env_config = (
"[ir.config_parameter]\n" "ircp_from_config=config_value\n" "ircp_empty=\n" "[ir.config_parameter.ircp_from_config]\n"
"value=config_value\n\n"
"[ir.config_parameter.ircp_empty]\n"
"value=\n\n"
) )
def _load_xml(self, module, filepath): def _load_xml(self, module, filepath):
@ -49,7 +52,7 @@ class TestEnv(ServerEnvironmentCase):
"""We can't set parameters that are in config file""" """We can't set parameters that are in config file"""
with self.load_config( with self.load_config(
public=self.env_config, serv_config_class=ir_config_parameter public=self.env_config, serv_config_class=ir_config_parameter
): ), self.load_config(public=self.env_config):
# 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")