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.

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