Code

Ticket #14608: INPhoneNumberField.diff

File INPhoneNumberField.diff, 7.5 KB (added by lawgon, 3 years ago)

fresh patch that fixes typo in the error message

Line 
1Index: django-mine/docs/ref/contrib/localflavor.txt
2===================================================================
3--- django-mine/docs/ref/contrib/localflavor.txt        (revision 16021)
4+++ django-mine/docs/ref/contrib/localflavor.txt        (working copy)
5@@ -440,22 +440,31 @@
6 India (``in_``)
7 ===============
8 
9-.. class:: in.forms.INStateField
10+.. class:: in_.forms.INStateField
11 
12     A form field that validates input as an Indian state/territory name or
13     abbreviation. Input is normalized to the standard two-letter vehicle
14     registration abbreviation for the given state or territory.
15 
16-.. class:: in.forms.INZipCodeField
17+.. class:: in_.forms.INZipCodeField
18 
19     A form field that validates input as an Indian zip code, with the
20     format XXXXXXX.
21 
22-.. class:: in.forms.INStateSelect
23+.. class:: in_.forms.INStateSelect
24 
25     A ``Select`` widget that uses a list of Indian states/territories as its
26     choices.
27+   
28+.. class:: in_.forms.INPhoneNumberField
29 
30+    A form field that validates that the data is a valid Indian phone number,
31+    including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
32+    string is the STD code which is a '0' followed by 2-4 digits. The second string
33+    is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
34+    6 digits if the STD code is 5 digits. The second string will start with numbers
35+    between 1 and 6. The separator is either a space or a hyphen.
36+
37 Ireland (``ie``)
38 ================
39 
40Index: django-mine/django/contrib/localflavor/in_/forms.py
41===================================================================
42--- django-mine/django/contrib/localflavor/in_/forms.py (revision 16021)
43+++ django-mine/django/contrib/localflavor/in_/forms.py (working copy)
44@@ -4,15 +4,15 @@
45 
46 from django.core.validators import EMPTY_VALUES
47 from django.forms import ValidationError
48-from django.forms.fields import Field, RegexField, Select
49+from django.forms.fields import Field, RegexField, Select, CharField
50 from django.utils.encoding import smart_unicode
51-from django.utils.translation import gettext
52+from django.utils.translation import ugettext_lazy as _
53 import re
54 
55 
56 class INZipCodeField(RegexField):
57     default_error_messages = {
58-        'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
59+        'invalid': _(u'Enter a zip code in the format XXXXXXX.'),
60     }
61 
62     def __init__(self, *args, **kwargs):
63@@ -54,3 +54,47 @@
64         from in_states import STATE_CHOICES
65         super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
66 
67+phone_digits_re = re.compile(r"""
68+     (
69+         (?P<std_code>                   # the std-code group
70+             ^0                          # all std-codes start with 0
71+             (
72+                 (?P<twodigit>\d{2})   | # either two, three or four digits
73+                 (?P<threedigit>\d{3}) | # following the 0
74+                 (?P<fourdigit>\d{4})
75+             )
76+         )
77+         [-\s]                           # space or -
78+         (?P<phone_no>                   # the phone number group
79+             [1-6]                       # first digit of phone number
80+             (
81+                 (?(twodigit)\d{7})   |  # 7 more phone digits for 3 digit stdcode
82+                 (?(threedigit)\d{6}) |  # 6 more phone digits for 4 digit stdcode
83+                 (?(fourdigit)\d{5})     # 5 more phone digits for 5 digit stdcode
84+             )
85+         )
86+     )$""", re.VERBOSE)
87+       
88+class INPhoneNumberField(CharField):
89+    """
90+        INPhoneNumberField validates that the data is a valid Indian phone number,
91+        including the STD code. It's normalised to 0XXX-XXXXXXX or 0XXX XXXXXXX format. The first
92+        string is the STD code which is a '0' followed by 2-4 digits. The second string
93+        is 8 digits if the STD code is 3 digits, 7 digits if the STD code is 4 digits and
94+        6 digits if the STD code is 5 digits. The second string will start with numbers
95+        between 1 and 6. The separator is either a space or a hyphen.
96+        """
97+    default_error_messages = {
98+        'invalid': _('Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.'),
99+    }
100+
101+    def clean(self, value):
102+        super(INPhoneNumberField, self).clean(value)
103+        if value in EMPTY_VALUES:
104+            return u''
105+        value = smart_unicode(value)
106+        m = phone_digits_re.match(value)
107+        if m:
108+            return u'%s' % (value)
109+        raise ValidationError(self.error_messages['invalid'])
110+
111Index: django-mine/tests/regressiontests/forms/tests/__init__.py
112===================================================================
113--- django-mine/tests/regressiontests/forms/tests/__init__.py   (revision 16021)
114+++ django-mine/tests/regressiontests/forms/tests/__init__.py   (working copy)
115@@ -17,10 +17,10 @@
116     CHLocalFlavorTests, CLLocalFlavorTests, CZLocalFlavorTests,
117     DELocalFlavorTests, ESLocalFlavorTests, FILocalFlavorTests,
118     FRLocalFlavorTests, GenericLocalFlavorTests, IDLocalFlavorTests,
119-    IELocalFlavorTests, ILLocalFlavorTests, ISLocalFlavorTests,
120-    ITLocalFlavorTests, JPLocalFlavorTests, KWLocalFlavorTests,
121-    NLLocalFlavorTests, PLLocalFlavorTests, PTLocalFlavorTests,
122-    ROLocalFlavorTests, SELocalFlavorTests, SKLocalFlavorTests,
123-    TRLocalFlavorTests, UKLocalFlavorTests, USLocalFlavorTests,
124-    UYLocalFlavorTests, ZALocalFlavorTests
125+    IELocalFlavorTests, ILLocalFlavorTests, INLocalFlavorTests,
126+    ISLocalFlavorTests, ITLocalFlavorTests, JPLocalFlavorTests,
127+    KWLocalFlavorTests, NLLocalFlavorTests, PLLocalFlavorTests,
128+    PTLocalFlavorTests, ROLocalFlavorTests, SELocalFlavorTests,
129+    SKLocalFlavorTests, TRLocalFlavorTests, UKLocalFlavorTests,
130+    USLocalFlavorTests, UYLocalFlavorTests, ZALocalFlavorTests
131 )
132Index: django-mine/tests/regressiontests/forms/localflavor/in_.py
133===================================================================
134--- django-mine/tests/regressiontests/forms/localflavor/in_.py  (revision 0)
135+++ django-mine/tests/regressiontests/forms/localflavor/in_.py  (revision 0)
136@@ -0,0 +1,24 @@
137+from django.contrib.localflavor.in_.forms import INPhoneNumberField
138+   
139+from utils import LocalFlavorTestCase
140+   
141+   
142+class INLocalFlavorTests(LocalFlavorTestCase):
143+    def test_INPhoneNumberField(self):
144+        error_format = [u'Phone numbers must be in 02X-8X or 03X-7X or 04X-6X format.']
145+        valid = {
146+            '0423-2443667': '0423-2443667',
147+            '0423 2443667': '0423 2443667',
148+            '04236-244366': '04236-244366',
149+            '040-24436678': '040-24436678',
150+        }
151+        invalid = {
152+            '04-2443667': error_format,
153+            '423-2443667': error_format,
154+            '0423-9442667': error_format,
155+            '0423-0443667': error_format,
156+            '0423-244366': error_format,
157+            '04232442667': error_format,
158+            '0423DJANGO': error_format,
159+        }
160+        self.assertFieldOutput(INPhoneNumberField, valid, invalid)
161Index: django-mine/tests/regressiontests/forms/localflavortests.py
162===================================================================
163--- django-mine/tests/regressiontests/forms/localflavortests.py (revision 16021)
164+++ django-mine/tests/regressiontests/forms/localflavortests.py (working copy)
165@@ -15,6 +15,7 @@
166 from localflavor.id import IDLocalFlavorTests
167 from localflavor.ie import IELocalFlavorTests
168 from localflavor.il import ILLocalFlavorTests
169+from localflavor.in_ import INLocalFlavorTests
170 from localflavor.is_ import ISLocalFlavorTests
171 from localflavor.it import ITLocalFlavorTests
172 from localflavor.jp import JPLocalFlavorTests