Ticket #11537: 11537.diff

File 11537.diff, 10.6 KB (added by lars, 15 years ago)

Patch cleans up the STATE_CHOICES and adds a test

  • django/contrib/localflavor/in_/forms.py

     
    55from django.forms import ValidationError
    66from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
    77from django.utils.encoding import smart_unicode
    8 from django.utils.translation import gettext
    9 import re
     8from django.utils.translation import ugettext_lazy as _
    109
    1110
    1211class INZipCodeField(RegexField):
    1312    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.'),
    1514    }
    1615
    1716    def __init__(self, *args, **kwargs):
    1817        super(INZipCodeField, self).__init__(r'^\d{6}$',
    1918            max_length=None, min_length=None, *args, **kwargs)
    2019
     20
    2121class INStateField(Field):
    2222    """
    2323    A form field that validates its input is a Indian state name or
    2424    abbreviation. It normalizes the input to the standard two-letter vehicle
    2525    registration abbreviation for the given state or union territory
    2626    """
     27
    2728    default_error_messages = {
    2829        'invalid': u'Enter a Indian state or territory.',
    2930    }
    3031
    3132    def clean(self, value):
    32         from in_states import STATES_NORMALIZED
     33        from in_states import REGIONS_NORMALIZED
    3334        super(INStateField, self).clean(value)
    3435        if value in EMPTY_VALUES:
    3536            return u''
     
    3940            pass
    4041        else:
    4142            try:
    42                 return smart_unicode(STATES_NORMALIZED[value.strip().lower()])
     43                return smart_unicode(REGIONS_NORMALIZED[value.strip().lower()])
    4344            except KeyError:
    4445                pass
    4546        raise ValidationError(self.error_messages['invalid'])
    4647
     48
    4749class INStateSelect(Select):
    4850    """
    4951    A Select widget that uses a list of Indian states/territories as its
    5052    choices.
    5153    """
     54
    5255    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

     
    11"""
     2
    23A mapping of state misspellings/abbreviations to normalized abbreviations, and
    34an alphabetical list of states for use as `choices` in a formfield.
    45
    56This exists in this standalone file so that it's only imported into memory
    67when explicitly needed.
     8
    79"""
     10from django.utils.translation import ugettext_lazy as _
    811
    912STATE_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)
    3842
    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
     44TERRITORY_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')),
    4752)
    4853
    49 STATES_NORMALIZED = {
    50     'ka': 'KA',
    51     'karnatka': 'KA',
    52     'tn': 'TN',
    53     'tamilnad': 'TN',
    54     'tamilnadu': 'TN',
     54REGION_CHOICES = STATE_CHOICES + TERRITORY_CHOICES
     55
     56REGIONS_NORMALIZED = {
     57    # States
     58    'ap': 'AP',
    5559    'andra pradesh': 'AP',
    5660    'andrapradesh': 'AP',
    5761    'andhrapradesh': 'AP',
    58     'maharastra': 'MH',
    59     'mh': 'MH',
    60     'ap': 'AP',
    61     'dl': 'DL',
    62     'dd': 'DD',
     62    'ar': 'AR',
     63    'as': 'AS',
    6364    'br': 'BR',
    64     'ar': 'AR',
    65     'sk': 'SK',
    66     'kl': 'KL',
     65    'cg': 'CG',
    6766    'ga': 'GA',
    68     'rj': 'RJ',
    69     'rajastan': 'RJ',
    70     'rajasthan': 'RJ',
     67    'gj': 'GJ',
     68    'hr': 'HR',
    7169    'hp': 'HP',
    72     'ua': 'UA',
    73     'up': 'UP',
     70    'jk': 'JK',
     71    'ka': 'KA',
     72    'karnatka': 'KA',
     73    'kl': 'KL',
    7474    'mp': 'MP',
     75    'mh': 'MH',
     76    'maharastra': 'MH',
     77    'mn': 'MN',
     78    'ml': 'ML',
    7579    'mz': 'MZ',
    76     'bengal': 'WB',
    77     'westbengal': 'WB',
    7880    'mizo': 'MZ',
     81    'nl': 'NL',
    7982    'orisa': 'OR',
    8083    'odisa': 'OR',
    8184    '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',
    83112}
    84 
  • tests/regressiontests/forms/localflavor/in_.py

     
     1# -*- coding: utf-8 -*-
     2
     3tests = r"""
     4# INZipCodeField ##############################################################
     5
     6>>> from django.contrib.localflavor.in_.forms import INZipCodeField
     7>>> f = INZipCodeField()
     8>>> f.clean('456001')
     9u'456001'
     10>>> f.clean(' 456001')
     11Traceback (most recent call last):
     12...
     13ValidationError: [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')
     20u'UA'
     21>>> f.clean('MZ')
     22u'MZ'
     23>>> f.clean('mz')
     24u'MZ'
     25>>> f.clean('wb')
     26u'WB'
     27>>> f.clean('  maharastra ')
     28u'MH'
     29>>> f.clean(60606)
     30Traceback (most recent call last):
     31...
     32ValidationError: [u'Enter a Indian state or territory.']
     33>>> f.clean(None)
     34Traceback (most recent call last):
     35...
     36ValidationError: [u'This field is required.']
     37>>> f.clean('')
     38Traceback (most recent call last):
     39...
     40ValidationError: [u'This field is required.']
     41
     42>>> f = INStateField(required=False)
     43>>> f.clean('mz')
     44u'MZ'
     45>>> f.clean('MZ')
     46u'MZ'
     47>>> f.clean(60606)
     48Traceback (most recent call last):
     49...
     50ValidationError: [u'Enter a Indian state or territory.']
     51>>> f.clean(None)
     52u''
     53>>> f.clean('')
     54u''
     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

     
    1616from localflavor.fi import tests as localflavor_fi_tests
    1717from localflavor.fr import tests as localflavor_fr_tests
    1818from localflavor.generic import tests as localflavor_generic_tests
     19from localflavor.in_ import tests as localflavor_in_tests
    1920from localflavor.is_ import tests as localflavor_is_tests
    2021from localflavor.it import tests as localflavor_it_tests
    2122from localflavor.jp import tests as localflavor_jp_tests
     
    5051    'localflavor_fi_tests': localflavor_fi_tests,
    5152    'localflavor_fr_tests': localflavor_fr_tests,
    5253    'localflavor_generic_tests': localflavor_generic_tests,
     54    'localflavor_in_tests': localflavor_in_tests,
    5355    'localflavor_is_tests': localflavor_is_tests,
    5456    'localflavor_it_tests': localflavor_it_tests,
    5557    'localflavor_jp_tests': localflavor_jp_tests,
Back to Top