Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3986 -- Added Indian localflavor. Thanks, pradeep.gowda@gmail.…

…com.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5847 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9309a8d6c3d11133270a891e799b3990e4fa5152 1 parent 9864c9d
Malcolm Tredinnick authored August 11, 2007
1  AUTHORS
@@ -120,6 +120,7 @@ answer newbie questions, and generally made Django that much better:
120 120
     martin.glueck@gmail.com
121 121
     GomoX <gomo@datafull.com>
122 122
     Mario Gonzalez <gonzalemario@gmail.com>
  123
+    pradeep.gowda@gmail.com
123 124
     Simon Greenhill <dev@simon.net.nz>
124 125
     Owen Griffiths
125 126
     Espen Grindhaug <http://grindhaug.org/>
1  django/contrib/in_/__init__.py
... ...
@@ -0,0 +1 @@
  1
+# __init__.py
49  django/contrib/in_/forms.py
... ...
@@ -0,0 +1,49 @@
  1
+"""
  2
+India-specific Form helpers.
  3
+"""
  4
+
  5
+from django.newforms import ValidationError
  6
+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
  7
+from django.utils.encoding import smart_unicode
  8
+from django.utils.translation import gettext
  9
+import re
  10
+
  11
+
  12
+class INZipCodeField(RegexField):
  13
+    def __init__(self, *args, **kwargs):
  14
+        super(INZipCodeField, self).__init__(r'^\d{6}$',
  15
+            max_length=None, min_length=None,
  16
+            error_message=gettext(u'Enter a zip code in the format XXXXXXX.'),
  17
+            *args, **kwargs)
  18
+
  19
+class INStateField(Field):
  20
+    """
  21
+    A form field that validates its input is a Indian state name or
  22
+    abbreviation. It normalizes the input to the standard two-letter vehicle
  23
+    registration abbreviation for the given state or union territory
  24
+    """
  25
+    def clean(self, value):
  26
+        from in_states import STATES_NORMALIZED
  27
+        super(INStateField, self).clean(value)
  28
+        if value in EMPTY_VALUES:
  29
+            return u''
  30
+        try:
  31
+            value = value.strip().lower()
  32
+        except AttributeError:
  33
+            pass
  34
+        else:
  35
+            try:
  36
+                return smart_unicode(STATES_NORMALIZED[value.strip().lower()])
  37
+            except KeyError:
  38
+                pass
  39
+        raise ValidationError(u'Enter a Indian state or territory.')
  40
+
  41
+class INStateSelect(Select):
  42
+    """
  43
+    A Select widget that uses a list of Indian states/territories as its
  44
+    choices.
  45
+    """
  46
+    def __init__(self, attrs=None):
  47
+        from in_states import STATE_CHOICES
  48
+        super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  49
+
84  django/contrib/in_/in_states.py
... ...
@@ -0,0 +1,84 @@
  1
+"""
  2
+A mapping of state misspellings/abbreviations to normalized abbreviations, and
  3
+an alphabetical list of states for use as `choices` in a formfield.
  4
+
  5
+This exists in this standalone file so that it's only imported into memory
  6
+when explicitly needed.
  7
+"""
  8
+
  9
+STATE_CHOICES = (
  10
+    'KA', 'Karnataka',
  11
+    'AP', 'Andhra Pradesh',
  12
+    'KL', 'Kerala',
  13
+    'TN', 'Tamil Nadu',
  14
+    'MH', 'Maharashtra',
  15
+    'UP', 'Uttar Pradesh',
  16
+    'GA', 'Goa',
  17
+    'GJ', 'Gujarat',
  18
+    'RJ', 'Rajasthan',
  19
+    'HP', 'Himachal Pradesh',
  20
+    'JK', 'Jammu and Kashmir',
  21
+    'AR', 'Arunachal Pradesh',
  22
+    'AS', 'Assam',
  23
+    'BR', 'Bihar',
  24
+    'CG', 'Chattisgarh',
  25
+    'HR', 'Haryana',
  26
+    'JH', 'Jharkhand',
  27
+    'MP', 'Madhya Pradesh',
  28
+    'MN', 'Manipur',
  29
+    'ML', 'Meghalaya',
  30
+    'MZ', 'Mizoram',
  31
+    'NL', 'Nagaland',
  32
+    'OR', 'Orissa',
  33
+    'PB', 'Punjab',
  34
+    'SK', 'Sikkim',
  35
+    'TR', 'Tripura',
  36
+    'UA', 'Uttarakhand',
  37
+    'WB', 'West Bengal',
  38
+    #Union Territories
  39
+    'AN', 'Andaman and Nicobar',
  40
+    'CH', 'Chandigarh',
  41
+    'DN', 'Dadra and Nagar Haveli',
  42
+    'DD', 'Daman and Diu',
  43
+    'DL', 'Delhi',
  44
+    'LD', 'Lakshadweep',
  45
+    'PY', 'Pondicherry',
  46
+)
  47
+
  48
+STATES_NORMALIZED = {
  49
+    'ka': 'KA',
  50
+    'karnatka': 'KA',
  51
+    'tn': 'TN',
  52
+    'tamilnad': 'TN',
  53
+    'tamilnadu': 'TN',
  54
+    'andra pradesh': 'AP',
  55
+    'andrapradesh': 'AP',
  56
+    'andhrapradesh': 'AP',
  57
+    'maharastra': 'MH',
  58
+    'mh': 'MH',
  59
+    'ap': 'AP',
  60
+    'dl': 'DL',
  61
+    'dd': 'DD',
  62
+    'br': 'BR',
  63
+    'ar': 'AR',
  64
+    'sk': 'SK',
  65
+    'kl': 'KL',
  66
+    'ga': 'GA',
  67
+    'rj': 'RJ',
  68
+    'rajastan': 'RJ',
  69
+    'rajasthan': 'RJ',
  70
+    'hp': 'HP',
  71
+    'ua': 'UA',
  72
+    'up': 'UP',
  73
+    'mp': 'MP',
  74
+    'mz': 'MZ',
  75
+    'bengal': 'WB',
  76
+    'westbengal': 'WB',
  77
+    'mizo': 'MZ',
  78
+    'orisa': 'OR',
  79
+    'odisa': 'OR',
  80
+    'or': 'OR',
  81
+    'ar': 'AR',
  82
+
  83
+    }
  84
+

0 notes on commit 9309a8d

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