Browse Source

[IMP] base_location: name_search improvement (#585)

pull/638/head
Carli Samuele 7 years ago
committed by Pedro M. Baeza
parent
commit
3944b9081e
  1. 8
      base_location/models/better_zip.py
  2. 60
      base_location/tests/test_base_location.py

8
base_location/models/better_zip.py

@ -51,6 +51,14 @@ class BetterZip(models.Model):
result.append((rec.id, ", ".join(name))) result.append((rec.id, ", ".join(name)))
return result return result
@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
args = list(args or [])
args += ['|', ('city', operator, name),
'|', ('name', operator, name), ('code', operator, name)]
recs = self.search(args, limit=limit)
return recs.name_get()
@api.onchange('country_id') @api.onchange('country_id')
def _onchange_country_id(self): def _onchange_country_id(self):
if self.state_id.country_id != self.country_id: if self.state_id.country_id != self.country_id:

60
base_location/tests/test_base_location.py

@ -231,6 +231,66 @@ class TestBaseLocation(TransactionCase):
).name ).name
) )
def test_name_search___can_find_using_city_name_or_zip_or_code(self):
barcelona_data = {
'city_id': self.city_bcn.id,
'city': self.city_bcn.name,
'name': '444',
'code': 'BA',
'state_id': self.state_bcn.id,
'country_id': self.ref('base.es'),
}
madrid_data = {
'city_id': self.city_madrid.id,
'city': self.city_madrid.name,
'name': '555',
'code': 'MD',
'state_id': self.state_madrid.id,
'country_id': self.ref('base.es'),
}
lausanne_data = {
'city_id': self.city_lausanne.id,
'city': self.city_lausanne.name,
'name': '666',
'code': 'LA',
'state_id': self.state_vd.id,
'country_id': self.ref('base.ch'),
}
barcelona = self.env['res.better.zip'].create(barcelona_data)
madrid = self.env['res.better.zip'].create(madrid_data)
lausanne = self.env['res.better.zip'].create(lausanne_data)
found_recs = self.env['res.better.zip'].name_search(name='444')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], barcelona.id)
found_recs = self.env['res.better.zip'].name_search(name='Barcelona')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], barcelona.id)
found_recs = self.env['res.better.zip'].name_search(name='BA')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], barcelona.id)
found_recs = self.env['res.better.zip'].name_search(name='555')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], madrid.id)
found_recs = self.env['res.better.zip'].name_search(name='Madrid')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], madrid.id)
found_recs = self.env['res.better.zip'].name_search(name='MD')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], madrid.id)
found_recs = self.env['res.better.zip'].name_search(name='666')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], lausanne.id)
found_recs = self.env['res.better.zip'].name_search(name='Lausanne')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], lausanne.id)
found_recs = self.env['res.better.zip'].name_search(name='LA')
self.assertEqual(len(found_recs), 1)
self.assertEqual(found_recs[0][0], lausanne.id)
def setUp(self): def setUp(self):
super(TestBaseLocation, self).setUp() super(TestBaseLocation, self).setUp()
self.state_vd = self.env['res.country.state'].create({ self.state_vd = self.env['res.country.state'].create({

Loading…
Cancel
Save