-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
ir_config_parameter.py
67 lines (56 loc) · 2.29 KB
/
ir_config_parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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.exceptions import UserError
from odoo.addons.server_environment import serv_config
SECTION = 'ir.config_parameter'
class IrConfigParameter(models.Model):
_inherit = 'ir.config_parameter'
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.")
@api.multi
def _compute_is_environment(self):
for parameter in self:
parameter.is_environment = serv_config.has_option(
SECTION, parameter.key)
@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)
if not cvalue:
raise UserError(_("Key %s is empty in "
"server_environment_file") %
(key, ))
if cvalue != value:
# we write in db on first access;
# should we have preloaded values in database at,
# server startup, modules loading their parameters
# from data files would break on unique key error.
self.sudo().set_param(key, cvalue)
value = cvalue
if value is None:
return default
return value
@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)
@api.multi
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