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.

90 lines
3.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import re
  5. from openerp import SUPERUSER_ID, models, fields, api, _
  6. from openerp.exceptions import Warning
  7. class IrModel(models.Model):
  8. _inherit = 'ir.model'
  9. validator_line_ids = fields.One2many(
  10. 'ir.model.validator.line', 'model_id', 'Validators')
  11. @api.model
  12. def check_vals(self, vals, model_name):
  13. validator_lines = self.env['ir.model.validator.line'].search([
  14. ('model_id.model', '=', model_name),
  15. ('field_id.name', 'in', vals.keys())])
  16. for validator_line in validator_lines:
  17. pattern = re.compile(validator_line.regex_id.regex)
  18. if vals[validator_line.field_id.name]:
  19. if not pattern.match(vals[validator_line.field_id.name]):
  20. raise Warning(
  21. _('Expression %s not valid for %s') % (
  22. validator_line.regex_id.regex,
  23. vals[validator_line.field_id.name]))
  24. return True
  25. def _field_validator_hook(self, cr, ids):
  26. def _wrap_create():
  27. def create(self, cr, uid, vals, context=None, **kwargs):
  28. model_pool = self.pool['ir.model']
  29. model_pool.check_vals(
  30. cr, uid, vals, self._name, context=context)
  31. new_id = create.origin(
  32. self, cr, uid, vals, context=context, **kwargs)
  33. return new_id
  34. return create
  35. def _wrap_write():
  36. def write(self, cr, uid, ids, vals, context=None, **kwargs):
  37. model_pool = self.pool['ir.model']
  38. model_pool.check_vals(
  39. cr, uid, vals, self._name, context=context)
  40. res = write.origin(
  41. self, cr, uid, ids, vals, context=context, **kwargs)
  42. return res
  43. return write
  44. for model in self.browse(cr, SUPERUSER_ID, ids):
  45. if model.validator_line_ids:
  46. model_name = model.model
  47. model_obj = self.pool.get(model_name)
  48. if model_obj and not hasattr(
  49. model_obj, 'field_validator_checked'
  50. ):
  51. model_obj._patch_method('create', _wrap_create())
  52. model_obj._patch_method('write', _wrap_write())
  53. model_obj.field_validator_checked = True
  54. return True
  55. def _register_hook(self, cr):
  56. self._field_validator_hook(cr, self.search(cr, SUPERUSER_ID, []))
  57. return super(IrModel, self)._register_hook(cr)
  58. def create(self, cr, uid, vals, context=None):
  59. res_id = super(IrModel, self).create(
  60. cr, uid, vals, context=context)
  61. self._field_validator_hook(cr, [res_id])
  62. return res_id
  63. def write(self, cr, uid, ids, vals, context=None):
  64. if isinstance(ids, (int, long)):
  65. ids = [ids]
  66. res = super(IrModel, self).write(cr, uid, ids, vals, context=context)
  67. self._field_validator_hook(cr, ids)
  68. return res
  69. class IrModelValidatorLine(models.Model):
  70. _name = "ir.model.validator.line"
  71. _rec_name = 'model_id'
  72. model_id = fields.Many2one('ir.model', string="Model", required=True)
  73. field_id = fields.Many2one('ir.model.fields', 'Field', required=True)
  74. regex_id = fields.Many2one(
  75. 'ir.model.fields.regex', string="Validator", required=True)