Ticket #11537: 11537.diff
File 11537.diff, 10.6 KB (added by , 15 years ago) |
---|
-
django/contrib/localflavor/in_/forms.py
5 5 from django.forms import ValidationError 6 6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES 7 7 from django.utils.encoding import smart_unicode 8 from django.utils.translation import gettext 9 import re 8 from django.utils.translation import ugettext_lazy as _ 10 9 11 10 12 11 class INZipCodeField(RegexField): 13 12 default_error_messages = { 14 'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),13 'invalid': _('Enter a zip code in the format XXXXXX.'), 15 14 } 16 15 17 16 def __init__(self, *args, **kwargs): 18 17 super(INZipCodeField, self).__init__(r'^\d{6}$', 19 18 max_length=None, min_length=None, *args, **kwargs) 20 19 20 21 21 class INStateField(Field): 22 22 """ 23 23 A form field that validates its input is a Indian state name or 24 24 abbreviation. It normalizes the input to the standard two-letter vehicle 25 25 registration abbreviation for the given state or union territory 26 26 """ 27 27 28 default_error_messages = { 28 29 'invalid': u'Enter a Indian state or territory.', 29 30 } 30 31 31 32 def clean(self, value): 32 from in_states import STATES_NORMALIZED33 from in_states import REGIONS_NORMALIZED 33 34 super(INStateField, self).clean(value) 34 35 if value in EMPTY_VALUES: 35 36 return u'' … … 39 40 pass 40 41 else: 41 42 try: 42 return smart_unicode( STATES_NORMALIZED[value.strip().lower()])43 return smart_unicode(REGIONS_NORMALIZED[value.strip().lower()]) 43 44 except KeyError: 44 45 pass 45 46 raise ValidationError(self.error_messages['invalid']) 46 47 48 47 49 class INStateSelect(Select): 48 50 """ 49 51 A Select widget that uses a list of Indian states/territories as its 50 52 choices. 51 53 """ 54 52 55 def __init__(self, attrs=None): 53 from in_states import STATE_CHOICES 54 super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 55 56 from in_states import REGION_CHOICES 57 super(INStateSelect, self).__init__(attrs, choices=REGION_CHOICES) -
django/contrib/localflavor/in_/in_states.py
1 1 """ 2 2 3 A mapping of state misspellings/abbreviations to normalized abbreviations, and 3 4 an alphabetical list of states for use as `choices` in a formfield. 4 5 5 6 This exists in this standalone file so that it's only imported into memory 6 7 when explicitly needed. 8 7 9 """ 10 from django.utils.translation import ugettext_lazy as _ 8 11 9 12 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', 13 ('AP', _('Andhra Pradesh')), 14 ('AR', _('Arunachal Pradesh')), 15 ('AS', _('Assam')), 16 ('BR', _('Bihar')), 17 ('CG', _('Chattisgarh')), 18 ('GA', _('Goa')), 19 ('GJ', _('Gujarat')), 20 ('HR', _('Haryana')), 21 ('HP', _('Himachal Pradesh')), 22 ('JK', _('Jammu and Kashmir')), 23 ('JH', _('Jharkhand')), 24 ('KA', _('Karnataka')), 25 ('KL', _('Kerala')), 26 ('MP', _('Madhya Pradesh')), 27 ('MH', _('Maharashtra')), 28 ('MN', _('Manipur')), 29 ('ML', _('Meghalaya')), 30 ('MZ', _('Mizoram')), 31 ('NL', _('Nagaland')), 32 ('OR', _('Orissa')), 33 ('PB', _('Punjab')), 34 ('RJ', _('Rajasthan')), 35 ('SK', _('Sikkim')), 36 ('TN', _('Tamil Nadu')), 37 ('TR', _('Tripura')), 38 ('UP', _('Uttar Pradesh')), 39 ('UA', _('Uttarakhand')), 40 ('WB', _('West Bengal')), 41 ) 38 42 39 # Union Territories 40 'AN', 'Andaman and Nicobar', 41 'CH', 'Chandigarh', 42 'DN', 'Dadra and Nagar Haveli', 43 'DD', 'Daman and Diu', 44 'DL', 'Delhi', 45 'LD', 'Lakshadweep', 46 'PY', 'Pondicherry', 43 # Union Territories 44 TERRITORY_CHOICES = ( 45 ('AN', _('Andaman and Nicobar Islands')), 46 ('CH', _('Chandigarh')), 47 ('DD', _('Daman and Diu')), 48 ('DL', _('Delhi')), 49 ('DN', _('Dadra and Nagar Haveli')), 50 ('LD', _('Lakshadweep')), 51 ('PY', _('Puducherry')), 47 52 ) 48 53 49 STATES_NORMALIZED = { 50 'ka': 'KA', 51 'karnatka': 'KA', 52 'tn': 'TN', 53 'tamilnad': 'TN', 54 'tamilnadu': 'TN', 54 REGION_CHOICES = STATE_CHOICES + TERRITORY_CHOICES 55 56 REGIONS_NORMALIZED = { 57 # States 58 'ap': 'AP', 55 59 'andra pradesh': 'AP', 56 60 'andrapradesh': 'AP', 57 61 'andhrapradesh': 'AP', 58 'maharastra': 'MH', 59 'mh': 'MH', 60 'ap': 'AP', 61 'dl': 'DL', 62 'dd': 'DD', 62 'ar': 'AR', 63 'as': 'AS', 63 64 'br': 'BR', 64 'ar': 'AR', 65 'sk': 'SK', 66 'kl': 'KL', 65 'cg': 'CG', 67 66 'ga': 'GA', 68 'rj': 'RJ', 69 'rajastan': 'RJ', 70 'rajasthan': 'RJ', 67 'gj': 'GJ', 68 'hr': 'HR', 71 69 'hp': 'HP', 72 'ua': 'UA', 73 'up': 'UP', 70 'jk': 'JK', 71 'ka': 'KA', 72 'karnatka': 'KA', 73 'kl': 'KL', 74 74 'mp': 'MP', 75 'mh': 'MH', 76 'maharastra': 'MH', 77 'mn': 'MN', 78 'ml': 'ML', 75 79 'mz': 'MZ', 76 'bengal': 'WB',77 'westbengal': 'WB',78 80 'mizo': 'MZ', 81 'nl': 'NL', 79 82 'orisa': 'OR', 80 83 'odisa': 'OR', 81 84 'or': 'OR', 82 'ar': 'AR', 85 'pb': 'pb', 86 'rj': 'RJ', 87 'rajastan': 'RJ', 88 'rajasthan': 'RJ', 89 'sk': 'SK', 90 'tn': 'TN', 91 'tamilnad': 'TN', 92 'tamilnadu': 'TN', 93 'tr': 'TR', 94 'up': 'UP', 95 'ua': 'UA', 96 'ul': 'UA', 97 'uk': 'UA', 98 'uttaranchal': 'UA', 99 'wb': 'WB', 100 'bengal': 'WB', 101 'westbengal': 'WB', 102 103 # Territories 104 'an': 'AN', 105 'ch': 'CH', 106 'dd': 'DD', 107 'dl': 'DL', 108 'dn': 'DN', 109 'ld': 'LD', 110 'py': 'PY', 111 'pondicherry': 'PY', 83 112 } 84 -
tests/regressiontests/forms/localflavor/in_.py
1 # -*- coding: utf-8 -*- 2 3 tests = r""" 4 # INZipCodeField ############################################################## 5 6 >>> from django.contrib.localflavor.in_.forms import INZipCodeField 7 >>> f = INZipCodeField() 8 >>> f.clean('456001') 9 u'456001' 10 >>> f.clean(' 456001') 11 Traceback (most recent call last): 12 ... 13 ValidationError: [u'Enter a zip code in the format XXXXXX.'] 14 15 # INStateField ################################################################ 16 17 >>> from django.contrib.localflavor.in_.forms import INStateField 18 >>> f = INStateField() 19 >>> f.clean('uttaranchal') 20 u'UA' 21 >>> f.clean('MZ') 22 u'MZ' 23 >>> f.clean('mz') 24 u'MZ' 25 >>> f.clean('wb') 26 u'WB' 27 >>> f.clean(' maharastra ') 28 u'MH' 29 >>> f.clean(60606) 30 Traceback (most recent call last): 31 ... 32 ValidationError: [u'Enter a Indian state or territory.'] 33 >>> f.clean(None) 34 Traceback (most recent call last): 35 ... 36 ValidationError: [u'This field is required.'] 37 >>> f.clean('') 38 Traceback (most recent call last): 39 ... 40 ValidationError: [u'This field is required.'] 41 42 >>> f = INStateField(required=False) 43 >>> f.clean('mz') 44 u'MZ' 45 >>> f.clean('MZ') 46 u'MZ' 47 >>> f.clean(60606) 48 Traceback (most recent call last): 49 ... 50 ValidationError: [u'Enter a Indian state or territory.'] 51 >>> f.clean(None) 52 u'' 53 >>> f.clean('') 54 u'' 55 56 # INStateSelect ############################################################# 57 58 >>> from django.contrib.localflavor.in_.forms import INStateSelect 59 >>> w = INStateSelect() 60 >>> print w.render('states', 'UA') 61 <select name="states"> 62 <option value="AP">Andhra Pradesh</option> 63 <option value="AR">Arunachal Pradesh</option> 64 <option value="AS">Assam</option> 65 <option value="BR">Bihar</option> 66 <option value="CG">Chattisgarh</option> 67 <option value="GA">Goa</option> 68 <option value="GJ">Gujarat</option> 69 <option value="HR">Haryana</option> 70 <option value="HP">Himachal Pradesh</option> 71 <option value="JK">Jammu and Kashmir</option> 72 <option value="JH">Jharkhand</option> 73 <option value="KA">Karnataka</option> 74 <option value="KL">Kerala</option> 75 <option value="MP">Madhya Pradesh</option> 76 <option value="MH">Maharashtra</option> 77 <option value="MN">Manipur</option> 78 <option value="ML">Meghalaya</option> 79 <option value="MZ">Mizoram</option> 80 <option value="NL">Nagaland</option> 81 <option value="OR">Orissa</option> 82 <option value="PB">Punjab</option> 83 <option value="RJ">Rajasthan</option> 84 <option value="SK">Sikkim</option> 85 <option value="TN">Tamil Nadu</option> 86 <option value="TR">Tripura</option> 87 <option value="UP">Uttar Pradesh</option> 88 <option value="UA" selected="selected">Uttarakhand</option> 89 <option value="WB">West Bengal</option> 90 <option value="AN">Andaman and Nicobar Islands</option> 91 <option value="CH">Chandigarh</option> 92 <option value="DD">Daman and Diu</option> 93 <option value="DL">Delhi</option> 94 <option value="DN">Dadra and Nagar Haveli</option> 95 <option value="LD">Lakshadweep</option> 96 <option value="PY">Puducherry</option> 97 </select> 98 """ -
tests/regressiontests/forms/tests.py
16 16 from localflavor.fi import tests as localflavor_fi_tests 17 17 from localflavor.fr import tests as localflavor_fr_tests 18 18 from localflavor.generic import tests as localflavor_generic_tests 19 from localflavor.in_ import tests as localflavor_in_tests 19 20 from localflavor.is_ import tests as localflavor_is_tests 20 21 from localflavor.it import tests as localflavor_it_tests 21 22 from localflavor.jp import tests as localflavor_jp_tests … … 50 51 'localflavor_fi_tests': localflavor_fi_tests, 51 52 'localflavor_fr_tests': localflavor_fr_tests, 52 53 'localflavor_generic_tests': localflavor_generic_tests, 54 'localflavor_in_tests': localflavor_in_tests, 53 55 'localflavor_is_tests': localflavor_is_tests, 54 56 'localflavor_it_tests': localflavor_it_tests, 55 57 'localflavor_jp_tests': localflavor_jp_tests,