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.

60 lines
2.1 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 openerp import api, models, _, SUPERUSER_ID
  5. from openerp.exceptions import UserError
  6. from openerp.addons.server_environment import serv_config
  7. SECTION = 'ir.config_parameter'
  8. CTX_NO_CHECK = 'icp_no_check'
  9. class IrConfigParameter(models.Model):
  10. _inherit = 'ir.config_parameter'
  11. def get_param(self, cr, uid, key, default=False, context=None):
  12. value = super(IrConfigParameter, self).get_param(
  13. cr, uid, key, default=None, context=context)
  14. if serv_config.has_option(SECTION, key):
  15. cvalue = serv_config.get(SECTION, key)
  16. if cvalue != value:
  17. # we write in db on first access;
  18. # should we have preloaded values in database at,
  19. # server startup, modules loading their parameters
  20. # from data files would break on unique key error.
  21. self.set_param(
  22. cr, SUPERUSER_ID, key, cvalue,
  23. context={CTX_NO_CHECK: True})
  24. value = cvalue
  25. if value is None:
  26. return default
  27. return value
  28. def _check_not_in_config(self, keys):
  29. if self.env.context.get(CTX_NO_CHECK):
  30. return
  31. if not serv_config.has_section(SECTION):
  32. return
  33. config_icp_keys = set(serv_config.options(SECTION)) & set(keys)
  34. if config_icp_keys:
  35. raise UserError(_("System Parameter(s) %s is/are defined "
  36. "in server_environment_files.") %
  37. (config_icp_keys, ))
  38. @api.model
  39. def create(self, vals):
  40. self._check_not_in_config([vals.get('key')])
  41. return super(IrConfigParameter, self).create(vals)
  42. @api.multi
  43. def write(self, vals):
  44. self._check_not_in_config(self.mapped('key'))
  45. return super(IrConfigParameter, self).write(vals)
  46. @api.multi
  47. def unlink(self):
  48. self._check_not_in_config(self.mapped('key'))
  49. return super(IrConfigParameter, self).unlink()