[FIX] search: searching fields.property not set

When searching if a many2one property field is not set, there may be less
results since only the ones with a reference set to NULL are returned.
We should also get those not in the table.

This commit change this case so instead of returning ['id', 'in', {matching non-set ids}],
the ['id', 'not in', {matching set ids}] is returned.

e.g: if (1, 3, 8) are set, (5, 9) are not set. ['id', 'not in', (1, 3, 8)] would
be returned instead of ['id', 'in', (5, 9)] which might not select all non-set
property fields.

closes #6044
opw-631057
This commit is contained in:
Nicolas Lempereur 2015-03-30 12:14:28 +02:00
parent 1288536c03
commit dd88f3c82f
2 changed files with 23 additions and 2 deletions

View File

@ -12,6 +12,8 @@ class TestSearch(TransactionCase):
cr, uid = self.cr, self.uid
self.account_model = self.registry('account.account')
self.account_type_model = self.registry('account.account.type')
self.res_partner_model = self.registry('res.partner')
self.account_payment_term_model = self.registry('account.payment.term')
ac_ids = self.account_type_model.search(cr, uid, [], limit=1)
self.atax = (int(self.account_model.create(cr, uid, dict(
name="Tax Received",
@ -33,6 +35,9 @@ class TestSearch(TransactionCase):
self.all_ids = [self.atax[0], self.apurchase[0], self.asale[0]]
self.a_partner = self.res_partner_model.create(cr, uid, {'name':'test partner'})
self.a_payment_term = self.account_payment_term_model.create(cr, uid, {'name':'test payment term'})
def test_name_search(self):
cr, uid = self.cr, self.uid
atax_ids = self.account_model.name_search(cr, uid, name="Tax", operator='ilike', args=[('id', 'in', self.all_ids)])
@ -58,3 +63,13 @@ class TestSearch(TransactionCase):
asale_ids = self.account_model.name_search(cr, uid, name='Product Sales', operator='not ilike', args=[('id', 'in', self.all_ids)])
self.assertEqual(set([self.atax[0], self.apurchase[0]]), set([a[0] for a in asale_ids]), "name_search 'not ilike Product Sales' should have returned all but Product Sales account")
def test_property_unset_search(self):
cr, uid = self.cr, self.uid
partner_ids = self.res_partner_model.search(cr, uid, [('property_payment_term', '=', False), ('id', '=', self.a_partner)])
self.assertTrue(partner_ids, "unset property field 'propety_payment_term' should have been found")
self.res_partner_model.write(cr, uid, [self.a_partner], {'property_payment_term': self.a_payment_term})
partner_ids = self.res_partner_model.search(cr, uid, [('property_payment_term', '=', False), ('id', '=', self.a_partner)])
self.assertFalse(partner_ids, "set property field 'propety_payment_term' should not have been found")

View File

@ -259,12 +259,19 @@ class ir_property(osv.osv):
@api.model
def search_multi(self, name, model, operator, value):
""" Return a domain for the records that match the given condition. """
default_matches = False
field = self.env[model]._fields[name]
if field.type == 'many2one':
comodel = field.comodel_name
def makeref(value):
return value and '%s,%s' % (comodel, value)
if operator in ('=', '!=', '<=', '<', '>', '>='):
if operator == "=":
value = makeref(value)
# if searching properties not set, search those not in those set
if value is False:
default_matches = True
elif operator in ('!=', '<=', '<', '>', '>='):
value = makeref(value)
elif operator in ('in', 'not in'):
value = map(makeref, value)
@ -283,7 +290,6 @@ class ir_property(osv.osv):
# retrieve the records corresponding to the properties that match
good_ids = []
default_matches = False
for prop in props:
if prop.res_id:
res_model, res_id = prop.res_id.split(',')