[IMP] base: code aestetic, res_country & state

[FIX] base: country & state, enforce the max number of records to the requested limit
This commit is contained in:
Fabien Pinckaers
2016-12-30 01:38:42 -08:00
parent 655c70319d
commit b70fe586f2
+21 -21
View File
@@ -9,23 +9,6 @@ from odoo.tools.translate import _
_logger = logging.getLogger(__name__)
@api.model
def location_name_search(self, name='', args=None, operator='ilike', limit=100):
if args is None:
args = []
records = self.browse()
if len(name) == 2:
records = self.search([('code', 'ilike', name)] + args, limit=limit)
search_domain = [('name', operator, name)]
if records:
search_domain.append(('id', 'not in', records.ids))
records += self.search(search_domain + args, limit=limit)
# the field 'display_name' calls name_get() to get its value
return [(record.id, record.display_name) for record in records]
class Country(models.Model):
_name = 'res.country'
@@ -67,7 +50,23 @@ class Country(models.Model):
'The code of the country must be unique !')
]
name_search = location_name_search
@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
if args is None:
args = []
records = self.browse()
if len(name) == 2:
records = self.search([('code', '=ilike', name)] + args, limit=limit)
search_domain = [('name', operator, name)]
if records:
search_domain.append(('id', 'not in', records.ids))
records += self.search(search_domain + args, limit=limit-len(records))
# the field 'display_name' calls name_get() to get its value
return [(record.id, record.display_name) for record in records]
@api.model
def create(self, vals):
@@ -115,11 +114,12 @@ class CountryState(models.Model):
if args is None:
args = []
if self.env.context.get('country_id'):
args = args + [('country_id', '=', self.env.context.get('country_id'))]
args = args + [('country_id', '=', self.env.context['country_id'])]
firsts_records = self.search([('code', '=ilike', name)] + args, limit=limit)
search_domain = [('name', operator, name)]
search_domain.append(('id', 'not in', firsts_records.ids))
records = firsts_records + self.search(search_domain + args, limit=limit)
if len(firsts_records):
search_domain.append(('id', 'not in', firsts_records.ids))
records = firsts_records + self.search(search_domain + args, limit=limit-len(firsts_records))
return [(record.id, record.display_name) for record in records]