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.

105 lines
3.8 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. import openerp
  10. from openerp import SUPERUSER_ID
  11. from openerp import models, fields, api, _
  12. from openerp.exceptions import Warning
  13. from openerp.osv import orm
  14. class IrModel(models.Model):
  15. _inherit = 'ir.model'
  16. validator_line_ids = fields.One2many(
  17. 'ir.model.validator.line', 'name', 'Validators')
  18. @api.model
  19. def check_vals(self, vals, model_name):
  20. validator_lines = self.env['ir.model.validator.line'].search([
  21. ('name.model', '=', model_name),
  22. ('field_id.name', 'in', vals.keys())])
  23. for validator_line in validator_lines:
  24. pattern = re.compile(validator_line.regex_id.regex)
  25. if vals[validator_line.field_id.name]:
  26. if not pattern.match(vals[validator_line.field_id.name]):
  27. raise Warning(
  28. _('Expression %s not valid for %s') % (
  29. validator_line.regex_id.regex,
  30. vals[validator_line.field_id.name]))
  31. return True
  32. class IrModel(orm.Model):
  33. _inherit = 'ir.model'
  34. def _wrap_create(self, old_create, model_name):
  35. def wrapper(cr, uid, vals, context=None, **kwargs):
  36. self.check_vals(cr, uid, vals, model_name, context=context)
  37. new_id = old_create(cr, uid, vals, context=context, **kwargs)
  38. return new_id
  39. return wrapper
  40. def _wrap_write(self, old_write, model_name):
  41. def wrapper(cr, uid, ids, vals, context=None, **kwargs):
  42. self.check_vals(cr, uid, vals, model_name, context=context)
  43. res = old_write(cr, uid, ids, vals, context=context, **kwargs)
  44. return res
  45. return wrapper
  46. def _register_hook(self, cr, ids=None):
  47. """ Wrap the methods `create` and `write` of the model
  48. """
  49. res = super(IrModel, self)._register_hook(cr)
  50. if ids is None:
  51. ids = self.search(
  52. cr, SUPERUSER_ID, [('validator_line_ids', '!=', False)])
  53. updated = False
  54. for model in self.browse(cr, SUPERUSER_ID, ids):
  55. if model.validator_line_ids:
  56. model_name = model.model
  57. model_obj = self.pool.get(model_name)
  58. if model_obj and not hasattr(
  59. model_obj, 'field_validator_checked'
  60. ):
  61. model_obj.create = self._wrap_create(
  62. model_obj.create, model_name)
  63. model_obj.write = self._wrap_write(
  64. model_obj.write, model_name)
  65. model_obj.field_validator_checked = True
  66. updated = True
  67. if updated:
  68. openerp.modules.registry.RegistryManager.\
  69. signal_registry_change(cr.dbname)
  70. return res
  71. def create(self, cr, uid, vals, context=None):
  72. res_id = super(IrModel, self).create(
  73. cr, uid, vals, context=context)
  74. self._register_hook(cr, [res_id])
  75. return res_id
  76. def write(self, cr, uid, ids, vals, context=None):
  77. if isinstance(ids, (int, long)):
  78. ids = [ids]
  79. res = super(IrModel, self).write(cr, uid, ids, vals, context=context)
  80. self._register_hook(cr, ids)
  81. return res
  82. class IrModelValidatorLine(models.Model):
  83. _name = "ir.model.validator.line"
  84. name = fields.Many2one('ir.model', string="Model", required=True)
  85. field_id = fields.Many2one('ir.model.fields', 'Field', required=True)
  86. regex_id = fields.Many2one(
  87. 'ir.model.fields.regex', string="Validator", required=True)