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.

111 lines
4.3 KiB

9 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2014 Agile Business Group sagl (<http://www.agilebg.com>)
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp.osv import orm, fields
  21. import re
  22. from openerp.tools.translate import _
  23. import openerp
  24. from openerp import SUPERUSER_ID
  25. class IrModel(orm.Model):
  26. _inherit = 'ir.model'
  27. _columns = {
  28. 'validator_line_ids': fields.one2many(
  29. 'ir.model.validator.line', 'name', 'Validators'),
  30. }
  31. def check_vals(self, cr, uid, vals, model, context=None):
  32. for validator_line in model.validator_line_ids:
  33. if validator_line.field_id.name in vals:
  34. pattern = re.compile(validator_line.regex_id.regex)
  35. if not pattern.match(vals[validator_line.field_id.name]):
  36. raise orm.except_orm(
  37. _('Error'),
  38. _('Expression %s not valid for %s') % (
  39. validator_line.regex_id.regex,
  40. vals[validator_line.field_id.name]))
  41. return True
  42. def _wrap_create(self, old_create, model):
  43. def wrapper(cr, uid, vals, context=None, **kwargs):
  44. self.check_vals(cr, uid, vals, model, context=context)
  45. new_id = old_create(cr, uid, vals, context=context, **kwargs)
  46. return new_id
  47. return wrapper
  48. def _wrap_write(self, old_write, model):
  49. def wrapper(cr, uid, ids, vals, context=None, **kwargs):
  50. self.check_vals(cr, uid, vals, model, context=context)
  51. old_write(cr, uid, ids, vals, context=context, **kwargs)
  52. return True
  53. return wrapper
  54. def _register_hook(self, cr, ids=None):
  55. """ Wrap the methods `create` and `write` of the model
  56. """
  57. if ids is None:
  58. ids = self.search(cr, SUPERUSER_ID, [])
  59. updated = False
  60. for model in self.browse(cr, SUPERUSER_ID, ids):
  61. if model.validator_line_ids:
  62. model_name = model.model
  63. model_obj = self.pool.get(model_name)
  64. if model_obj and not hasattr(
  65. model_obj, 'field_validator_checked'
  66. ):
  67. model_obj.create = self._wrap_create(
  68. model_obj.create, model)
  69. model_obj.write = self._wrap_write(model_obj.write, model)
  70. model_obj.field_validator_checked = True
  71. updated = True
  72. return updated
  73. def create(self, cr, uid, vals, context=None):
  74. res_id = super(IrModel, self).create(
  75. cr, uid, vals, context=context)
  76. if self._register_hook(cr, [res_id]):
  77. openerp.modules.registry.RegistryManager.\
  78. signal_registry_change(cr.dbname)
  79. return res_id
  80. def write(self, cr, uid, ids, vals, context=None):
  81. if isinstance(ids, (int, long)):
  82. ids = [ids]
  83. super(IrModel, self).write(cr, uid, ids, vals, context=context)
  84. if self._register_hook(cr, ids):
  85. openerp.modules.registry.RegistryManager.\
  86. signal_registry_change(cr.dbname)
  87. return True
  88. class IrModelValidatorLine(orm.Model):
  89. _name = "ir.model.validator.line"
  90. _columns = {
  91. 'name': fields.many2one('ir.model', string="Model", required=True),
  92. 'field_id': fields.many2one(
  93. 'ir.model.fields', 'Field', required=True),
  94. 'regex_id': fields.many2one(
  95. 'ir.model.fields.regex', string="Validator",
  96. required=True),
  97. }