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.

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