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.

112 lines
4.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. res = old_write(cr, uid, ids, vals, context=context, **kwargs)
  52. return res
  53. return wrapper
  54. def _register_hook(self, cr, ids=None):
  55. """ Wrap the methods `create` and `write` of the model
  56. """
  57. res = super(IrModel, self)._register_hook(cr)
  58. if ids is None:
  59. ids = self.search(
  60. cr, SUPERUSER_ID, [('validator_line_ids', '!=', False)])
  61. updated = False
  62. for model in self.browse(cr, SUPERUSER_ID, ids):
  63. if model.validator_line_ids:
  64. model_name = model.model
  65. model_obj = self.pool.get(model_name)
  66. if model_obj and not hasattr(
  67. model_obj, 'field_validator_checked'
  68. ):
  69. model_obj.create = self._wrap_create(
  70. model_obj.create, model)
  71. model_obj.write = self._wrap_write(model_obj.write, model)
  72. model_obj.field_validator_checked = True
  73. updated = True
  74. if updated:
  75. openerp.modules.registry.RegistryManager.\
  76. signal_registry_change(cr.dbname)
  77. return res
  78. def create(self, cr, uid, vals, context=None):
  79. res_id = super(IrModel, self).create(
  80. cr, uid, vals, context=context)
  81. self._register_hook(cr, [res_id])
  82. return res_id
  83. def write(self, cr, uid, ids, vals, context=None):
  84. if isinstance(ids, (int, long)):
  85. ids = [ids]
  86. res = super(IrModel, self).write(cr, uid, ids, vals, context=context)
  87. self._register_hook(cr, ids)
  88. return res
  89. class IrModelValidatorLine(orm.Model):
  90. _name = "ir.model.validator.line"
  91. _columns = {
  92. 'name': fields.many2one('ir.model', string="Model", required=True),
  93. 'field_id': fields.many2one(
  94. 'ir.model.fields', 'Field', required=True),
  95. 'regex_id': fields.many2one(
  96. 'ir.model.fields.regex', string="Validator",
  97. required=True),
  98. }