Ticket #14608: 14608.INPhoneNumberField.diff
File 14608.INPhoneNumberField.diff, 7.4 KB (added by , 14 years ago) |
---|
-
django/contrib/localflavor/in_/forms.py
diff --git a/django/contrib/localflavor/in_/forms.py b/django/contrib/localflavor/in_/forms.py index 0597623..8554e07 100644
a b India-specific Form helpers. 4 4 5 5 from django.core.validators import EMPTY_VALUES 6 6 from django.forms import ValidationError 7 from django.forms.fields import Field, RegexField, Select 7 from django.forms.fields import Field, RegexField, Select, CharField 8 8 from django.utils.encoding import smart_unicode 9 from django.utils.translation import gettext9 from django.utils.translation import ugettext_lazy as _ 10 10 import re 11 11 12 12 13 13 class INZipCodeField(RegexField): 14 14 default_error_messages = { 15 'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),15 'invalid': _(u'Enter a zip code in the format XXXXXXX.'), 16 16 } 17 17 18 18 def __init__(self, *args, **kwargs): … … class INStateSelect(Select): 54 54 from in_states import STATE_CHOICES 55 55 super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 56 56 57 phone_digits_re = re.compile(r""" 58 ( 59 (?P<std_code> # the std-code group 60 ^0 # all std-codes start with 0 61 ( 62 (?P<twodigit>\d{2}) | # either two, three or four digits 63 (?P<threedigit>\d{3}) | # following the 0 64 (?P<fourdigit>\d{4}) 65 ) 66 ) 67 [-\s] # space or - 68 (?P<phone_no> # the phone number group 69 [1-6] # first digit of phone number 70 ( 71 (?(twodigit)\d{7}) | # 7 more phone digits for 3 digit stdcode 72 (?(threedigit)\d{6}) | # 6 more phone digits for 4 digit stdcode 73 (?(fourdigit)\d{5}) # 5 more phone digits for 5 digit stdcode 74 ) 75 ) 76 )$""", re.VERBOSE) 77 78 class INPhoneNumberField(CharField): 79 """ 80 INPhoneNumberField validates that the data is a valid Indian phone number, 81 including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first 82 string is the STD code which is a '0' followed by 2-4 digits. The second string 83 is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and 84 6 digits if the STD code is 5 digits. The second string will start with numbers 85 between 1 and 6. The separator is either a space or a hyphen. 86 """ 87 default_error_messages = { 88 'invalid': _('Phone numbers must be in 02X-7X or 03X-6X or 04X-5X format.'), 89 } 90 91 def clean(self, value): 92 super(INPhoneNumberField, self).clean(value) 93 if value in EMPTY_VALUES: 94 return u'' 95 value = smart_unicode(value) 96 m = phone_digits_re.match(value) 97 if m: 98 return u'%s' % (value) 99 raise ValidationError(self.error_messages['invalid']) 100 -
docs/ref/contrib/localflavor.txt
diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index f54341e..cfdfe20 100644
a b Iceland (``is_``) 440 440 India (``in_``) 441 441 =============== 442 442 443 .. class:: in .forms.INStateField443 .. class:: in_.forms.INStateField 444 444 445 445 A form field that validates input as an Indian state/territory name or 446 446 abbreviation. Input is normalized to the standard two-letter vehicle 447 447 registration abbreviation for the given state or territory. 448 448 449 .. class:: in .forms.INZipCodeField449 .. class:: in_.forms.INZipCodeField 450 450 451 451 A form field that validates input as an Indian zip code, with the 452 452 format XXXXXXX. 453 453 454 .. class:: in .forms.INStateSelect454 .. class:: in_.forms.INStateSelect 455 455 456 456 A ``Select`` widget that uses a list of Indian states/territories as its 457 457 choices. 458 459 .. class:: in_.forms.INPhoneNumberField 460 461 A form field that validates that the data is a valid Indian phone number, 462 including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first 463 string is the STD code which is a '0' followed by 2-4 digits. The second string 464 is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and 465 6 digits if the STD code is 5 digits. The second string will start with numbers 466 between 1 and 6. The separator is either a space or a hyphen. 458 467 459 468 Ireland (``ie``) 460 469 ================ -
new file tests/regressiontests/forms/localflavor/in_.py
diff --git a/tests/regressiontests/forms/localflavor/in_.py b/tests/regressiontests/forms/localflavor/in_.py new file mode 100644 index 0000000..01eef7d
- + 1 from django.contrib.localflavor.in_.forms import INPhoneNumberField 2 3 from utils import LocalFlavorTestCase 4 5 6 class INLocalFlavorTests(LocalFlavorTestCase): 7 def test_INPhoneNumberField(self): 8 error_format = [u'Phone numbers must be in 02X-7X or 03X-6X or 04X-5X format.'] 9 valid = { 10 '0423-2443667': '0423-2443667', 11 '0423 2443667': '0423 2443667', 12 '04236-244366': '04236-244366', 13 '040-24436678': '040-24436678', 14 } 15 invalid = { 16 '04-2443667': error_format, 17 '423-2443667': error_format, 18 '0423-9442667': error_format, 19 '0423-0443667': error_format, 20 '0423-244366': error_format, 21 '04232442667': error_format, 22 '0423DJANGO': error_format, 23 } 24 self.assertFieldOutput(INPhoneNumberField, valid, invalid) -
tests/regressiontests/forms/localflavortests.py
diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 5ee1c32..7ff68cd 100644
a b from localflavor.generic import GenericLocalFlavorTests 15 15 from localflavor.id import IDLocalFlavorTests 16 16 from localflavor.ie import IELocalFlavorTests 17 17 from localflavor.il import ILLocalFlavorTests 18 from localflavor.in_ import INLocalFlavorTests 18 19 from localflavor.is_ import ISLocalFlavorTests 19 20 from localflavor.it import ITLocalFlavorTests 20 21 from localflavor.jp import JPLocalFlavorTests -
tests/regressiontests/forms/tests/__init__.py
diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 2d96b2f..bc25b8a 100644
a b from regressiontests.forms.localflavortests import ( 17 17 CHLocalFlavorTests, CLLocalFlavorTests, CZLocalFlavorTests, 18 18 DELocalFlavorTests, ESLocalFlavorTests, FILocalFlavorTests, 19 19 FRLocalFlavorTests, GenericLocalFlavorTests, IDLocalFlavorTests, 20 IELocalFlavorTests, ILLocalFlavorTests, I SLocalFlavorTests,21 I TLocalFlavorTests, JPLocalFlavorTests, KWLocalFlavorTests,22 NLLocalFlavorTests, PLLocalFlavorTests, PTLocalFlavorTests,23 ROLocalFlavorTests, SELocalFlavorTests, SKLocalFlavorTests,24 TRLocalFlavorTests, UKLocalFlavorTests, USLocalFlavorTests,25 U YLocalFlavorTests, ZALocalFlavorTests20 IELocalFlavorTests, ILLocalFlavorTests, INLocalFlavorTests, 21 ISLocalFlavorTests, ITLocalFlavorTests, JPLocalFlavorTests, 22 KWLocalFlavorTests, NLLocalFlavorTests, PLLocalFlavorTests, 23 PTLocalFlavorTests, ROLocalFlavorTests, SELocalFlavorTests, 24 SKLocalFlavorTests, TRLocalFlavorTests, UKLocalFlavorTests, 25 USLocalFlavorTests, UYLocalFlavorTests, ZALocalFlavorTests 26 26 )