Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Improve i18n API

Artur Smęt5731b783c8dc
diff --git a/saleor/userprofile/forms.py b/saleor/userprofile/forms.py
index 2f4c50367b..9a62454deb 100644
--- a/saleor/userprofile/forms.py
+++ b/saleor/userprofile/forms.py
@@ -4,7 +4,7 @@ from __future__ import unicode_literals
from .i18n import AddressMetaForm, get_address_form_class, AddressForm
-def get_address_form(data, country_code, initial, instance, **kwargs):
+def get_address_form(data, country_code, initial=None, instance=None, **kwargs):
country_form = AddressMetaForm(data, initial=initial)
preview = False
diff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.py
index 50b7897e09..5a69f98a0b 100644
--- a/saleor/userprofile/i18n.py
+++ b/saleor/userprofile/i18n.py
@@ -115,8 +115,9 @@ class CountryAwareAddressForm(AddressForm):
def validate_address(self, data):
try:
data['country_code'] = data['country']
- data['street_address'] = '%s\n%s' % (
- data['street_address_1'], data['street_address_2'])
+ if data['street_address_1'] or data['street_address_2']:
+ data['street_address'] = '%s\n%s' % (
+ data['street_address_1'], data['street_address_2'])
data = i18naddress.normalize_address(data)
del data['sorting_code']
except i18naddress.InvalidAddress as exc:
@@ -134,7 +135,7 @@ def get_address_form_class(country_code):
def get_form_18n_lines(form_instance):
country_code = form_instance.i18n_country_code
- fields_order = i18naddress.get_fields_order({'country_code': country_code})
+ fields_order = i18naddress.get_field_order({'country_code': country_code})
field_mapping = dict(form_instance.I18N_MAPPING)
def _convert_to_bound_fields(form, i18n_field_names):
diff --git a/tests/test_userprofile.py b/tests/test_userprofile.py
index cf1483ead5..37fa87281a 100644
--- a/tests/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -24,9 +24,10 @@ def test_address_form_for_country(country):
'first_name': 'John',
'last_name': 'Doe',
'country': country}
- form = forms.AddressForm(data)
+ form = forms.get_address_form(data, country_code=country)[0]
errors = form.errors
- required = i18naddress.validate_areas(country)[0]
+ rules = i18naddress.get_validation_rules({'country_code': country})
+ required = rules.required_fields
if 'street_address' in required:
assert 'street_address_1' in errors
else:
@@ -53,8 +54,8 @@ def test_address_form_postal_code_validation():
data = {
'first_name': 'John',
'last_name': 'Doe',
- 'country': 'PE',
+ 'country': 'PL',
'postal_code': 'XXX'}
- form = forms.AddressForm(data)
+ form = forms.get_address_form(data, country_code='PL')[0]
errors = form.errors
assert 'postal_code' in errors