diff --git a/bin/osv/fields.py b/bin/osv/fields.py index e3c2be5b711..724077aadfa 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -35,9 +35,9 @@ import datetime as DT import string import netsvc import sys +import warnings from psycopg2 import Binary -import warnings import tools from tools.translate import _ @@ -538,34 +538,46 @@ class many2many(_column): return res for id in ids: res[id] = [] - limit_str = self._limit is not None and ' limit %d' % self._limit or '' + if offset: + warnings.warn("Specifying offset at a many2many.get() may produce unpredictable results.", + DeprecationWarning, stacklevel=2) obj = obj.pool.get(self._obj) - 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 = '' + wquery = obj._where_calc(cr, user, self._domain, context=context) + obj._apply_ir_rules(cr, user, wquery, 'read', context=context) + from_c, where_c, where_params = wquery.get_sql() + if where_c: + where_c = ' AND ' + where_c + + if offset or self._limit: + order_by = ' ORDER BY "%s".%s' %(obj._table, obj._order.split(',')[0]) + else: + order_by = '' + + limit_str = '' + if self._limit is not None: + limit_str = ' LIMIT %d' % self._limit + query = 'SELECT %(rel)s.%(id2)s, %(rel)s.%(id1)s \ - FROM %(rel)s, %(tbl)s \ - WHERE %(rel)s.%(id1)s in %%s \ + FROM %(rel)s, %(from_c)s \ + WHERE %(rel)s.%(id1)s IN %%s \ AND %(rel)s.%(id2)s = %(tbl)s.id \ - %(d1)s \ + %(where_c)s \ + %(order_by)s \ %(limit)s \ - ORDER BY %(tbl)s.%(order)s \ OFFSET %(offset)d' \ % {'rel': self._rel, + 'from_c': from_c, 'tbl': obj._table, 'id1': self._id1, 'id2': self._id2, - 'd1': d1, + 'where_c': where_c, 'limit': limit_str, - 'order': obj._order, + 'order_by': order_by, 'offset': offset, } - cr.execute(query, [tuple(ids)] + d2) + cr.execute(query, [tuple(ids),] + where_params) for r in cr.fetchall(): - if r[0] not in res[r[1]]: - ids2 = obj.search(cr, user, self._domain + [('id', '=', r[0])], context=context) - if ids2:res[r[1]] += ids2 + res[r[1]].append(r[0]) return res def set(self, cr, obj, id, name, values, user=None, context=None):