Fixed #15813 -- Updated Indian localflavor to use correct state choices and fixed various other bugs. Thanks, jsdalton.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16480 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-06-28 10:44:33 +00:00
parent a6cd78662e
commit 940aed10b7
5 changed files with 247 additions and 29 deletions

View File

@ -1,24 +1,32 @@
"""
India-specific Form helpers.
"""
import re
from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select
from django.utils.encoding import smart_unicode
from django.utils.translation import gettext
import re
from django.utils.translation import ugettext_lazy as _
class INZipCodeField(RegexField):
default_error_messages = {
'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
'invalid': _(u'Enter a zip code in the format XXXXXX or XXX XXX.'),
}
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(INZipCodeField, self).__init__(r'^\d{6}$',
super(INZipCodeField, self).__init__(r'^\d{3}\s?\d{3}$',
max_length, min_length, *args, **kwargs)
def clean(self, value):
super(INZipCodeField, self).clean(value)
if value in EMPTY_VALUES:
return u''
# Convert to "NNNNNN" if "NNN NNN" given
value = re.sub(r'^(\d{3})\s(\d{3})$', r'\1\2', value)
return value
class INStateField(Field):
"""
A form field that validates its input is a Indian state name or
@ -26,7 +34,7 @@ class INStateField(Field):
registration abbreviation for the given state or union territory
"""
default_error_messages = {
'invalid': u'Enter a Indian state or territory.',
'invalid': _(u'Enter an Indian state or territory.'),
}
def clean(self, value):

View File

@ -47,37 +47,87 @@ STATE_CHOICES = (
)
STATES_NORMALIZED = {
'ka': 'KA',
'karnatka': 'KA',
'tn': 'TN',
'tamilnad': 'TN',
'tamilnadu': 'TN',
'an': 'AN',
'andaman and nicobar': 'AN',
'andra pradesh': 'AP',
'andrapradesh': 'AP',
'andhrapradesh': 'AP',
'ap': 'AP',
'andhra pradesh': 'AP',
'ar': 'AR',
'arunachal pradesh': 'AR',
'assam': 'AS',
'as': 'AS',
'bihar': 'BR',
'br': 'BR',
'cg': 'CG',
'chattisgarh': 'CG',
'ch': 'CH',
'chandigarh': 'CH',
'daman and diu': 'DD',
'dd': 'DD',
'dl': 'DL',
'delhi': 'DL',
'dn': 'DN',
'dadra and nagar haveli': 'DN',
'ga': 'GA',
'goa': 'GA',
'gj': 'GJ',
'gujarat': 'GJ',
'himachal pradesh': 'HP',
'hp': 'HP',
'hr': 'HR',
'haryana': 'HR',
'jharkhand': 'JH',
'jh': 'JH',
'jammu and kashmir': 'JK',
'jk': 'JK',
'karnataka': 'KA',
'karnatka': 'KA',
'ka': 'KA',
'kerala': 'KL',
'kl': 'KL',
'ld': 'LD',
'lakshadweep': 'LD',
'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',
'maharashtra': 'MH',
'meghalaya': 'ML',
'ml': 'ML',
'mn': 'MN',
'manipur': 'MN',
'madhya pradesh': 'MP',
'mp': 'MP',
'mz': 'MZ',
'bengal': 'WB',
'westbengal': 'WB',
'mizoram': 'MZ',
'mizo': 'MZ',
'orisa': 'OR',
'mz': 'MZ',
'nl': 'NL',
'nagaland': 'NL',
'orissa': 'OR',
'odisa': 'OR',
'orisa': 'OR',
'or': 'OR',
'pb': 'PB',
'punjab': 'PB',
'py': 'PY',
'pondicherry': 'PY',
'rajasthan': 'RJ',
'rajastan': 'RJ',
'rj': 'RJ',
'sikkim': 'SK',
'sk': 'SK',
'tamil nadu': 'TN',
'tn': 'TN',
'tamilnadu': 'TN',
'tamilnad': 'TN',
'tr': 'TR',
'tripura': 'TR',
'ua': 'UA',
'uttarakhand': 'UA',
'up': 'UP',
'uttar pradesh': 'UP',
'westbengal': 'WB',
'bengal': 'WB',
'wb': 'WB',
'west bengal': 'WB'
}

View File

@ -0,0 +1,158 @@
import warnings
from django.contrib.localflavor.in_.forms import (INZipCodeField,
INStateField, INStateSelect)
from utils import LocalFlavorTestCase
class INLocalFlavorTests(LocalFlavorTestCase):
def test_INPStateSelect(self):
f = INStateSelect()
out = u'''<select name="state">
<option value="KA">Karnataka</option>
<option value="AP" selected="selected">Andhra Pradesh</option>
<option value="KL">Kerala</option>
<option value="TN">Tamil Nadu</option>
<option value="MH">Maharashtra</option>
<option value="UP">Uttar Pradesh</option>
<option value="GA">Goa</option>
<option value="GJ">Gujarat</option>
<option value="RJ">Rajasthan</option>
<option value="HP">Himachal Pradesh</option>
<option value="JK">Jammu and Kashmir</option>
<option value="AR">Arunachal Pradesh</option>
<option value="AS">Assam</option>
<option value="BR">Bihar</option>
<option value="CG">Chattisgarh</option>
<option value="HR">Haryana</option>
<option value="JH">Jharkhand</option>
<option value="MP">Madhya Pradesh</option>
<option value="MN">Manipur</option>
<option value="ML">Meghalaya</option>
<option value="MZ">Mizoram</option>
<option value="NL">Nagaland</option>
<option value="OR">Orissa</option>
<option value="PB">Punjab</option>
<option value="SK">Sikkim</option>
<option value="TR">Tripura</option>
<option value="UA">Uttarakhand</option>
<option value="WB">West Bengal</option>
<option value="AN">Andaman and Nicobar</option>
<option value="CH">Chandigarh</option>
<option value="DN">Dadra and Nagar Haveli</option>
<option value="DD">Daman and Diu</option>
<option value="DL">Delhi</option>
<option value="LD">Lakshadweep</option>
<option value="PY">Pondicherry</option>
</select>'''
self.assertEqual(f.render('state', 'AP'), out)
def test_INZipCodeField(self):
error_format = [u'Enter a zip code in the format XXXXXX or XXX XXX.']
valid = {
'360311': '360311',
'360 311': '360311',
}
invalid = {
'36 0311': error_format,
'3603111': error_format,
'360 31': error_format,
'36031': error_format,
'O2B 2R3': error_format
}
self.assertFieldOutput(INZipCodeField, valid, invalid)
def test_INStateField(self):
error_format = [u'Enter an Indian state or territory.']
valid = {
'an': 'AN',
'AN': 'AN',
'andaman and nicobar': 'AN',
'andra pradesh': 'AP',
'andrapradesh': 'AP',
'andhrapradesh': 'AP',
'ap': 'AP',
'andhra pradesh': 'AP',
'ar': 'AR',
'arunachal pradesh': 'AR',
'assam': 'AS',
'as': 'AS',
'bihar': 'BR',
'br': 'BR',
'cg': 'CG',
'chattisgarh': 'CG',
'ch': 'CH',
'chandigarh': 'CH',
'daman and diu': 'DD',
'dd': 'DD',
'dl': 'DL',
'delhi': 'DL',
'dn': 'DN',
'dadra and nagar haveli': 'DN',
'ga': 'GA',
'goa': 'GA',
'gj': 'GJ',
'gujarat': 'GJ',
'himachal pradesh': 'HP',
'hp': 'HP',
'hr': 'HR',
'haryana': 'HR',
'jharkhand': 'JH',
'jh': 'JH',
'jammu and kashmir': 'JK',
'jk': 'JK',
'karnataka': 'KA',
'karnatka': 'KA',
'ka': 'KA',
'kerala': 'KL',
'kl': 'KL',
'ld': 'LD',
'lakshadweep': 'LD',
'maharastra': 'MH',
'mh': 'MH',
'maharashtra': 'MH',
'meghalaya': 'ML',
'ml': 'ML',
'mn': 'MN',
'manipur': 'MN',
'madhya pradesh': 'MP',
'mp': 'MP',
'mizoram': 'MZ',
'mizo': 'MZ',
'mz': 'MZ',
'nl': 'NL',
'nagaland': 'NL',
'orissa': 'OR',
'odisa': 'OR',
'orisa': 'OR',
'or': 'OR',
'pb': 'PB',
'punjab': 'PB',
'py': 'PY',
'pondicherry': 'PY',
'rajasthan': 'RJ',
'rajastan': 'RJ',
'rj': 'RJ',
'sikkim': 'SK',
'sk': 'SK',
'tamil nadu': 'TN',
'tn': 'TN',
'tamilnadu': 'TN',
'tamilnad': 'TN',
'tr': 'TR',
'tripura': 'TR',
'ua': 'UA',
'uttarakhand': 'UA',
'up': 'UP',
'uttar pradesh': 'UP',
'westbengal': 'WB',
'bengal': 'WB',
'wb': 'WB',
'west bengal': 'WB'
}
invalid = {
'florida': error_format,
'FL': error_format,
}
self.assertFieldOutput(INStateField, valid, invalid)

View File

@ -20,6 +20,7 @@ from localflavor.id import IDLocalFlavorTests
from localflavor.ie import IELocalFlavorTests
from localflavor.il import ILLocalFlavorTests
from localflavor.is_ import ISLocalFlavorTests
from localflavor.in_ import INLocalFlavorTests
from localflavor.it import ITLocalFlavorTests
from localflavor.jp import JPLocalFlavorTests
from localflavor.kw import KWLocalFlavorTests

View File

@ -33,6 +33,7 @@ from regressiontests.forms.localflavortests import (
IELocalFlavorTests,
ILLocalFlavorTests,
ISLocalFlavorTests,
INLocalFlavorTests,
ITLocalFlavorTests,
JPLocalFlavorTests,
KWLocalFlavorTests,