Read default values from database when no config is provided
Automatically add <field_name>_env_default for every field transformed to a "computed from env" field, so a default value can be set. It will be used when the configuration is not set in a configuration file (when the key is absent, not empty).
This commit is contained in:
parent
fbc4f8f928
commit
d917407672
|
|
@ -20,8 +20,10 @@
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "server configuration environment files",
|
"name": "server configuration environment files",
|
||||||
"version": "11.0.1.0.1",
|
"depends": [
|
||||||
"depends": ["base"],
|
"base",
|
||||||
|
"base_sparse_field",
|
||||||
|
],
|
||||||
"author": "Camptocamp,Odoo Community Association (OCA)",
|
"author": "Camptocamp,Odoo Community Association (OCA)",
|
||||||
"summary": "move some configurations out of the database",
|
"summary": "move some configurations out of the database",
|
||||||
"website": "http://odoo-community.org/",
|
"website": "http://odoo-community.org/",
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from odoo import api, models
|
from odoo import api, fields, models
|
||||||
from ..serv_config import serv_config
|
from ..serv_config import serv_config
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
@ -22,19 +22,27 @@ class ServerEnvMixin(models.AbstractModel):
|
||||||
def _server_env_fields(self):
|
def _server_env_fields(self):
|
||||||
return {"directory_path": 'get'}
|
return {"directory_path": 'get'}
|
||||||
|
|
||||||
With the snippet above, the "storage.backend" model will now use a server
|
With the snippet above, the "storage.backend" model now uses a server
|
||||||
environment configuration for the field ``directory_path``.
|
environment configuration for the field ``directory_path``.
|
||||||
|
|
||||||
Under the hood, this mixin will automatically replaces the original field
|
Under the hood, this mixin automatically replaces the original field
|
||||||
by a computed field that reads from the configuration files.
|
by a computed field that reads from the configuration files.
|
||||||
|
|
||||||
By default, it will look for the configuration in a section named
|
By default, it looks for the configuration in a section named
|
||||||
``[model_name.Record Name]`` where ``model_name`` is the ``_name`` of the
|
``[model_name.Record Name]`` where ``model_name`` is the ``_name`` of the
|
||||||
model with ``.`` replaced by ``_``. It can be customized by overriding the
|
model with ``.`` replaced by ``_``. It can be customized by overriding the
|
||||||
method :meth:`~server_env_section_name`.
|
method :meth:`~server_env_section_name`.
|
||||||
|
|
||||||
|
For each field transformed to an env-computed field, a companion field
|
||||||
|
``<field>_env_default`` is automatically created. When it's value is set
|
||||||
|
and the configuration files do not contain a key, the env-computed field
|
||||||
|
uses the default value stored in database. If a key is empty, the
|
||||||
|
env-computed field has an empty value.
|
||||||
"""
|
"""
|
||||||
_name = 'server.env.mixin'
|
_name = 'server.env.mixin'
|
||||||
|
|
||||||
|
server_env_defaults = fields.Serialized()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _server_env_fields(self):
|
def _server_env_fields(self):
|
||||||
"""Dict of fields to replace by fields computed from env
|
"""Dict of fields to replace by fields computed from env
|
||||||
|
|
@ -92,11 +100,24 @@ class ServerEnvMixin(models.AbstractModel):
|
||||||
for record in self:
|
for record in self:
|
||||||
for field_name, getter_name in self._server_env_fields.items():
|
for field_name, getter_name in self._server_env_fields.items():
|
||||||
section_name = self._server_env_section_name()
|
section_name = self._server_env_section_name()
|
||||||
value = self._server_env_read_from_config(
|
if (section_name in serv_config
|
||||||
section_name, field_name, getter_name
|
and field_name in serv_config[section_name]):
|
||||||
)
|
|
||||||
|
value = self._server_env_read_from_config(
|
||||||
|
section_name, field_name, getter_name
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
default_field = self._server_env_default_fieldname(
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
value = record[default_field]
|
||||||
|
|
||||||
record[field_name] = value
|
record[field_name] = value
|
||||||
|
|
||||||
|
def _server_env_default_fieldname(self, base_field_name):
|
||||||
|
return '%s_env_default' % (base_field_name,)
|
||||||
|
|
||||||
def _server_env_transform_field_to_read_from_env(self, field):
|
def _server_env_transform_field_to_read_from_env(self, field):
|
||||||
"""Transform the original field in a computed field"""
|
"""Transform the original field in a computed field"""
|
||||||
field.compute = '_compute_server_env'
|
field.compute = '_compute_server_env'
|
||||||
|
|
@ -104,9 +125,23 @@ class ServerEnvMixin(models.AbstractModel):
|
||||||
field.copy = False
|
field.copy = False
|
||||||
field.sparse = None
|
field.sparse = None
|
||||||
|
|
||||||
|
def _server_env_add_default_field(self, base_field):
|
||||||
|
fieldname = self._server_env_default_fieldname(base_field.name)
|
||||||
|
if fieldname not in self._fields:
|
||||||
|
base_field_cls = base_field.__class__
|
||||||
|
field_args = base_field.args
|
||||||
|
field_args.pop('_sequence', None)
|
||||||
|
field_args.update({
|
||||||
|
'sparse': 'server_env_defaults',
|
||||||
|
'automatic': True,
|
||||||
|
})
|
||||||
|
field = base_field_cls(**field_args)
|
||||||
|
self._add_field(fieldname, field)
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _setup_base(self):
|
def _setup_base(self):
|
||||||
super()._setup_base()
|
super()._setup_base()
|
||||||
for fieldname in self._server_env_fields:
|
for fieldname in self._server_env_fields:
|
||||||
field = self._fields[fieldname]
|
field = self._fields[fieldname]
|
||||||
self._server_env_transform_field_to_read_from_env(field)
|
self._server_env_transform_field_to_read_from_env(field)
|
||||||
|
self._server_env_add_default_field(field)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue