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.

64 lines
2.3 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 not cvalue:
  17. raise UserError(_("Key %s is empty in "
  18. "server_environment_file") %
  19. (key, ))
  20. if cvalue != value:
  21. # we write in db on first access;
  22. # should we have preloaded values in database at,
  23. # server startup, modules loading their parameters
  24. # from data files would break on unique key error.
  25. self.set_param(
  26. cr, SUPERUSER_ID, key, cvalue,
  27. context={CTX_NO_CHECK: True})
  28. value = cvalue
  29. if value is None:
  30. return default
  31. return value
  32. def _check_not_in_config(self, keys):
  33. if self.env.context.get(CTX_NO_CHECK):
  34. return
  35. if not serv_config.has_section(SECTION):
  36. return
  37. config_icp_keys = set(serv_config.options(SECTION)) & set(keys)
  38. if config_icp_keys:
  39. raise UserError(_("System Parameter(s) %s is/are defined "
  40. "in server_environment_files.") %
  41. (config_icp_keys, ))
  42. @api.model
  43. def create(self, vals):
  44. self._check_not_in_config([vals.get('key')])
  45. return super(IrConfigParameter, self).create(vals)
  46. @api.multi
  47. def write(self, vals):
  48. self._check_not_in_config(self.mapped('key'))
  49. return super(IrConfigParameter, self).write(vals)
  50. @api.multi
  51. def unlink(self):
  52. self._check_not_in_config(self.mapped('key'))
  53. return super(IrConfigParameter, self).unlink()