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.

101 lines
4.0 KiB

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 IrModelField(orm.Model):
  26. _inherit = 'ir.model.field'
  27. _columns = {
  28. 'regex_validator': fields.char(
  29. 'Validator', size=256,
  30. help="Regular expression used to validate the field. For example, "
  31. "you can add the expression\n%s\nto the email field"
  32. % r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'),
  33. }
  34. def check_vals(self, cr, uid, vals, model, context=None):
  35. field_ids = self.search(cr, uid, [
  36. ('model', '=', model), ('regex_validator', '!=', False)
  37. ], context=context)
  38. for field in self.browse(cr, uid, field_ids, context=context):
  39. if field.name in vals:
  40. p = re.compile(field.regex_validator)
  41. if not p.match(vals[field.name]):
  42. raise orm.except_orm(
  43. _('Error'),
  44. _('Expression %s not passed for %s') % (
  45. field.regex_validator, vals[field.name]))
  46. return True
  47. def _wrap_create(self, old_create, model):
  48. def wrapper(cr, uid, vals, context=None, **kwargs):
  49. self.check_vals(cr, uid, vals, model, context=context)
  50. new_id = old_create(cr, uid, vals, context=context, **kwargs)
  51. return new_id
  52. return wrapper
  53. def _wrap_write(self, old_write, model):
  54. def wrapper(cr, uid, ids, vals, context=None, **kwargs):
  55. self.check_vals(cr, uid, vals, model, context=context)
  56. old_write(cr, uid, ids, vals, context=context, **kwargs)
  57. return True
  58. return wrapper
  59. def _register_hook(self, cr, ids=None):
  60. """ Wrap the methods `create` and `write` of the model
  61. """
  62. updated = False
  63. for field in self.browse(cr, SUPERUSER_ID, ids):
  64. model = field.model_id.model
  65. model_obj = self.pool.get(model)
  66. if not hasattr(model_obj, 'field_validator_checked'):
  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(IrModelField, self).create(
  75. cr, uid, vals, context=context)
  76. if vals.get('regex_validator'):
  77. if self._register_hook(cr, [res_id]):
  78. openerp.modules.registry.RegistryManager.\
  79. signal_registry_change(cr.dbname)
  80. return res_id
  81. def write(self, cr, uid, ids, vals, context=None):
  82. if isinstance(ids, (int, long)):
  83. ids = [ids]
  84. super(IrModelField, self).write(cr, uid, ids, vals, context=context)
  85. if vals.get('regex_validator'):
  86. if self._register_hook(cr, ids):
  87. openerp.modules.registry.RegistryManager.\
  88. signal_registry_change(cr.dbname)
  89. return True