From f1bbbaa5f84f774fa300506e8c1d6b492bd27d0b Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 19 Mar 2012 17:15:26 +0100 Subject: [PATCH 1/2] [FIX] res.country (and res.country.state) name_search so it returns all countries matching even when there's a country matching by code bzr revid: xmo@openerp.com-20120319161526-txd1eiomjrntgmxi --- openerp/addons/base/res/res_country.py | 30 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/openerp/addons/base/res/res_country.py b/openerp/addons/base/res/res_country.py index f240784dc47..3b7ac8a0db9 100644 --- a/openerp/addons/base/res/res_country.py +++ b/openerp/addons/base/res/res_country.py @@ -55,14 +55,18 @@ addresses belonging to this country.\n\nYou can use the python-style string pate args=[] if not context: context={} - ids = False + ids = [] if len(name) == 2: ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit, context=context) - if not ids: - ids = self.search(cr, user, [('name', operator, name)] + args, - limit=limit, context=context) - return self.name_get(cr, user, ids, context) + + search_domain = [('name', operator, name)] + if ids: + search_domain.append(('id', 'not in', ids)) + ids.extend(self.search(cr, user, search_domain + args, + limit=limit, context=context)) + countries = self.name_get(cr, user, ids, context) + return sorted(countries, key=lambda c: ids.index(c[0])) _order='name' def create(self, cursor, user, vals, context=None): @@ -96,12 +100,18 @@ class CountryState(osv.osv): args = [] if not context: context = {} - ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit, - context=context) - if not ids: - ids = self.search(cr, user, [('name', operator, name)] + args, + ids = [] + if len(name) == 2: + ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit, context=context) - return self.name_get(cr, user, ids, context) + + search_domain = [('name', operator, name)] + if ids: + search_domain.append(('id', 'not in', ids)) + ids.extend(self.search(cr, user, search_domain + args, + limit=limit, context=context)) + states = self.name_get(cr, user, ids, context) + return sorted(states, key=lambda c: ids.index(c[0])) _order = 'code' CountryState() From 35c0c368165c3202a66e9653042e27398779abb0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 19 Mar 2012 17:22:56 +0100 Subject: [PATCH 2/2] [REF] use single implementation for name_search of Country and CountryState bzr revid: xmo@openerp.com-20120319162256-2wcmges2o1gqpb3e --- openerp/addons/base/res/res_country.py | 62 +++++++++----------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/openerp/addons/base/res/res_country.py b/openerp/addons/base/res/res_country.py index 3b7ac8a0db9..c32302a0783 100644 --- a/openerp/addons/base/res/res_country.py +++ b/openerp/addons/base/res/res_country.py @@ -21,6 +21,23 @@ from osv import fields, osv +def location_name_search(self, cr, user, name='', args=None, operator='ilike', + context=None, limit=100): + if not args: + args = [] + + ids = [] + if len(name) == 2: + ids = self.search(cr, user, [('code', 'ilike', name)] + args, + limit=limit, context=context) + + search_domain = [('name', operator, name)] + if ids: search_domain.append(('id', 'not in', ids)) + ids.extend(self.search(cr, user, search_domain + args, + limit=limit, context=context)) + + locations = self.name_get(cr, user, ids, context) + return sorted(locations, key=lambda (id, name): ids.index(id)) class Country(osv.osv): _name = 'res.country' @@ -48,27 +65,10 @@ addresses belonging to this country.\n\nYou can use the python-style string pate _defaults = { 'address_format': "%(street)s\n%(street2)s\n%(city)s,%(state_code)s %(zip)s\n%(country_name)s", } - - def name_search(self, cr, user, name='', args=None, operator='ilike', - context=None, limit=100): - if not args: - args=[] - if not context: - context={} - ids = [] - if len(name) == 2: - ids = self.search(cr, user, [('code', 'ilike', name)] + args, - limit=limit, context=context) - - search_domain = [('name', operator, name)] - if ids: - search_domain.append(('id', 'not in', ids)) - ids.extend(self.search(cr, user, search_domain + args, - limit=limit, context=context)) - countries = self.name_get(cr, user, ids, context) - return sorted(countries, key=lambda c: ids.index(c[0])) _order='name' + name_search = location_name_search + def create(self, cursor, user, vals, context=None): if 'code' in vals: vals['code'] = vals['code'].upper() @@ -81,8 +81,6 @@ addresses belonging to this country.\n\nYou can use the python-style string pate return super(Country, self).write(cursor, user, ids, vals, context=context) -Country() - class CountryState(osv.osv): _description="Country state" @@ -94,29 +92,9 @@ class CountryState(osv.osv): 'code': fields.char('State Code', size=3, help='The state code in three chars.\n', required=True), } - def name_search(self, cr, user, name='', args=None, operator='ilike', - context=None, limit=100): - if not args: - args = [] - if not context: - context = {} - ids = [] - if len(name) == 2: - ids = self.search(cr, user, [('code', 'ilike', name)] + args, - limit=limit, context=context) - - search_domain = [('name', operator, name)] - if ids: - search_domain.append(('id', 'not in', ids)) - ids.extend(self.search(cr, user, search_domain + args, - limit=limit, context=context)) - states = self.name_get(cr, user, ids, context) - return sorted(states, key=lambda c: ids.index(c[0])) - _order = 'code' -CountryState() - + name_search = location_name_search # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: