Remove search call in address_get
For performance, it is better to use addresses attribute which is cached instead of doing a search request on each call. issue7092 review36961002
This commit is contained in:
@@ -227,18 +227,13 @@ class Party(ModelSQL, ModelView, MultiValueMixin):
|
||||
Try to find an address for the given type, if no type matches
|
||||
the first address is returned.
|
||||
"""
|
||||
Address = Pool().get("party.address")
|
||||
addresses = Address.search(
|
||||
[("party", "=", self.id), ("active", "=", True)],
|
||||
order=[('sequence', 'ASC'), ('id', 'ASC')])
|
||||
if not addresses:
|
||||
return None
|
||||
default_address = addresses[0]
|
||||
if not type:
|
||||
return default_address
|
||||
for address in addresses:
|
||||
if getattr(address, type):
|
||||
return address
|
||||
default_address = None
|
||||
if self.addresses:
|
||||
default_address = self.addresses[0]
|
||||
if type:
|
||||
for address in self.addresses:
|
||||
if getattr(address, type):
|
||||
return address
|
||||
return default_address
|
||||
|
||||
|
||||
|
||||
@@ -128,6 +128,78 @@ class PartyTestCase(ModuleTestCase):
|
||||
"Subdivision\n"
|
||||
"COUNTRY")
|
||||
|
||||
@with_transaction()
|
||||
def test_address_get_no_type(self):
|
||||
"Test address_get with no type"
|
||||
pool = Pool()
|
||||
Party = pool.get('party.party')
|
||||
Address = pool.get('party.address')
|
||||
party, = Party.create([{}])
|
||||
address1, address2 = Address.create([{
|
||||
'party': party.id,
|
||||
'sequence': 1,
|
||||
}, {
|
||||
'party': party.id,
|
||||
'sequence': 2,
|
||||
}])
|
||||
|
||||
address = party.address_get()
|
||||
|
||||
self.assertEqual(address, address1)
|
||||
|
||||
@with_transaction()
|
||||
def test_address_get_no_address(self):
|
||||
"Test address_get with no address"
|
||||
pool = Pool()
|
||||
Party = pool.get('party.party')
|
||||
party, = Party.create([{}])
|
||||
|
||||
address = party.address_get()
|
||||
|
||||
self.assertEqual(address, None)
|
||||
|
||||
@with_transaction()
|
||||
def test_address_get_inactive(self):
|
||||
"Test address_get with inactive"
|
||||
pool = Pool()
|
||||
Party = pool.get('party.party')
|
||||
Address = pool.get('party.address')
|
||||
party, = Party.create([{}])
|
||||
address1, address2 = Address.create([{
|
||||
'party': party.id,
|
||||
'sequence': 1,
|
||||
'active': False,
|
||||
}, {
|
||||
'party': party.id,
|
||||
'sequence': 2,
|
||||
'active': True,
|
||||
}])
|
||||
|
||||
address = party.address_get()
|
||||
|
||||
self.assertEqual(address, address2)
|
||||
|
||||
@with_transaction()
|
||||
def test_address_get_type(self):
|
||||
"Test address_get with type"
|
||||
pool = Pool()
|
||||
Party = pool.get('party.party')
|
||||
Address = pool.get('party.address')
|
||||
party, = Party.create([{}])
|
||||
address1, address2 = Address.create([{
|
||||
'party': party.id,
|
||||
'sequence': 1,
|
||||
'zip': None,
|
||||
}, {
|
||||
'party': party.id,
|
||||
'sequence': 2,
|
||||
'zip': '1000',
|
||||
}])
|
||||
|
||||
address = party.address_get(type='zip')
|
||||
|
||||
self.assertEqual(address, address2)
|
||||
|
||||
@with_transaction()
|
||||
def test_party_label_report(self):
|
||||
'Test party label report'
|
||||
|
||||
Reference in New Issue
Block a user