You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, models, _
  5. from odoo.exceptions import UserError
  6. from odoo.addons.server_environment import serv_config
  7. SECTION = 'ir.config_parameter'
  8. class IrConfigParameter(models.Model):
  9. _inherit = 'ir.config_parameter'
  10. @api.model
  11. def get_param(self, key, default=False):
  12. value = super(IrConfigParameter, self).get_param(key, default=None)
  13. if serv_config.has_option(SECTION, key):
  14. cvalue = serv_config.get(SECTION, key)
  15. if not cvalue:
  16. raise UserError(_("Key %s is empty in "
  17. "server_environment_file") %
  18. (key, ))
  19. if cvalue != value:
  20. # we write in db on first access;
  21. # should we have preloaded values in database at,
  22. # server startup, modules loading their parameters
  23. # from data files would break on unique key error.
  24. self.sudo().set_param(key, cvalue)
  25. value = cvalue
  26. if value is None:
  27. return default
  28. return value
  29. @api.model
  30. def create(self, vals):
  31. key = vals.get('key')
  32. if serv_config.has_option(SECTION, key):
  33. # enforce value from config file
  34. vals = dict(vals, value=serv_config.get(SECTION, key))
  35. return super(IrConfigParameter, self).create(vals)
  36. @api.multi
  37. def write(self, vals):
  38. for rec in self:
  39. key = vals.get('key') or rec.key
  40. if serv_config.has_option(SECTION, key):
  41. # enforce value from config file
  42. newvals = dict(vals, value=serv_config.get(SECTION, key))
  43. else:
  44. newvals = vals
  45. super(IrConfigParameter, rec).write(newvals)
  46. return True