Skip to content

Commit

Permalink
Merge remote-tracking branch 'odoo/7.0' into 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
OCA-git-bot committed Jan 8, 2016
2 parents 156e9b6 + 152d9de commit f3ae297
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions openerp/osv/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,25 @@ def set(self, cr, model, id, name, values, user=None, context=None):
return
rel, id1, id2 = self._sql_names(model)
obj = model.pool.get(self._obj)

def link(ids):
# beware of duplicates when inserting
query = """ INSERT INTO {rel} ({id1}, {id2})
(SELECT %s, unnest(%s)) EXCEPT (SELECT {id1}, {id2} FROM {rel} WHERE {id1}=%s)
""".format(rel=rel, id1=id1, id2=id2)
for sub_ids in cr.split_for_in_conditions(ids):
cr.execute(query, (id, list(sub_ids), id))

def unlink_all():
# remove all records for which user has access rights
clauses, params, tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
cond = " AND ".join(clauses) if clauses else "1=1"
query = """ DELETE FROM {rel} USING {tables}
WHERE {rel}.{id1}=%s AND {rel}.{id2}={table}.id AND {cond}
""".format(rel=rel, id1=id1, id2=id2,
table=obj._table, tables=','.join(tables), cond=cond)
cr.execute(query, [id] + params)

for act in values:
if not (isinstance(act, list) or isinstance(act, tuple)) or not act:
continue
Expand All @@ -777,23 +796,12 @@ def set(self, cr, model, id, name, values, user=None, context=None):
elif act[0] == 3:
cr.execute('delete from '+rel+' where ' + id1 + '=%s and '+ id2 + '=%s', (id, act[1]))
elif act[0] == 4:
# following queries are in the same transaction - so should be relatively safe
cr.execute('SELECT 1 FROM '+rel+' WHERE '+id1+' = %s and '+id2+' = %s', (id, act[1]))
if not cr.fetchone():
cr.execute('insert into '+rel+' ('+id1+','+id2+') values (%s,%s)', (id, act[1]))
link([act[1]])
elif act[0] == 5:
cr.execute('delete from '+rel+' where ' + id1 + ' = %s', (id,))
unlink_all()
elif act[0] == 6:

d1, d2,tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
if d1:
d1 = ' and ' + ' and '.join(d1)
else:
d1 = ''
cr.execute('delete from '+rel+' where '+id1+'=%s AND '+id2+' IN (SELECT '+rel+'.'+id2+' FROM '+rel+', '+','.join(tables)+' WHERE '+rel+'.'+id1+'=%s AND '+rel+'.'+id2+' = '+obj._table+'.id '+ d1 +')', [id, id]+d2)

for act_nbr in act[2]:
cr.execute('insert into '+rel+' ('+id1+','+id2+') values (%s, %s)', (id, act_nbr))
unlink_all()
link(act[2])

#
# TODO: use a name_search
Expand Down

0 comments on commit f3ae297

Please sign in to comment.