New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve phone handling #1433
Improve phone handling #1433
Changes from 12 commits
b245afe
6577af8
333cada
54090e1
e129c03
cea2516
921dc51
989821d
73bddd1
2e0b382
b2e2ccb
0623a62
334cf8f
dbd1cd7
ce57b8e
77cca38
81af621
0187d67
1692ec8
549e8d6
fcc7964
caba37c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,27 @@ | |
import i18naddress | ||
from django import forms | ||
from django.forms.forms import BoundField | ||
from django.utils.translation import pgettext, pgettext_lazy | ||
from django.forms import Select, TextInput | ||
from django_countries.data import COUNTRIES | ||
from 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 .models import Address | ||
from .validators import validate_possible_number | ||
|
||
|
||
PhoneNumberField.default_validators = [validate_possible_number] | ||
PhoneNumberField.to_python = lambda self, value: value | ||
|
||
COUNTRY_FORMS = {} | ||
UNKNOWN_COUNTRIES = set() | ||
|
||
phone_prefixes = [ | ||
('+{}'.format(k), '+{}'.format(k)) for | ||
(k, v) in COUNTRY_CODE_TO_REGION_CODE.items()] | ||
|
||
AREA_TYPE_TRANSLATIONS = { | ||
'area': pgettext_lazy('Address field', 'Area'), | ||
|
@@ -37,6 +50,22 @@ | |
'zip': pgettext_lazy('Address field', 'ZIP code')} | ||
|
||
|
||
class CustomPhonePrefix(PhonePrefixSelect): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice to include the widget type in the class name so in this case, |
||
|
||
def __init__(self, initial=None): | ||
choices = phone_prefixes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's no need to assign |
||
if initial: | ||
self.initial = phone_prefixes[initial] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty blank lines in simple functions aren't necessary, it doesn't add much to readability. |
||
super(PhonePrefixSelect, self).__init__(choices=choices) | ||
|
||
|
||
class PhonePrefixWidget(PhoneNumberPrefixWidget): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to overwrite this one as there are issues with default widget value: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring. |
||
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 UI | ||
preview = forms.BooleanField(initial=False, required=False) | ||
|
@@ -74,6 +103,8 @@ class Meta: | |
model = Address | ||
exclude = [] | ||
|
||
phone = PhoneNumberField(widget=PhonePrefixWidget, required=False) | ||
|
||
def __init__(self, *args, **kwargs): | ||
autocomplete_type = kwargs.pop('autocomplete_type', None) | ||
super(AddressForm, self).__init__(*args, **kwargs) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import ugettext_lazy as _ | ||
from phonenumber_field.phonenumber import to_python | ||
from phonenumbers.phonenumberutil import is_possible_number | ||
|
||
|
||
def validate_possible_number(value): | ||
phone_number = to_python(value) | ||
if phone_number and not is_possible_number(phone_number): | ||
raise ValidationError( | ||
_('The phone number entered is not valid.'), | ||
code='invalid_phone_number') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this kind of patching, we should rather extend the original class and override the methods or fields that we want to customize. This would be also a good place to include relevant docstrings, explaining how the customization is different from the original implementation.