Skip to content

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

WIP: ad phoneprefix and try make it work

akjanikb245afe87f22
diff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.py
index 6281f6c860..62180305df 100644
--- a/saleor/userprofile/i18n.py
+++ b/saleor/userprofile/i18n.py
@@ -4,15 +4,19 @@ from collections import defaultdict
import i18naddress
from django import forms
+from django.forms.fields import ChoiceField
from django.forms.forms import BoundField
from django.utils.translation import pgettext, pgettext_lazy
from django_countries.data import COUNTRIES
+from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
from .models import Address
COUNTRY_FORMS = {}
UNKNOWN_COUNTRIES = set()
+phone_prefixes = [(k,'+{}'.format(k)) for (k, v) in COUNTRY_CODE_TO_REGION_CODE.items()]
+
AREA_TYPE_TRANSLATIONS = {
'area': pgettext_lazy('Address field', 'Area'),
@@ -74,9 +78,12 @@ class AddressForm(forms.ModelForm):
model = Address
exclude = []
+ phoneprefix = ChoiceField(choices=phone_prefixes)
+
def __init__(self, *args, **kwargs):
autocomplete_type = kwargs.pop('autocomplete_type', None)
super(AddressForm, self).__init__(*args, **kwargs)
+ #TODO at this point, phone number is treated as invalid
autocomplete_dict = defaultdict(
lambda: 'off', self.AUTOCOMPLETE_MAPPING)
for field_name, field in self.fields.items():
@@ -87,6 +94,16 @@ class AddressForm(forms.ModelForm):
autocomplete = autocomplete_dict[field_name]
field.widget.attrs['autocomplete'] = autocomplete
+ def clean(self):
+ cleaned_data = super(AddressForm, self).clean()
+ phoneprefix = cleaned_data.get("phoneprefix")
+ phone = cleaned_data.get("phone") #TODO: It's None for now
+ full_number = str(phoneprefix) + phone
+
+ # if full_number <phonenumbers validation here>:
+ # # perform your validation
+ # raise forms.ValidationError("Invalid phone number")
+
class CountryAwareAddressForm(AddressForm):
@@ -140,6 +157,7 @@ def get_address_form_class(country_code):
def get_form_i18n_lines(form_instance):
+ # import ipdb; ipdb.set_trace()
country_code = form_instance.i18n_country_code
try:
fields_order = i18naddress.get_field_order(
diff --git a/templates/userprofile/snippets/address-form.html b/templates/userprofile/snippets/address-form.html
index 725f4c6d46..fb3615d269 100644
--- a/templates/userprofile/snippets/address-form.html
+++ b/templates/userprofile/snippets/address-form.html
@@ -1,6 +1,7 @@
{% load bootstrap_field from bootstrap3 %}
<div class="i18n-address">
- {% bootstrap_field address_form.phone %}
+ {% bootstrap_field address_form.phoneprefix %}
+ {% bootstrap_field address_form.phone%}
{% with address_form_lines=address_form.i18n_fields_order %}
{% if address_form_lines %}
{% for line in address_form_lines %}