Browse Source

REF using _patch_method

pull/246/head
eLBati 9 years ago
parent
commit
d436897d9a
  1. 49
      base_field_validator/models/ir_model.py
  2. 2
      base_field_validator/test/validator.yml

49
base_field_validator/models/ir_model.py

@ -7,7 +7,6 @@
import re import re
from openerp.tools.translate import _ from openerp.tools.translate import _
import openerp
from openerp import SUPERUSER_ID from openerp import SUPERUSER_ID
from openerp import models, fields, api, _ from openerp import models, fields, api, _
@ -41,26 +40,28 @@ class IrModel(orm.Model):
_inherit = 'ir.model' _inherit = 'ir.model'
def _wrap_create(self, old_create, model_name):
def wrapper(cr, uid, vals, context=None, **kwargs):
self.check_vals(cr, uid, vals, model_name, context=context)
new_id = old_create(cr, uid, vals, context=context, **kwargs)
return new_id
return wrapper
def _field_validator_hook(self, cr, ids):
def _wrap_write(self, old_write, model_name):
def wrapper(cr, uid, ids, vals, context=None, **kwargs):
self.check_vals(cr, uid, vals, model_name, context=context)
res = old_write(cr, uid, ids, vals, context=context, **kwargs)
def _wrap_create():
def create(self, cr, uid, vals, context=None, **kwargs):
model_pool = self.pool['ir.model']
model_pool.check_vals(
cr, uid, vals, self._name, context=context)
new_id = create.origin(
self, cr, uid, vals, context=context, **kwargs)
return new_id
return create
def _wrap_write():
def write(self, cr, uid, ids, vals, context=None, **kwargs):
model_pool = self.pool['ir.model']
model_pool.check_vals(
cr, uid, vals, self._name, context=context)
res = write.origin(
self, cr, uid, ids, vals, context=context, **kwargs)
return res return res
return write
return wrapper
def _field_validator_hook(self, cr, ids):
""" Wrap the methods `create` and `write` of the model
"""
updated = False
for model in self.browse(cr, SUPERUSER_ID, ids): for model in self.browse(cr, SUPERUSER_ID, ids):
if model.validator_line_ids: if model.validator_line_ids:
model_name = model.model model_name = model.model
@ -68,18 +69,12 @@ class IrModel(orm.Model):
if model_obj and not hasattr( if model_obj and not hasattr(
model_obj, 'field_validator_checked' model_obj, 'field_validator_checked'
): ):
model_obj.create = self._wrap_create(
model_obj.create, model_name)
model_obj.write = self._wrap_write(
model_obj.write, model_name)
model_obj._patch_method('create', _wrap_create())
model_obj._patch_method('write', _wrap_write())
model_obj.field_validator_checked = True model_obj.field_validator_checked = True
updated = True
if updated:
openerp.modules.registry.RegistryManager.\
signal_registry_change(cr.dbname)
return True return True
def _register_hook(self, cr, ids=None):
def _register_hook(self, cr):
self._field_validator_hook(cr, self.search(cr, SUPERUSER_ID, [])) self._field_validator_hook(cr, self.search(cr, SUPERUSER_ID, []))
return super(IrModel, self)._register_hook(cr) return super(IrModel, self)._register_hook(cr)

2
base_field_validator/test/validator.yml

@ -5,7 +5,7 @@
self.write(cr, uid, [ref('base.model_res_partner')], { self.write(cr, uid, [ref('base.model_res_partner')], {
'validator_line_ids': [(0,0, 'validator_line_ids': [(0,0,
{ {
'name': ref('base.model_res_partner'),
'model_id': ref('base.model_res_partner'),
'field_id': ref('base.field_res_partner_email'), 'field_id': ref('base.field_res_partner_email'),
'regex_id': ref('regex_mail'), 'regex_id': ref('regex_mail'),
})], })],

Loading…
Cancel
Save