[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:
parent
b2c1a2184c
commit
360a6ce583
|
|
@ -1 +1,2 @@
|
|||
from . import models
|
||||
from .hook import post_init_keep_parameter_value
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@
|
|||
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
|
||||
"website": "https://github.com/OCA/server-env",
|
||||
"depends": ["server_environment"],
|
||||
"data": [
|
||||
"views/view_ir_config_parameter.xml",
|
||||
],
|
||||
"post_init_hook": "post_init_keep_parameter_value",
|
||||
"data": [],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
# Copyright 2016-2018 ACSONE SA/NV
|
||||
# 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.addons.server_environment.server_env import serv_config
|
||||
|
|
@ -10,26 +10,34 @@ SECTION = "ir.config_parameter"
|
|||
|
||||
|
||||
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(
|
||||
string="Defined by environment",
|
||||
compute="_compute_is_environment",
|
||||
help="If check, the value in the database will be ignored"
|
||||
" and alternatively, the system will use the key defined"
|
||||
" in your odoo.cfg environment file.",
|
||||
)
|
||||
@property
|
||||
def _server_env_fields(self):
|
||||
base_fields = super()._server_env_fields
|
||||
parameter_fields = {
|
||||
"value": {},
|
||||
}
|
||||
parameter_fields.update(base_fields)
|
||||
return parameter_fields
|
||||
|
||||
def _compute_is_environment(self):
|
||||
for parameter in self:
|
||||
parameter.is_environment = serv_config.has_option(SECTION, parameter.key)
|
||||
@api.model
|
||||
def _server_env_global_section_name(self):
|
||||
"""Name of the global section in the configuration files
|
||||
|
||||
Can be customized in your model
|
||||
"""
|
||||
return SECTION
|
||||
|
||||
@api.model
|
||||
def get_param(self, key, default=False):
|
||||
value = super().get_param(key, default=None)
|
||||
if serv_config.has_option(SECTION, key):
|
||||
cvalue = serv_config.get(SECTION, key)
|
||||
section = "%s.%s" % (SECTION, key)
|
||||
if serv_config.has_option(section, "value"):
|
||||
cvalue = serv_config.get(section, "value")
|
||||
if not cvalue:
|
||||
raise UserError(
|
||||
_("Key %s is empty in " "server_environment_file") % (key,)
|
||||
|
|
@ -47,19 +55,24 @@ class IrConfigParameter(models.Model):
|
|||
|
||||
@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)
|
||||
record = super().create(vals)
|
||||
# in case of creation of a param which is in config file but with another value
|
||||
# the value is in cache after creation and then a get_param will give back the
|
||||
# cache value instead of reading the field from the config...
|
||||
# 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):
|
||||
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
|
||||
res = super().write(vals)
|
||||
# in case of creation of a param which is in config file but with another value
|
||||
# the value is in cache after creation and then a get_param will give back the
|
||||
# cache value instead of reading the field from the config...
|
||||
# so if there is a config value, we clean the cache so it will be taken into
|
||||
# account of the next read
|
||||
for record in self:
|
||||
if record._server_env_has_key_defined("value"):
|
||||
record.invalidate_cache(fnames=["value"], ids=record.ids)
|
||||
return res
|
||||
|
|
|
|||
|
|
@ -15,7 +15,10 @@ class TestEnv(ServerEnvironmentCase):
|
|||
super().setUp()
|
||||
self.ICP = self.env["ir.config_parameter"]
|
||||
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):
|
||||
|
|
@ -49,7 +52,7 @@ class TestEnv(ServerEnvironmentCase):
|
|||
"""We can't set parameters that are in config file"""
|
||||
with self.load_config(
|
||||
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
|
||||
self.ICP.set_param("ircp_from_config", "new_value")
|
||||
value = self.ICP.get_param("ircp_from_config")
|
||||
|
|
|
|||
Loading…
Reference in New Issue