Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #114: UK addresses with no locality #116

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions address/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _to_python(value):
state_code = value.get('state_code', '')
locality = value.get('locality', '')
sublocality = value.get('sublocality', '')
postal_town = value.get('postal_town', '')
postal_code = value.get('postal_code', '')
street_number = value.get('street_number', '')
route = value.get('route', '')
Expand All @@ -47,6 +48,11 @@ def _to_python(value):
if not locality and sublocality:
locality = sublocality

# Fix issue with UK addresses with no locality
# (https://github.com/furious-luke/django-address/issues/114)
if not locality and postal_town:
locality = postal_town

# If we have an inconsistent set of value bail out now.
if (country or state or locality) and not (country and state and locality):
raise InconsistentDictError
Expand Down
1 change: 1 addition & 0 deletions address/static/address/js/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $(function () {
'country_code',
'locality',
'postal_code',
'postal_town',
'route',
'street_number',
'state',
Expand Down
2 changes: 1 addition & 1 deletion address/static/js/jquery.geocomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions address/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def test_to_python_no_locality(self):
res = self.field.to_python(input)
self.assertEqual(res.locality.name, 'Brooklyn')

def test_to_python_postal_town(self):
'''UK addresses with no `locality`, but a populated `postal_town`, should use the
`postal_town` as the `locality`'''
data = {
'raw': 'High Street, Leamington Spa',
'route': 'High Street',
'postal_town': 'Leamington Spa',
'state': 'England',
'state_code': 'England',
'country': 'United Kingdom',
'country_code': 'GB',
'postal_code': 'CV31',
'formatted': 'High St, Royal Leamington Spa, Leamington Spa CV31, UK'
}
address = self.field.to_python(data)
self.assertIsNotNone(address.locality)
self.assertEqual(address.locality.name, data["postal_town"])

# TODO: Fix
# def test_to_python_empty_state(self):
# val = self.field.to_python(self.missing_state)
Expand Down
4 changes: 2 additions & 2 deletions address/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class AddressWidget(forms.TextInput):
components = [('country', 'country'), ('country_code', 'country_short'),
('locality', 'locality'), ('sublocality', 'sublocality'),
('postal_code', 'postal_code'), ('route', 'route'),
('street_number', 'street_number'),
('postal_code', 'postal_code'), ('postal_town', 'postal_town'),
('route', 'route'), ('street_number', 'street_number'),
('state', 'administrative_area_level_1'),
('state_code', 'administrative_area_level_1_short'),
('formatted', 'formatted_address'),
Expand Down