Repository: django-oscar/django-oscar

Issue:
Country model field

In url (address-create) or (address-detail).
If you input phone number but choose empty_label for country 
you will get 'NoneType' object has no attribute 'iso_3166_1_a2'
https://github.com/django-oscar/django-oscar/blob/master/src/oscar/views/generic.py#L188
return country.iso_3166_1_a2

1- So you need to adjust the UserAddressForm
https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/address/forms.py#L23
to make it like ShippingAddressForm
https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/checkout/forms.py#L15

or
2- adjust  get_region_code function
https://github.com/django-oscar/django-oscar/blob/master/src/oscar/views/generic.py#L188

I think the first solution much better!



Discussion:
`country` is a required field on the `UserAddress` model, so it shouldn't be possible to submit it empty - that should raise a validation error. Have you overridden the model? If so, I think you're probably better off setting a default country or overriding the form handling logic as well.
Sorry for the delay in reply.
To produce the bug
1- add OSCAR_REQUIRED_ADDRESS_FIELDS = ('phone_number', 'country') to settings.py.
2- In page (accounts/addresses/add/) Do not choose a country & enter phone number.
3- click on save button.

the output :
AttributeError at /ar/accounts/addresses/add/
'NoneType' object has no attribute 'iso_3166_1_a2'

I think the bug happened because it starts to check the phone number before return the error flash message. 
Thanks - I can reproduce that error now.

Resolving patch:
diff --git a/src/oscar/views/generic.py b/src/oscar/views/generic.py
--- a/src/oscar/views/generic.py
+++ b/src/oscar/views/generic.py
@@ -198,9 +198,9 @@ def clean_phone_number(self):
         try:
             phone_number = PhoneNumber.from_string(number)
         except phonenumbers.NumberParseException:
-            # Try hinting with the shipping country
+            # Try hinting with the shipping country if we can determine one
             country = self.get_country()
-            region_code = self.get_region_code(country)
+            region_code = self.get_region_code(country) if country else None
 
             if not region_code:
                 # There is no shipping country, not a valid international


Test patch:
diff --git a/tests/integration/checkout/test_forms.py b/tests/integration/checkout/test_forms.py
--- a/tests/integration/checkout/test_forms.py
+++ b/tests/integration/checkout/test_forms.py
@@ -83,3 +83,15 @@ def test_only_accepts_local_phone_number_when_country_matches(self):
 
         data['country'] = Country.objects.get(iso_3166_1_a2='GB').pk
         self.assertTrue(ShippingAddressForm(data).is_valid())
+
+    @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=('phone_number',))
+    def test_local_phonenumber_invalid_without_country(self):
+        # Add another country, so we have two.
+        CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True)
+        data = self.minimal_data.copy()
+        data['phone_number'] = '07 914721389'
+        # User hasn't selected a country. Because there are multiple country
+        # choices we should not accept the local number.
+        form = ShippingAddressForm(data)
+        self.assertFalse(form.is_valid())
+        self.assertIn('phone_number', form.errors)