Skip to content

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

WIP: hacky solution

akjanik333cadaa5075
diff --git a/saleor/userprofile/forms.py b/saleor/userprofile/forms.py
index 8a34fac636..8fc31e50ca 100644
--- a/saleor/userprofile/forms.py
+++ b/saleor/userprofile/forms.py
@@ -5,6 +5,7 @@ from django.conf import settings
from django.contrib.auth import forms as django_forms, update_session_auth_hash
from .i18n import AddressMetaForm, get_address_form_class
+import phonenumbers
def get_address_form(data, country_code, initial=None, instance=None, **kwargs):
@@ -15,14 +16,23 @@ def get_address_form(data, country_code, initial=None, instance=None, **kwargs):
country_code = country_form.cleaned_data['country']
preview = country_form.cleaned_data['preview']
+ phone = str(instance.phone.national_number) if instance else ''
+ phoneprefix = str(instance.phone.country_code) if instance else ''
+
+ if data:
+ phoneprefix = data.get('phoneprefix')
+ phone = data.get('phone')
+ if instance:
+ instance.phone.country_code = phoneprefix
+ instance.phone.national_number = phone
+
address_form_class = get_address_form_class(country_code)
- # CHANGE HERE
if not preview and instance is not None:
address_form_class = get_address_form_class(
instance.country.code)
address_form = address_form_class(
- data, instance=instance,
+ data, instance=instance, phone=phone, phoneprefix=phoneprefix,
**kwargs)
else:
initial_address = (
diff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.py
index 67c787f158..797fb23941 100644
--- a/saleor/userprofile/i18n.py
+++ b/saleor/userprofile/i18n.py
@@ -123,8 +123,10 @@ class AddressForm(forms.ModelForm):
phone = kwargs.pop('phone', None)
phoneprefix = kwargs.pop('phoneprefix', None)
super(AddressForm, self).__init__(*args, **kwargs)
- self.fields['phone'].initial = phone
- self.fields['phoneprefix'].initial = phoneprefix
+ if self.instance.phone:
+ self.fields['phoneprefix'].initial = phoneprefix
+ self.fields['phone'].initial = phone
+
autocomplete_dict = defaultdict(
lambda: 'off', self.AUTOCOMPLETE_MAPPING)
for field_name, field in self.fields.items():
@@ -171,7 +173,7 @@ class CountryAwareAddressForm(AddressForm):
class Meta:
model = Address
- exclude = []
+ exclude = ['phone']
def add_field_errors(self, errors):
field_mapping = dict(self.I18N_MAPPING)
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index 7a6a7d43cd..2a9e6475b7 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -11,7 +11,7 @@ from django_countries.fields import Country, CountryField
from phonenumber_field.modelfields import PhoneNumberField
from .validators import validate_possible_number
-# PhoneNumberField.default_validators = [validate_possible_number]
+PhoneNumberField.default_validators = [validate_possible_number]
class AddressManager(models.Manager):
diff --git a/tests/test_userprofile.py b/tests/test_userprofile.py
index f368ccb5ce..4779c2b24f 100644
--- a/tests/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -30,7 +30,9 @@ def test_address_form_for_country(country):
data = {
'first_name': 'John',
'last_name': 'Doe',
- 'country': country}
+ 'country': country,
+ 'phone': '123456789'}
+
form = forms.get_address_form(data, country_code=country)[0]
errors = form.errors
rules = i18naddress.get_validation_rules({'country_code': country})