1
# -*- coding: utf-8 -*-
3
from osv import fields, osv
7
class many2many_sorted(fields.many2many):
8
def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
9
super(many2many_sorted, self).__init__(obj, rel, id1, id2, string, limit, **args)
11
def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
22
warnings.warn("Specifying offset at a many2many.get() may produce unpredictable results.",
23
DeprecationWarning, stacklevel=2)
24
obj = obj.pool.get(self._obj)
26
# static domains are lists, and are evaluated both here and on client-side, while string
27
# domains supposed by dynamic and evaluated on client-side only (thus ignored here)
28
# FIXME: make this distinction explicit in API!
29
domain = isinstance(self._domain, list) and self._domain or []
31
wquery = obj._where_calc(cr, user, domain, context=context)
32
obj._apply_ir_rules(cr, user, wquery, 'read', context=context)
33
from_c, where_c, where_params = wquery.get_sql()
35
where_c = ' AND ' + where_c
39
order_by = ' ORDER BY '
41
for order in obj._order.split(','):
42
order_tab.append('%s.%s' %(from_c, order.strip()))
43
order_by += ','.join(order_tab)
46
if self._limit is not None:
47
limit_str = ' LIMIT %d' % self._limit
49
query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \
50
FROM %(rel)s, %(from_c)s \
51
WHERE %(rel)s.%(id1)s IN %%s \
52
AND %(rel)s.%(id2)s = %(tbl)s.id \
67
cr.execute(query, [tuple(ids),] + where_params)
68
for r in cr.fetchall():
69
res[r[1]].append(r[0])
73
class many2many_notlazy(many2many_sorted):
74
def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
75
super(many2many_notlazy, self).__init__(obj, rel, id1, id2, string, limit, **args)
77
def set(self, cr, obj, id, name, values, user=None, context=None):
82
obj = obj.pool.get(self._obj)
85
if not (isinstance(act, list) or isinstance(act, tuple)) or not act:
88
d1, d2,tables = obj.pool.get('ir.rule').domain_get(cr, user, obj._name, context=context)
90
d1 = ' and ' + ' and '.join(d1)
97
args.append(tuple(act[2]))
98
cr.execute('delete from '+self._rel+' where '+self._id1+'=%s AND '+self._id2+' IN (SELECT '+self._rel+'.'+self._id2+' FROM '+self._rel+', '+','.join(tables)+' WHERE '+self._rel+'.'+self._id1+'=%s AND '+self._rel+'.'+self._id2+' = '+obj._table+'.id '+ d1 +' and '+self._rel+'.'+self._id2+' not in %s)', args)
100
cr.execute('select '+self._id2+' from '+self._rel+' where '+self._id1+'=%s', [id, ])
101
existing = [x[0] for x in cr.fetchall()]
103
for act_nbr in act[2]:
104
if act_nbr not in existing:
105
if self._rel == 'account_destination_link':
106
link_obj = pooler.get_pool(cr.dbname).get('account.destination.link')
107
link_obj.create(cr, user, {self._id1: id, self._id2: act_nbr})
109
cr.execute('insert into '+self._rel+' ('+self._id1+','+self._id2+') values (%s, %s)', (id, act_nbr))
114
return super(many2many_notlazy, self).set(cr, obj, id, name, newargs, user=user, context=context)