diff --git a/party.py b/party.py index 089b740ae6..5efe081db0 100644 --- a/party.py +++ b/party.py @@ -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 diff --git a/tests/test_party.py b/tests/test_party.py index f6bbce83c5..7e5d958613 100644 --- a/tests/test_party.py +++ b/tests/test_party.py @@ -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'