[IMP] ir_model_data: convert assert to sql constraint + exception

[IMP] the <delete> tag doesn't crash if the referenced id does not exists

bzr revid: christophe@tinyerp.com-20090629124045-ycw89fo3lem0xxlt
This commit is contained in:
Christophe Simonis 2009-06-29 14:40:45 +02:00
parent 6be08e1808
commit c7bb1da092
2 changed files with 13 additions and 4 deletions

View File

@ -408,6 +408,9 @@ class ir_model_data(osv.osv):
'date_update': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
'noupdate': lambda *a: False
}
_sql_constraints = [
('module_name_uniq', 'unique(name, module)', 'You can not have multiple records with the same id for the same module'),
]
def __init__(self, pool, cr):
osv.osv.__init__(self, pool, cr)
@ -415,11 +418,13 @@ class ir_model_data(osv.osv):
self.doinit = True
self.unlink_mark = {}
def _get_id(self,cr, uid, module, xml_id):
@tools.cache()
def _get_id(self, cr, uid, module, xml_id):
ids = self.search(cr, uid, [('module','=',module),('name','=', xml_id)])
assert len(ids)==1, '%d reference(s) to %s.%s. You should have one and only one !' % (len(ids), module, xml_id)
if not ids:
raise Exception('No references to %s.%s' % (module, xml_id))
# the sql constraints ensure us we have only one result
return ids[0]
_get_id = tools.cache(skiparg=2)(_get_id)
def _update_dummy(self,cr, uid, model, module, xml_id=False, store=True):
if not xml_id:

View File

@ -256,7 +256,11 @@ form: module.record_id""" % (xml_id,)
if len(d_search):
ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
if len(d_id):
ids.append(self.id_get(cr, d_model, d_id))
try:
ids.append(self.id_get(cr, d_model, d_id))
except:
# d_id cannot be found. doesn't matter in this case
pass
if len(ids):
self.pool.get(d_model).unlink(cr, self.uid, ids)
self.pool.get('ir.model.data')._unlink(cr, self.uid, d_model, ids, direct=True)