0004-fields-add-a-PhoneField-69221.patch
src/authentic2/forms/fields.py | ||
---|---|---|
17 | 17 |
import io |
18 | 18 |
import warnings |
19 | 19 | |
20 |
import phonenumbers |
|
20 | 21 |
import PIL.Image |
22 |
from django.conf import settings |
|
21 | 23 |
from django.core.files import File |
22 |
from django.forms import CharField, EmailField, FileField, ValidationError |
|
24 |
from django.forms import CharField, EmailField, FileField, MultiValueField, ValidationError
|
|
23 | 25 |
from django.forms.fields import FILE_INPUT_CONTRADICTION |
24 | 26 |
from django.utils.translation import ugettext_lazy as _ |
25 | 27 | |
... | ... | |
29 | 31 |
EmailInput, |
30 | 32 |
NewPasswordInput, |
31 | 33 |
PasswordInput, |
34 |
PhoneWidget, |
|
32 | 35 |
ProfileImageInput, |
33 | 36 |
) |
34 | 37 |
from authentic2.passwords import validate_password |
... | ... | |
115 | 118 |
class ValidatedEmailField(EmailField): |
116 | 119 |
default_validators = [email_validator] |
117 | 120 |
widget = EmailInput |
121 | ||
122 | ||
123 |
class PhoneField(MultiValueField): |
|
124 |
widget = PhoneWidget |
|
125 | ||
126 |
def __init__(self, **kwargs): |
|
127 |
fields = ( |
|
128 |
CharField(max_length=8), |
|
129 |
CharField(max_length=16), |
|
130 |
) |
|
131 |
super().__init__(error_messages=None, fields=fields, require_all_fields=False, **kwargs) |
|
132 | ||
133 |
def compress(self, data_list): |
|
134 |
country_code = data_list[0] |
|
135 |
data_list[0] = '+%s' % data_list[0] |
|
136 |
pn = phonenumbers.parse(''.join(data_list), settings.PHONE_COUNTRY_CODES[country_code]) |
|
137 |
return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164) |
|
118 |
- |