Code

Ticket #3986: indianlocalflavour.diff

File indianlocalflavour.diff, 4.3 KB (added by pradeep.gowda@…, 7 years ago)

initial diff file attached.

Line 
1Index: ind/in_states.py
2===================================================================
3--- ind/in_states.py    (revision 0)
4+++ ind/in_states.py    (revision 0)
5@@ -0,0 +1,84 @@
6+"""
7+A mapping of state misspellings/abbreviations to normalized abbreviations, and
8+an alphabetical list of states for use as `choices` in a formfield.
9+
10+This exists in this standalone file so that it's only imported into memory
11+when explicitly needed.
12+"""
13+
14+STATE_CHOICES = (
15+    'KA', 'Karnataka',
16+    'AP', 'Andhra Pradesh',
17+    'KL', 'Kerala',
18+    'TN', 'Tamil Nadu',
19+    'MH', 'Maharashtra',
20+    'UP', 'Uttar Pradesh',
21+    'GA', 'Goa',
22+    'GJ', 'Gujarat',
23+    'RJ', 'Rajasthan',
24+    'HP', 'Himachal Pradesh',
25+    'JK', 'Jammu and Kashmir',
26+    'AR', 'Arunachal Pradesh',
27+    'AS', 'Assam',
28+    'BR', 'Bihar',
29+    'CG', 'Chattisgarh',
30+    'HR', 'Haryana',
31+    'JH', 'Jharkhand',
32+    'MP', 'Madhya Pradesh',
33+    'MN', 'Manipur',
34+    'ML', 'Meghalaya',
35+    'MZ', 'Mizoram',
36+    'NL', 'Nagaland',
37+    'OR', 'Orissa',
38+    'PB', 'Punjab',
39+    'SK', 'Sikkim',
40+    'TR', 'Tripura',
41+    'UA', 'Uttarakhand',
42+    'WB', 'West Bengal',
43+    #Union Territories
44+    'AN', 'Andaman and Nicobar',
45+    'CH', 'Chandigarh',
46+    'DN', 'Dadra and Nagar Haveli',
47+    'DD', 'Daman and Diu',
48+    'DL', 'Delhi',
49+    'LD', 'Lakshadweep',
50+    'PY', 'Pondicherry',
51+)
52+
53+STATES_NORMALIZED = {
54+    'ka': 'KA',
55+    'karnatka': 'KA',
56+    'tn': 'TN',
57+    'tamilnad': 'TN',
58+    'tamilnadu': 'TN',
59+    'andra pradesh': 'AP',
60+    'andrapradesh': 'AP',
61+    'andhrapradesh': 'AP',
62+    'maharastra': 'MH',
63+    'mh': 'MH',
64+    'ap': 'AP',
65+    'dl': 'DL',
66+    'dd': 'DD',
67+    'br': 'BR',
68+    'ar': 'AR',
69+    'sk': 'SK',
70+    'kl': 'KL',
71+    'ga': 'GA',
72+    'rj': 'RJ',
73+    'rajastan': 'RJ',
74+    'rajasthan': 'RJ',
75+    'hp': 'HP',
76+    'ua': 'UA',
77+    'up': 'UP',
78+    'mp': 'MP',
79+    'mz': 'MZ',
80+    'bengal': 'WB',
81+    'westbengal': 'WB',
82+    'mizo': 'MZ',
83+    'orisa': 'OR',
84+    'odisa': 'OR',
85+    'or': 'OR',
86+    'ar': 'AR',
87+
88+    }
89+
90
91Index: ind/__init__.py
92===================================================================
93--- ind/__init__.py     (revision 0)
94+++ ind/__init__.py     (revision 0)
95@@ -0,0 +1 @@
96+# __init__.py
97
98Index: ind/forms.py
99===================================================================
100--- ind/forms.py        (revision 0)
101+++ ind/forms.py        (revision 0)
102@@ -0,0 +1,48 @@
103+"""
104+India-specific Form helpers
105+"""
106+
107+from django.newforms import ValidationError
108+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
109+from django.utils.encoding import smart_unicode
110+from django.utils.translation import gettext
111+import re
112+
113+
114+class INZipCodeField(RegexField):
115+    def __init__(self, *args, **kwargs):
116+        super(INZipCodeField, self).__init__(r'^\d{6}$',
117+            max_length=None, min_length=None,
118+            error_message=gettext(u'Enter a zip code in the format XXXXXXX.'),
119+            *args, **kwargs)
120+
121+
122+class INStateField(Field):
123+    """
124+    A form field that validates its input is a Indian state name or abbreviatio                                              n.
125+    It normalizes the input to the standard two-leter vehicle registration
126+    abbreviation for the given state or union territory
127+    """
128+    def clean(self, value):
129+        from in_states import STATES_NORMALIZED # relative import
130+        super(INStateField, self).clean(value)
131+        if value in EMPTY_VALUES:
132+            return u''
133+        try:
134+            value = value.strip().lower()
135+        except AttributeError:
136+            pass
137+        else:
138+            try:
139+                return STATES_NORMALIZED[value.strip().lower()].decode('ascii')
140+            except KeyError:
141+                pass
142+        raise ValidationError(u'Enter a Indian state or territory.')
143+
144+class INStateSelect(Select):
145+    """
146+    A Select widget that uses a list of Indian states/territories as its choice                                              s.
147+    """
148+    def __init__(self, attrs=None):
149+        from in_states import STATE_CHOICES # relative import
150+        super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)