Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15813 -- Updated Indian localflavor to use correct state choic…

…es and fixed various other bugs. Thanks, jsdalton.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16480 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 940aed10b7bdd8f25aff38b8b923deb94ce4cee4 1 parent a6cd786
Jannis Leidel authored June 28, 2011
18  django/contrib/localflavor/in_/forms.py
... ...
@@ -1,24 +1,32 @@
1 1
 """
2 2
 India-specific Form helpers.
3 3
 """
  4
+import re
4 5
 
5 6
 from django.core.validators import EMPTY_VALUES
6 7
 from django.forms import ValidationError
7 8
 from django.forms.fields import Field, RegexField, Select
8 9
 from django.utils.encoding import smart_unicode
9  
-from django.utils.translation import gettext
10  
-import re
  10
+from django.utils.translation import ugettext_lazy as _
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 XXXXXX or XXX XXX.'),
16 16
     }
17 17
 
18 18
     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
19  
-        super(INZipCodeField, self).__init__(r'^\d{6}$',
  19
+        super(INZipCodeField, self).__init__(r'^\d{3}\s?\d{3}$',
20 20
             max_length, min_length, *args, **kwargs)
21 21
 
  22
+    def clean(self, value):
  23
+        super(INZipCodeField, self).clean(value)
  24
+        if value in EMPTY_VALUES:
  25
+            return u''
  26
+        # Convert to "NNNNNN" if "NNN NNN" given
  27
+        value = re.sub(r'^(\d{3})\s(\d{3})$', r'\1\2', value)
  28
+        return value
  29
+
22 30
 class INStateField(Field):
23 31
     """
24 32
     A form field that validates its input is a Indian state name or
@@ -26,7 +34,7 @@ class INStateField(Field):
26 34
     registration abbreviation for the given state or union territory
27 35
     """
28 36
     default_error_messages = {
29  
-        'invalid': u'Enter a Indian state or territory.',
  37
+        'invalid': _(u'Enter an Indian state or territory.'),
30 38
     }
31 39
 
32 40
     def clean(self, value):
94  django/contrib/localflavor/in_/in_states.py
@@ -47,37 +47,87 @@
47 47
 )
48 48
 
49 49
 STATES_NORMALIZED = {
50  
-    'ka': 'KA',
51  
-    'karnatka': 'KA',
52  
-    'tn': 'TN',
53  
-    'tamilnad': 'TN',
54  
-    'tamilnadu': 'TN',
  50
+    'an': 'AN',
  51
+    'andaman and nicobar': 'AN',
55 52
     'andra pradesh': 'AP',
56 53
     'andrapradesh': 'AP',
57 54
     'andhrapradesh': 'AP',
58  
-    'maharastra': 'MH',
59  
-    'mh': 'MH',
60 55
     'ap': 'AP',
61  
-    'dl': 'DL',
62  
-    'dd': 'DD',
63  
-    'br': 'BR',
  56
+    'andhra pradesh': 'AP',
64 57
     'ar': 'AR',
65  
-    'sk': 'SK',
66  
-    'kl': 'KL',
  58
+    'arunachal pradesh': 'AR',
  59
+    'assam': 'AS',
  60
+    'as': 'AS',
  61
+    'bihar': 'BR',
  62
+    'br': 'BR',
  63
+    'cg': 'CG',
  64
+    'chattisgarh': 'CG',
  65
+    'ch': 'CH',
  66
+    'chandigarh': 'CH',
  67
+    'daman and diu': 'DD',
  68
+    'dd': 'DD',
  69
+    'dl': 'DL',
  70
+    'delhi': 'DL',
  71
+    'dn': 'DN',
  72
+    'dadra and nagar haveli': 'DN',
67 73
     'ga': 'GA',
68  
-    'rj': 'RJ',
69  
-    'rajastan': 'RJ',
70  
-    'rajasthan': 'RJ',
  74
+    'goa': 'GA',
  75
+    'gj': 'GJ',
  76
+    'gujarat': 'GJ',
  77
+    'himachal pradesh': 'HP',
71 78
     'hp': 'HP',
72  
-    'ua': 'UA',
73  
-    'up': 'UP',
  79
+    'hr': 'HR',
  80
+    'haryana': 'HR',
  81
+    'jharkhand': 'JH',
  82
+    'jh': 'JH',
  83
+    'jammu and kashmir': 'JK',
  84
+    'jk': 'JK',
  85
+    'karnataka': 'KA',
  86
+    'karnatka': 'KA',
  87
+    'ka': 'KA',
  88
+    'kerala': 'KL',
  89
+    'kl': 'KL',
  90
+    'ld': 'LD',
  91
+    'lakshadweep': 'LD',
  92
+    'maharastra': 'MH',
  93
+    'mh': 'MH',
  94
+    'maharashtra': 'MH',
  95
+    'meghalaya': 'ML',
  96
+    'ml': 'ML',
  97
+    'mn': 'MN',
  98
+    'manipur': 'MN',
  99
+    'madhya pradesh': 'MP',
74 100
     'mp': 'MP',
75  
-    'mz': 'MZ',
76  
-    'bengal': 'WB',
77  
-    'westbengal': 'WB',
  101
+    'mizoram': 'MZ',
78 102
     'mizo': 'MZ',
79  
-    'orisa': 'OR',
  103
+    'mz': 'MZ',
  104
+    'nl': 'NL',
  105
+    'nagaland': 'NL',
  106
+    'orissa': 'OR',
80 107
     'odisa': 'OR',
  108
+    'orisa': 'OR',
81 109
     'or': 'OR',
  110
+    'pb': 'PB',
  111
+    'punjab': 'PB',
  112
+    'py': 'PY',
  113
+    'pondicherry': 'PY',
  114
+    'rajasthan': 'RJ',
  115
+    'rajastan': 'RJ',
  116
+    'rj': 'RJ',
  117
+    'sikkim': 'SK',
  118
+    'sk': 'SK',
  119
+    'tamil nadu': 'TN',
  120
+    'tn': 'TN',
  121
+    'tamilnadu': 'TN',
  122
+    'tamilnad': 'TN',
  123
+    'tr': 'TR',
  124
+    'tripura': 'TR',
  125
+    'ua': 'UA',
  126
+    'uttarakhand': 'UA',
  127
+    'up': 'UP',
  128
+    'uttar pradesh': 'UP',
  129
+    'westbengal': 'WB',
  130
+    'bengal': 'WB',
  131
+    'wb': 'WB',
  132
+    'west bengal': 'WB'
82 133
 }
83  
-
158  tests/regressiontests/forms/localflavor/in_.py
... ...
@@ -0,0 +1,158 @@
  1
+import warnings
  2
+
  3
+from django.contrib.localflavor.in_.forms import (INZipCodeField,
  4
+                                                  INStateField, INStateSelect)
  5
+
  6
+from utils import LocalFlavorTestCase
  7
+
  8
+
  9
+class INLocalFlavorTests(LocalFlavorTestCase):
  10
+    def test_INPStateSelect(self):
  11
+        f = INStateSelect()
  12
+        out = u'''<select name="state">
  13
+<option value="KA">Karnataka</option>
  14
+<option value="AP" selected="selected">Andhra Pradesh</option>
  15
+<option value="KL">Kerala</option>
  16
+<option value="TN">Tamil Nadu</option>
  17
+<option value="MH">Maharashtra</option>
  18
+<option value="UP">Uttar Pradesh</option>
  19
+<option value="GA">Goa</option>
  20
+<option value="GJ">Gujarat</option>
  21
+<option value="RJ">Rajasthan</option>
  22
+<option value="HP">Himachal Pradesh</option>
  23
+<option value="JK">Jammu and Kashmir</option>
  24
+<option value="AR">Arunachal Pradesh</option>
  25
+<option value="AS">Assam</option>
  26
+<option value="BR">Bihar</option>
  27
+<option value="CG">Chattisgarh</option>
  28
+<option value="HR">Haryana</option>
  29
+<option value="JH">Jharkhand</option>
  30
+<option value="MP">Madhya Pradesh</option>
  31
+<option value="MN">Manipur</option>
  32
+<option value="ML">Meghalaya</option>
  33
+<option value="MZ">Mizoram</option>
  34
+<option value="NL">Nagaland</option>
  35
+<option value="OR">Orissa</option>
  36
+<option value="PB">Punjab</option>
  37
+<option value="SK">Sikkim</option>
  38
+<option value="TR">Tripura</option>
  39
+<option value="UA">Uttarakhand</option>
  40
+<option value="WB">West Bengal</option>
  41
+<option value="AN">Andaman and Nicobar</option>
  42
+<option value="CH">Chandigarh</option>
  43
+<option value="DN">Dadra and Nagar Haveli</option>
  44
+<option value="DD">Daman and Diu</option>
  45
+<option value="DL">Delhi</option>
  46
+<option value="LD">Lakshadweep</option>
  47
+<option value="PY">Pondicherry</option>
  48
+</select>'''
  49
+        self.assertEqual(f.render('state', 'AP'), out)
  50
+
  51
+    def test_INZipCodeField(self):
  52
+        error_format = [u'Enter a zip code in the format XXXXXX or XXX XXX.']
  53
+        valid = {
  54
+            '360311': '360311',
  55
+            '360 311': '360311',
  56
+        }
  57
+        invalid = {
  58
+            '36 0311': error_format,
  59
+            '3603111': error_format,
  60
+            '360 31': error_format,
  61
+            '36031': error_format,
  62
+            'O2B 2R3': error_format
  63
+        }
  64
+        self.assertFieldOutput(INZipCodeField, valid, invalid)
  65
+
  66
+    def test_INStateField(self):
  67
+        error_format = [u'Enter an Indian state or territory.']
  68
+        valid = {
  69
+            'an': 'AN',
  70
+            'AN': 'AN',
  71
+            'andaman and nicobar': 'AN',
  72
+            'andra pradesh': 'AP',
  73
+            'andrapradesh': 'AP',
  74
+            'andhrapradesh': 'AP',
  75
+            'ap': 'AP',
  76
+            'andhra pradesh': 'AP',
  77
+            'ar': 'AR',
  78
+            'arunachal pradesh': 'AR',
  79
+            'assam': 'AS',
  80
+            'as': 'AS',
  81
+            'bihar': 'BR',
  82
+            'br': 'BR',
  83
+            'cg': 'CG',
  84
+            'chattisgarh': 'CG',
  85
+            'ch': 'CH',
  86
+            'chandigarh': 'CH',
  87
+            'daman and diu': 'DD',
  88
+            'dd': 'DD',
  89
+            'dl': 'DL',
  90
+            'delhi': 'DL',
  91
+            'dn': 'DN',
  92
+            'dadra and nagar haveli': 'DN',
  93
+            'ga': 'GA',
  94
+            'goa': 'GA',
  95
+            'gj': 'GJ',
  96
+            'gujarat': 'GJ',
  97
+            'himachal pradesh': 'HP',
  98
+            'hp': 'HP',
  99
+            'hr': 'HR',
  100
+            'haryana': 'HR',
  101
+            'jharkhand': 'JH',
  102
+            'jh': 'JH',
  103
+            'jammu and kashmir': 'JK',
  104
+            'jk': 'JK',
  105
+            'karnataka': 'KA',
  106
+            'karnatka': 'KA',
  107
+            'ka': 'KA',
  108
+            'kerala': 'KL',
  109
+            'kl': 'KL',
  110
+            'ld': 'LD',
  111
+            'lakshadweep': 'LD',
  112
+            'maharastra': 'MH',
  113
+            'mh': 'MH',
  114
+            'maharashtra': 'MH',
  115
+            'meghalaya': 'ML',
  116
+            'ml': 'ML',
  117
+            'mn': 'MN',
  118
+            'manipur': 'MN',
  119
+            'madhya pradesh': 'MP',
  120
+            'mp': 'MP',
  121
+            'mizoram': 'MZ',
  122
+            'mizo': 'MZ',
  123
+            'mz': 'MZ',
  124
+            'nl': 'NL',
  125
+            'nagaland': 'NL',
  126
+            'orissa': 'OR',
  127
+            'odisa': 'OR',
  128
+            'orisa': 'OR',
  129
+            'or': 'OR',
  130
+            'pb': 'PB',
  131
+            'punjab': 'PB',
  132
+            'py': 'PY',
  133
+            'pondicherry': 'PY',
  134
+            'rajasthan': 'RJ',
  135
+            'rajastan': 'RJ',
  136
+            'rj': 'RJ',
  137
+            'sikkim': 'SK',
  138
+            'sk': 'SK',
  139
+            'tamil nadu': 'TN',
  140
+            'tn': 'TN',
  141
+            'tamilnadu': 'TN',
  142
+            'tamilnad': 'TN',
  143
+            'tr': 'TR',
  144
+            'tripura': 'TR',
  145
+            'ua': 'UA',
  146
+            'uttarakhand': 'UA',
  147
+            'up': 'UP',
  148
+            'uttar pradesh': 'UP',
  149
+            'westbengal': 'WB',
  150
+            'bengal': 'WB',
  151
+            'wb': 'WB',
  152
+            'west bengal': 'WB'
  153
+        }
  154
+        invalid = {
  155
+            'florida': error_format,
  156
+            'FL': error_format,
  157
+        }
  158
+        self.assertFieldOutput(INStateField, valid, invalid)
1  tests/regressiontests/forms/localflavortests.py
@@ -20,6 +20,7 @@
20 20
 from localflavor.ie import IELocalFlavorTests
21 21
 from localflavor.il import ILLocalFlavorTests
22 22
 from localflavor.is_ import ISLocalFlavorTests
  23
+from localflavor.in_ import INLocalFlavorTests
23 24
 from localflavor.it import ITLocalFlavorTests
24 25
 from localflavor.jp import JPLocalFlavorTests
25 26
 from localflavor.kw import KWLocalFlavorTests
1  tests/regressiontests/forms/tests/__init__.py
@@ -33,6 +33,7 @@
33 33
     IELocalFlavorTests,
34 34
     ILLocalFlavorTests,
35 35
     ISLocalFlavorTests,
  36
+    INLocalFlavorTests,
36 37
     ITLocalFlavorTests,
37 38
     JPLocalFlavorTests,
38 39
     KWLocalFlavorTests,

0 notes on commit 940aed1

Please sign in to comment.
Something went wrong with that request. Please try again.