[FIX] orm: correct the cleaning of incorrect reference fields

bzr revid: chs@openerp.com-20100416144856-gh9yaulzc4bc32zv
This commit is contained in:
Christophe Simonis 2010-04-16 16:48:56 +02:00
parent 83f0ddf1d0
commit 5b146e8062
1 changed files with 19 additions and 14 deletions

View File

@ -2638,24 +2638,29 @@ class orm(orm_template):
select = ids
select = map(lambda x: isinstance(x,dict) and x['id'] or x, select)
result = self._read_flat(cr, user, select, fields, context, load)
for r in result:
for key, v in r.items():
if v == None:
r[key] = False
if key in self._columns.keys():
type = self._columns[key]._type
elif key in self._inherit_fields.keys():
type = self._inherit_fields[key][2]._type
if key in self._columns:
column = self._columns[key]
elif key in self._inherit_fields:
column = self._inherit_fields[key][2]
else:
continue
if type == 'reference' and v:
model,ref_id = v.split(',')
table = self.pool.get(model)._table
cr.execute('select id from "%s" where id=%s' % (table,ref_id))
id_exist = cr.fetchone()
if not id_exist:
cr.execute('update "'+self._table+'" set "'+key+'"=NULL where "%s"=%s' %(key,''.join("'"+str(v)+"'")))
r[key] = ''
if v and column._type == 'reference':
model_name, ref_id = v.split(',', 1)
model = self.pool.get(model_name)
if not model:
reset = True
else:
cr.execute('SELECT count(1) FROM "%s" WHERE id=%%s' % (model._table,), (ref_id,))
reset = not cr.fetchone()[0]
if reset:
if column._classic_write:
query = 'UPDATE "%s" SET "%s"=NULL WHERE id=%%s' % (self._table, key)
cr.execute(query, (r['id'],))
r[key] = False
if isinstance(ids, (int, long, dict)):
return result and result[0] or False
return result