Ticket #14608: 14608.IndianPhoneNumber-LocalFlavor.diff
File 14608.IndianPhoneNumber-LocalFlavor.diff, 5.5 KB (added by , 13 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 afc5534..3bdd481 100644
a b import re 5 5 6 6 from django.core.validators import EMPTY_VALUES 7 7 from django.forms import ValidationError 8 from django.forms.fields import Field, RegexField, Select8 from django.forms.fields import Field, RegexField, CharField, Select 9 9 from django.utils.encoding import smart_unicode 10 10 from django.utils.translation import ugettext_lazy as _ 11 11 … … class INStateSelect(Select): 62 62 from in_states import STATE_CHOICES 63 63 super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 64 64 65 phone_digits_re = re.compile(r""" 66 ( 67 (?P<std_code> # the std-code group 68 ^0 # all std-codes start with 0 69 ( 70 (?P<twodigit>\d{2}) | # either two, three or four digits 71 (?P<threedigit>\d{3}) | # following the 0 72 (?P<fourdigit>\d{4}) 73 ) 74 ) 75 [-\s] # space or - 76 (?P<phone_no> # the phone number group 77 [1-6] # first digit of phone number 78 ( 79 (?(twodigit)\d{7}) | # 7 more phone digits for 3 digit stdcode 80 (?(threedigit)\d{6}) | # 6 more phone digits for 4 digit stdcode 81 (?(fourdigit)\d{5}) # 5 more phone digits for 5 digit stdcode 82 ) 83 ) 84 )$""", re.VERBOSE) 85 86 class INPhoneNumberField(CharField): 87 """ 88 INPhoneNumberField validates that the data is a valid Indian phone number, 89 including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first 90 string is the STD code which is a '0' followed by 2-4 digits. The second string 91 is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and 92 6 digits if the STD code is 5 digits. The second string will start with numbers 93 between 1 and 6. The separator is either a space or a hyphen. 94 """ 95 default_error_messages = { 96 'invalid': _('Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.'), 97 } 98 99 def clean(self, value): 100 super(INPhoneNumberField, self).clean(value) 101 if value in EMPTY_VALUES: 102 return u'' 103 value = smart_unicode(value) 104 m = phone_digits_re.match(value) 105 if m: 106 return u'%s' % (value) 107 raise ValidationError(self.error_messages['invalid']) 108 -
docs/ref/contrib/localflavor.txt
diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index 3ad5899..80aa8f7 100644
a b Iceland (``is_``) 576 576 India (``in_``) 577 577 =============== 578 578 579 .. class:: in .forms.INStateField579 .. class:: in_.forms.INStateField 580 580 581 581 A form field that validates input as an Indian state/territory name or 582 582 abbreviation. Input is normalized to the standard two-letter vehicle 583 583 registration abbreviation for the given state or territory. 584 584 585 .. class:: in .forms.INZipCodeField585 .. class:: in_.forms.INZipCodeField 586 586 587 587 A form field that validates input as an Indian zip code, with the 588 588 format XXXXXXX. 589 589 590 .. class:: in .forms.INStateSelect590 .. class:: in_.forms.INStateSelect 591 591 592 592 A ``Select`` widget that uses a list of Indian states/territories as its 593 593 choices. 594 595 .. class:: in_.forms.INPhoneNumberField 596 597 A form field that validates that the data is a valid Indian phone number, 598 including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first 599 string is the STD code which is a '0' followed by 2-4 digits. The second string 600 is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and 601 6 digits if the STD code is 5 digits. The second string will start with numbers 602 between 1 and 6. The separator is either a space or a hyphen. 594 603 595 604 Ireland (``ie``) 596 605 ================ -
tests/regressiontests/forms/localflavor/in_.py
diff --git a/tests/regressiontests/forms/localflavor/in_.py b/tests/regressiontests/forms/localflavor/in_.py index 1755366..5a04fd1 100644
a b 1 1 import warnings 2 2 3 3 from django.contrib.localflavor.in_.forms import (INZipCodeField, 4 INStateField, INStateSelect)4 INStateField, INStateSelect, INPhoneNumberField) 5 5 6 6 from utils import LocalFlavorTestCase 7 7 8 8 9 9 10 class INLocalFlavorTests(LocalFlavorTestCase): 11 def test_INPhoneNumberField(self): 12 error_format = [u'Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.'] 13 valid = { 14 '0423-2443667': '0423-2443667', 15 '0423 2443667': '0423 2443667', 16 '04236-244366': '04236-244366', 17 '040-24436678': '040-24436678', 18 } 19 invalid = { 20 '04-2443667': error_format, 21 '423-2443667': error_format, 22 '0423-9442667': error_format, 23 '0423-0443667': error_format, 24 '0423-244366': error_format, 25 '04232442667': error_format, 26 '0423DJANGO': error_format, 27 } 28 self.assertFieldOutput(INPhoneNumberField, valid, invalid) 29 10 30 def test_INPStateSelect(self): 11 31 f = INStateSelect() 12 32 out = u'''<select name="state">