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.

95 lines
3.6 KiB

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