From 9309a8d6c3d11133270a891e799b3990e4fa5152 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 11 Aug 2007 10:04:19 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + django/contrib/in_/__init__.py | 1 + django/contrib/in_/forms.py | 49 +++++++++++++++++++ django/contrib/in_/in_states.py | 84 +++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 django/contrib/in_/__init__.py create mode 100644 django/contrib/in_/forms.py create mode 100644 django/contrib/in_/in_states.py diff --git a/AUTHORS b/AUTHORS index 6d27742790..d5faf2f244 100644 --- a/AUTHORS +++ b/AUTHORS @@ -120,6 +120,7 @@ answer newbie questions, and generally made Django that much better: martin.glueck@gmail.com GomoX Mario Gonzalez + pradeep.gowda@gmail.com Simon Greenhill Owen Griffiths Espen Grindhaug diff --git a/django/contrib/in_/__init__.py b/django/contrib/in_/__init__.py new file mode 100644 index 0000000000..143f486c05 --- /dev/null +++ b/django/contrib/in_/__init__.py @@ -0,0 +1 @@ +# __init__.py diff --git a/django/contrib/in_/forms.py b/django/contrib/in_/forms.py new file mode 100644 index 0000000000..5febc40e67 --- /dev/null +++ b/django/contrib/in_/forms.py @@ -0,0 +1,49 @@ +""" +India-specific Form helpers. +""" + +from django.newforms import ValidationError +from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES +from django.utils.encoding import smart_unicode +from django.utils.translation import gettext +import re + + +class INZipCodeField(RegexField): + def __init__(self, *args, **kwargs): + super(INZipCodeField, self).__init__(r'^\d{6}$', + max_length=None, min_length=None, + error_message=gettext(u'Enter a zip code in the format XXXXXXX.'), + *args, **kwargs) + +class INStateField(Field): + """ + A form field that validates its input is a Indian state name or + abbreviation. It normalizes the input to the standard two-letter vehicle + registration abbreviation for the given state or union territory + """ + def clean(self, value): + from in_states import STATES_NORMALIZED + super(INStateField, self).clean(value) + if value in EMPTY_VALUES: + return u'' + try: + value = value.strip().lower() + except AttributeError: + pass + else: + try: + return smart_unicode(STATES_NORMALIZED[value.strip().lower()]) + except KeyError: + pass + raise ValidationError(u'Enter a Indian state or territory.') + +class INStateSelect(Select): + """ + A Select widget that uses a list of Indian states/territories as its + choices. + """ + def __init__(self, attrs=None): + from in_states import STATE_CHOICES + super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES) + diff --git a/django/contrib/in_/in_states.py b/django/contrib/in_/in_states.py new file mode 100644 index 0000000000..faa3a9101b --- /dev/null +++ b/django/contrib/in_/in_states.py @@ -0,0 +1,84 @@ +""" +A mapping of state misspellings/abbreviations to normalized abbreviations, and +an alphabetical list of states for use as `choices` in a formfield. + +This exists in this standalone file so that it's only imported into memory +when explicitly needed. +""" + +STATE_CHOICES = ( + 'KA', 'Karnataka', + 'AP', 'Andhra Pradesh', + 'KL', 'Kerala', + 'TN', 'Tamil Nadu', + 'MH', 'Maharashtra', + 'UP', 'Uttar Pradesh', + 'GA', 'Goa', + 'GJ', 'Gujarat', + 'RJ', 'Rajasthan', + 'HP', 'Himachal Pradesh', + 'JK', 'Jammu and Kashmir', + 'AR', 'Arunachal Pradesh', + 'AS', 'Assam', + 'BR', 'Bihar', + 'CG', 'Chattisgarh', + 'HR', 'Haryana', + 'JH', 'Jharkhand', + 'MP', 'Madhya Pradesh', + 'MN', 'Manipur', + 'ML', 'Meghalaya', + 'MZ', 'Mizoram', + 'NL', 'Nagaland', + 'OR', 'Orissa', + 'PB', 'Punjab', + 'SK', 'Sikkim', + 'TR', 'Tripura', + 'UA', 'Uttarakhand', + 'WB', 'West Bengal', + #Union Territories + 'AN', 'Andaman and Nicobar', + 'CH', 'Chandigarh', + 'DN', 'Dadra and Nagar Haveli', + 'DD', 'Daman and Diu', + 'DL', 'Delhi', + 'LD', 'Lakshadweep', + 'PY', 'Pondicherry', +) + +STATES_NORMALIZED = { + 'ka': 'KA', + 'karnatka': 'KA', + 'tn': 'TN', + 'tamilnad': 'TN', + 'tamilnadu': 'TN', + 'andra pradesh': 'AP', + 'andrapradesh': 'AP', + 'andhrapradesh': 'AP', + 'maharastra': 'MH', + 'mh': 'MH', + 'ap': 'AP', + 'dl': 'DL', + 'dd': 'DD', + 'br': 'BR', + 'ar': 'AR', + 'sk': 'SK', + 'kl': 'KL', + 'ga': 'GA', + 'rj': 'RJ', + 'rajastan': 'RJ', + 'rajasthan': 'RJ', + 'hp': 'HP', + 'ua': 'UA', + 'up': 'UP', + 'mp': 'MP', + 'mz': 'MZ', + 'bengal': 'WB', + 'westbengal': 'WB', + 'mizo': 'MZ', + 'orisa': 'OR', + 'odisa': 'OR', + 'or': 'OR', + 'ar': 'AR', + + } +