Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move custom PhoneNumber fields and widget to separate files
akjanik1692ec8cf2b9
diff --git a/saleor/userprofile/fields.py b/saleor/userprofile/fields.pynew file mode 100644index 0000000000..eb1ca6a5ac--- /dev/null+++ b/saleor/userprofile/fields.py@@ -0,0 +1,27 @@+from phonenumber_field.formfields import (+ PhoneNumberField as PhoneNumberFormField)+from phonenumber_field.modelfields import (+ PhoneNumberField as PhoneNumberModelField)++from .validators import validate_possible_number+++class PossiblePhoneNumberModelField(PhoneNumberModelField):+ """+ Less strict rule for phone numbers written to database.+ """+ default_validators = [validate_possible_number]+++class PossiblePhoneNumberFormField(PhoneNumberFormField):+ """+ Modify PhoneNumberField form field to allow using phone numbers from+ countries other than selected one.+ To achieve this both default_validator attribute and to_python method needs+ to be overwritten.+ """++ default_validators = [validate_possible_number]++ def to_python(self, value):+ return valuediff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.pyindex 1aa83bc1c5..345a8dc2c8 100644--- a/saleor/userprofile/i18n.py+++ b/saleor/userprofile/i18n.py@@ -5,24 +5,17 @@ from collections import defaultdictimport i18naddressfrom django import formsfrom django.forms.forms import BoundField-from django.forms import Select, TextInputfrom django_countries.data import COUNTRIESfrom django.utils.translation import pgettext_lazy-from phonenumber_field.widgets import (- PhoneNumberPrefixWidget, PhonePrefixSelect)-from phonenumber_field.formfields import PhoneNumberField-from phonenumbers import COUNTRY_CODE_TO_REGION_CODE+from .fields import PossiblePhoneNumberFormFieldfrom .models import Address-from .validators import validate_possible_number+from .widgets import PhonePrefixWidgetCOUNTRY_FORMS = {}UNKNOWN_COUNTRIES = set()-phone_prefixes = [- ('+{}'.format(k), '+{}'.format(k)) for- (k, v) in sorted(COUNTRY_CODE_TO_REGION_CODE.items())]AREA_TYPE_TRANSLATIONS = {'area': pgettext_lazy('Address field', 'Area'),@@ -47,34 +40,6 @@ AREA_TYPE_TRANSLATIONS = {'zip': pgettext_lazy('Address field', 'ZIP code')}-class PossiblePhoneNumberField(PhoneNumberField):- """- Modify PhoneNumberField form field to allow using phone numbers from- countries other than selected one.- To achieve this both default_validator attribute and to_python method needs- to be overwritten.- """-- default_validators = [validate_possible_number]-- def to_python(self, value):- return value---class PhonePrefixWidget(PhoneNumberPrefixWidget):- """- Overwrite widget to use choices with tuple in a simple form of "+XYZ: +XYZ"- Workaround for an issue:- https://github.com/stefanfoulis/django-phonenumber-field/issues/82- """-- template_name = 'userprofile/snippets/phone-prefix-widget.html'-- def __init__(self, attrs=None):- widgets = (Select(attrs=attrs, choices=phone_prefixes), TextInput())- super(PhoneNumberPrefixWidget, self).__init__(widgets, attrs)--class AddressMetaForm(forms.ModelForm):# This field is never visible in UIpreview = forms.BooleanField(initial=False, required=False)@@ -112,7 +77,8 @@ class AddressForm(forms.ModelForm):model = Addressexclude = []- phone = PossiblePhoneNumberField(widget=PhonePrefixWidget, required=False)+ phone = PossiblePhoneNumberFormField(+ widget=PhonePrefixWidget, required=False)def __init__(self, *args, **kwargs):autocomplete_type = kwargs.pop('autocomplete_type', None)diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.pyindex c9fd7eb5a9..eb1f54780c 100644--- a/saleor/userprofile/models.py+++ b/saleor/userprofile/models.py@@ -8,11 +8,8 @@ from django.utils import timezonefrom django.utils.encoding import python_2_unicode_compatiblefrom django.utils.translation import pgettext_lazyfrom django_countries.fields import Country, CountryField-from phonenumber_field.modelfields import PhoneNumberField-from .validators import validate_possible_number--PhoneNumberField.default_validators = [validate_possible_number]+from .fields import PossiblePhoneNumberModelFieldclass AddressManager(models.Manager):@@ -76,7 +73,7 @@ class Address(models.Model):country_area = models.CharField(pgettext_lazy('Address field', 'state or province'),max_length=128, blank=True)- phone = PhoneNumberField(+ phone = PossiblePhoneNumberModelField(verbose_name=pgettext_lazy('Address field', 'phone number'),blank=True, default='')objects = AddressManager()diff --git a/saleor/userprofile/widgets.py b/saleor/userprofile/widgets.pynew file mode 100644index 0000000000..643c524447--- /dev/null+++ b/saleor/userprofile/widgets.py@@ -0,0 +1,24 @@+from django.forms import Select, TextInput+from phonenumber_field.widgets import PhoneNumberPrefixWidget+from phonenumbers import COUNTRY_CODE_TO_REGION_CODE++from .validators import validate_possible_number+++phone_prefixes = [+ ('+{}'.format(k), '+{}'.format(k)) for+ (k, v) in sorted(COUNTRY_CODE_TO_REGION_CODE.items())]+++class PhonePrefixWidget(PhoneNumberPrefixWidget):+ """+ Overwrite widget to use choices with tuple in a simple form of "+XYZ: +XYZ"+ Workaround for an issue:+ https://github.com/stefanfoulis/django-phonenumber-field/issues/82+ """++ template_name = 'userprofile/snippets/phone-prefix-widget.html'++ def __init__(self, attrs=None):+ widgets = (Select(attrs=attrs, choices=phone_prefixes), TextInput())+ super(PhoneNumberPrefixWidget, self).__init__(widgets, attrs)