Ticket #3986: in_localflavor.diff
File in_localflavor.diff, 4.3 KB (added by , 17 years ago) |
---|
-
in_/in_states.py
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 -
in_/__init__.py
1 # __init__.py -
in_/forms.py
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 20 class INStateField(Field): 21 """ 22 A form field that validates its input is a Indian state name or abbreviatio n. 23 It normalizes the input to the standard two-leter vehicle registration 24 abbreviation for the given state or union territory 25 """ 26 def clean(self, value): 27 from in_states import STATES_NORMALIZED # relative import 28 super(INStateField, self).clean(value) 29 if value in EMPTY_VALUES: 30 return u'' 31 try: 32 value = value.strip().lower() 33 except AttributeError: 34 pass 35 else: 36 try: 37 return STATES_NORMALIZED[value.strip().lower()].decode('ascii') 38 except KeyError: 39 pass 40 raise ValidationError(u'Enter a Indian state or territory.') 41 42 class INStateSelect(Select): 43 """ 44 A Select widget that uses a list of Indian states/territories as its choice s. 45 """ 46 def __init__(self, attrs=None): 47 from in_states import STATE_CHOICES # relative import 48 super(INStateSelect, self).__init__(attrs, choices=STATE_CHOICES)