[IMP] orm,base: added context to _constraints functions + use generic osv.osv.check_recursion()

lp bug: https://launchpad.net/bugs/525808 fixed

bzr revid: odo@openerp.com-20101209105733-j3uffwp4rl07nzc4
This commit is contained in:
Olivier Dony 2010-12-09 11:57:33 +01:00
parent c8f985e831
commit 9daa68e67c
7 changed files with 11 additions and 41 deletions

View File

@ -81,8 +81,8 @@ class ir_model(osv.osv):
'state': lambda self,cr,uid,ctx=None: (ctx and ctx.get('manual',False)) and 'manual' or 'base',
}
def _check_model_name(self, cr, uid, ids):
for model in self.browse(cr, uid, ids):
def _check_model_name(self, cr, uid, ids, context=None):
for model in self.browse(cr, uid, ids, context=context):
if model.state=='manual':
if not model.model.startswith('x_'):
return False

View File

@ -47,7 +47,7 @@ class ir_rule(osv.osv):
res[rule.id] = False
return res
def _check_model_obj(self, cr, uid, ids, context={}):
def _check_model_obj(self, cr, uid, ids, context=None):
return not any(isinstance(self.pool.get(rule.model_id.model), osv.osv_memory) for rule in self.browse(cr, uid, ids, context))
_columns = {

View File

@ -257,16 +257,6 @@ class ir_ui_menu(osv.osv):
return {}
return {'type': {'icon_pict': 'picture'}, 'value': {'icon_pict': ('stock', (icon,'ICON_SIZE_MENU'))}}
def _check_recursion(self, cr, uid, ids):
level = 100
while len(ids):
cr.execute('select distinct parent_id from ir_ui_menu where id IN %s',(tuple(ids),))
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True
def read_image(self, path):
path_info = path.split(',')
icon_path = addons.get_module_resource(path_info[0],path_info[1])
@ -318,7 +308,7 @@ class ir_ui_menu(osv.osv):
return _('Error ! You can not create recursive Menu.')
_constraints = [
(_check_recursion, _rec_message , ['parent_id'])
(osv.osv._check_recursion, _rec_message , ['parent_id'])
]
_defaults = {
'icon' : 'STOCK_OPEN',

View File

@ -28,7 +28,7 @@ import netsvc
import os
import logging
def _check_xml(self, cr, uid, ids, context={}):
def _check_xml(self, cr, uid, ids, context=None):
logger = logging.getLogger('init')
for view in self.browse(cr, uid, ids, context):
eview = etree.fromstring(view.arch.encode('utf8'))

View File

@ -51,16 +51,6 @@ class res_partner_category(osv.osv):
res = self.name_get(cr, uid, ids, context=context)
return dict(res)
def _check_recursion(self, cr, uid, ids):
level = 100
while len(ids):
cr.execute('select distinct parent_id from res_partner_category where id IN %s',(tuple(ids),))
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True
_description='Partner Categories'
_name = 'res.partner.category'
_columns = {
@ -71,7 +61,7 @@ class res_partner_category(osv.osv):
'active' : fields.boolean('Active', help="The active field allows you to hide the category without removing it."),
}
_constraints = [
(_check_recursion, 'Error ! You can not create recursive categories.', ['parent_id'])
(osv.osv._check_recursion, 'Error ! You can not create recursive categories.', ['parent_id'])
]
_defaults = {
'active' : lambda *a: 1,
@ -149,7 +139,7 @@ class res_partner(osv.osv):
def do_share(self, cr, uid, ids, *args):
return True
def _check_ean_key(self, cr, uid, ids):
def _check_ean_key(self, cr, uid, ids, context=None):
for partner_o in pooler.get_pool(cr.dbname).get('res.partner').read(cr, uid, ids, ['ean13',]):
thisean=partner_o['ean13']
if thisean and thisean!='':

View File

@ -184,16 +184,6 @@ class res_company(osv.osv):
except:
return False
def _check_recursion(self, cr, uid, ids):
level = 100
while len(ids):
cr.execute('select distinct parent_id from res_company where id IN %s',(tuple(ids),))
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True
def _get_logo(self, cr, uid, ids):
return open(os.path.join(
tools.config['root_path'], '..', 'pixmaps', 'openerp-header.png'),
@ -280,7 +270,7 @@ class res_company(osv.osv):
}
_constraints = [
(_check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
(osv.osv._check_recursion, 'Error! You can not create recursive companies.', ['parent_id'])
]
res_company()

View File

@ -4233,14 +4233,14 @@ class orm(orm_template):
cr.execute(query + "WHERE ID IN %s", (tuple(ids),))
return cr.fetchone()[0] == len(ids)
def check_recursion(self, cr, uid, ids, parent=None):
def check_recursion(self, cr, uid, ids, context=None, parent=None):
warnings.warn("You are using deprecated %s.check_recursion(). Please use the '_check_recursion()' instead!" % \
self._name, DeprecationWarning, stacklevel=3)
assert parent is None or parent in self._columns or parent in self._inherit_fields,\
"The 'parent' parameter passed to check_recursion() must be None or a valid field name"
return self._check_recursion(cr, uid, ids, parent)
return self._check_recursion(cr, uid, ids, context, parent)
def _check_recursion(self, cr, uid, ids, parent=None):
def _check_recursion(self, cr, uid, ids, context=None, parent=None):
"""
Verifies that there is no loop in a hierarchical structure of records,
by following the parent relationship using the **parent** field until a loop