From 940aed10b7bdd8f25aff38b8b923deb94ce4cee4 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 28 Jun 2011 10:44:33 +0000 Subject: [PATCH] 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 --- django/contrib/localflavor/in_/forms.py | 18 +- django/contrib/localflavor/in_/in_states.py | 98 ++++++++--- .../regressiontests/forms/localflavor/in_.py | 158 ++++++++++++++++++ .../regressiontests/forms/localflavortests.py | 1 + tests/regressiontests/forms/tests/__init__.py | 1 + 5 files changed, 247 insertions(+), 29 deletions(-) create mode 100644 tests/regressiontests/forms/localflavor/in_.py diff --git a/django/contrib/localflavor/in_/forms.py b/django/contrib/localflavor/in_/forms.py index c94c7e7a1e1..afc5534f92c 100644 --- a/django/contrib/localflavor/in_/forms.py +++ b/django/contrib/localflavor/in_/forms.py @@ -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): diff --git a/django/contrib/localflavor/in_/in_states.py b/django/contrib/localflavor/in_/in_states.py index 25b220b94ae..375b201311f 100644 --- a/django/contrib/localflavor/in_/in_states.py +++ b/django/contrib/localflavor/in_/in_states.py @@ -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' } - diff --git a/tests/regressiontests/forms/localflavor/in_.py b/tests/regressiontests/forms/localflavor/in_.py new file mode 100644 index 00000000000..17553667a79 --- /dev/null +++ b/tests/regressiontests/forms/localflavor/in_.py @@ -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'''''' + 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) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 4bd934199e9..7f29feae3f1 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -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 diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 46cc5faf47a..67154514d72 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -33,6 +33,7 @@ from regressiontests.forms.localflavortests import ( IELocalFlavorTests, ILLocalFlavorTests, ISLocalFlavorTests, + INLocalFlavorTests, ITLocalFlavorTests, JPLocalFlavorTests, KWLocalFlavorTests,