Ticket #14937: us_postal_codes.diff

File us_postal_codes.diff, 15.3 KB (added by James Bennett, 14 years ago)
  • django/contrib/localflavor/us/us_states.py

     
    11"""
    2 A mapping of state misspellings/abbreviations to normalized abbreviations, and
    3 an alphabetical list of states for use as `choices` in a formfield.
     2A mapping of state misspellings/abbreviations to normalized
     3abbreviations, and alphabetical lists of US states, territories,
     4military mail regions and non-US states to which the US provides
     5postal service.
    46
    57This exists in this standalone file so that it's only imported into memory
    68when explicitly needed.
    79"""
    810
    9 STATE_CHOICES = (
     11# The 48 contiguous states, plus the District of Columbia.
     12CONTIGUOUS_STATES = (
    1013    ('AL', 'Alabama'),
     14    ('AZ', 'Arizona'),
     15    ('AR', 'Arkansas'),
     16    ('CA', 'California'),
     17    ('CO', 'Colorado'),
     18    ('CT', 'Connecticut'),
     19    ('DE', 'Delaware'),
     20    ('DC', 'District of Columbia'),
     21    ('FL', 'Florida'),
     22    ('GA', 'Georgia'),
     23    ('ID', 'Idaho'),
     24    ('IL', 'Illinois'),
     25    ('IN', 'Indiana'),
     26    ('IA', 'Iowa'),
     27    ('KS', 'Kansas'),
     28    ('KY', 'Kentucky'),
     29    ('LA', 'Louisiana'),
     30    ('ME', 'Maine'),
     31    ('MD', 'Maryland'),
     32    ('MA', 'Massachusetts'),
     33    ('MI', 'Michigan'),
     34    ('MN', 'Minnesota'),
     35    ('MS', 'Mississippi'),
     36    ('MO', 'Missouri'),
     37    ('MT', 'Montana'),
     38    ('NE', 'Nebraska'),
     39    ('NV', 'Nevada'),
     40    ('NH', 'New Hampshire'),
     41    ('NJ', 'New Jersey'),
     42    ('NM', 'New Mexico'),
     43    ('NY', 'New York'),
     44    ('NC', 'North Carolina'),
     45    ('ND', 'North Dakota'),
     46    ('OH', 'Ohio'),
     47    ('OK', 'Oklahoma'),
     48    ('OR', 'Oregon'),
     49    ('PA', 'Pennsylvania'),
     50    ('RI', 'Rhode Island'),
     51    ('SC', 'South Carolina'),
     52    ('SD', 'South Dakota'),
     53    ('TN', 'Tennessee'),
     54    ('TX', 'Texas'),
     55    ('UT', 'Utah'),
     56    ('VT', 'Vermont'),
     57    ('VA', 'Virginia'),
     58    ('WA', 'Washington'),
     59    ('WV', 'West Virginia'),
     60    ('WI', 'Wisconsin'),
     61    ('WY', 'Wyoming'),
     62)
     63
     64# All 50 states, plus the District of Columbia.
     65US_STATES = (
     66    ('AL', 'Alabama'),
    1167    ('AK', 'Alaska'),
    12     ('AS', 'American Samoa'),
    1368    ('AZ', 'Arizona'),
    1469    ('AR', 'Arkansas'),
    1570    ('CA', 'California'),
     
    1974    ('DC', 'District of Columbia'),
    2075    ('FL', 'Florida'),
    2176    ('GA', 'Georgia'),
    22     ('GU', 'Guam'),
    2377    ('HI', 'Hawaii'),
    2478    ('ID', 'Idaho'),
    2579    ('IL', 'Illinois'),
     
    4498    ('NY', 'New York'),
    4599    ('NC', 'North Carolina'),
    46100    ('ND', 'North Dakota'),
    47     ('MP', 'Northern Mariana Islands'),
    48101    ('OH', 'Ohio'),
    49102    ('OK', 'Oklahoma'),
    50103    ('OR', 'Oregon'),
    51104    ('PA', 'Pennsylvania'),
    52     ('PR', 'Puerto Rico'),
    53105    ('RI', 'Rhode Island'),
    54106    ('SC', 'South Carolina'),
    55107    ('SD', 'South Dakota'),
     
    57109    ('TX', 'Texas'),
    58110    ('UT', 'Utah'),
    59111    ('VT', 'Vermont'),
    60     ('VI', 'Virgin Islands'),
    61112    ('VA', 'Virginia'),
    62113    ('WA', 'Washington'),
    63114    ('WV', 'West Virginia'),
     
    65116    ('WY', 'Wyoming'),
    66117)
    67118
     119# Non-state territories.
     120US_TERRITORIES = (
     121    ('AS', 'American Samoa'),
     122    ('GU', 'Guam'),
     123    ('MP', 'Northern Mariana Islands'),
     124    ('PR', 'Puerto Rico'),
     125    ('VI', 'Virgin Islands'),
     126)
     127
     128# Military postal "states". Note that 'AE' actually encompasses
     129# Europe, Canada, Africa and the Middle East.
     130ARMED_FORCES_STATES = (
     131    ('AA', 'Armed Forces Americas'),
     132    ('AE', 'Armed Forces Europe'),
     133    ('AP', 'Armed Forces Pacific'),
     134)
     135
     136# Non-US locations serviced by USPS (under Compact of Free
     137# Association).
     138COFA_STATES = (
     139    ('FM', 'Federated States of Micronesia'),
     140    ('MH', 'Marshall Islands'),
     141    ('PW', 'Palau'),
     142)
     143
     144# Obsolete abbreviations (no longer US territories/USPS service, or
     145# code changed).
     146OBSOLETE_STATES = (
     147    ('CM', 'Commonwealth of the Northern Mariana Islands'), # Is now 'MP'
     148    ('CZ', 'Panama Canal Zone'),                            # Reverted to Panama 1979
     149    ('PI', 'Philippine Islands'),                           # Philippine independence 1946
     150    ('TT', 'Trust Territory of the Pacific Islands'),       # Became the independent COFA states + Northern Mariana Islands 1979-1994
     151)
     152
     153
     154# All US states and territories plus DC and military mail.
     155STATE_CHOICES = US_STATES + US_TERRITORIES + ARMED_FORCES_STATES
     156
     157# All US Postal Service locations.
     158USPS_CHOICES = US_STATES + US_TERRITORIES + ARMED_FORCES_STATES + COFA_STATES
     159
    68160STATES_NORMALIZED = {
    69161    'ak': 'AK',
    70162    'al': 'AL',
  • django/contrib/localflavor/us/models.py

     
    22from django.utils.translation import ugettext_lazy as _
    33from django.db.models.fields import CharField
    44from django.contrib.localflavor.us.us_states import STATE_CHOICES
     5from django.contrib.localflavor.us.us_states import USPS_CHOICES
    56
    67class USStateField(CharField):
    78
     
    1213        kwargs['max_length'] = 2
    1314        super(USStateField, self).__init__(*args, **kwargs)
    1415
     16class USPostalCodeField(CharField):
     17
     18    description = _("U.S. postal code (two uppercase letters)")
     19
     20    def __init__(self, *args, **kwargs):
     21        kwargs['choices'] = USPS_CHOICES
     22        kwargs['max_length'] = 2
     23        super(USPostalCodeField, self).__init__(*args, **kwargs)
     24
    1525class PhoneNumberField(CharField):
    1626
    1727    description = _("Phone number")
  • django/contrib/localflavor/us/forms.py

     
    111111    def __init__(self, attrs=None):
    112112        from us_states import STATE_CHOICES
    113113        super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
     114
     115class USPSSelect(Select):
     116    """
     117    A Select widget that uses a list of US Postal Service codes as its
     118    choices.
     119    """
     120    def __init__(self, attrs=None):
     121        from us_states import USPS_CHOICES
     122        super(USStateSelect, self).__init__(attrs, choices=USPS_CHOICES)
  • tests/regressiontests/localflavor/us/tests.py

     
    33
    44class USLocalflavorTests(TestCase):
    55    def setUp(self):
    6         self.form = USPlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
     6        self.form = USPlaceForm({'state':'GA', 'state_req':'NC', 'postal_code': 'GA', 'name':'impossible'})
    77
    88    def test_get_display_methods(self):
    99        """Test that the get_*_display() methods are added to the model instances."""
     
    2424<option value="">---------</option>
    2525<option value="AL">Alabama</option>
    2626<option value="AK">Alaska</option>
     27<option value="AZ">Arizona</option>
     28<option value="AR">Arkansas</option>
     29<option value="CA">California</option>
     30<option value="CO">Colorado</option>
     31<option value="CT">Connecticut</option>
     32<option value="DE">Delaware</option>
     33<option value="DC">District of Columbia</option>
     34<option value="FL">Florida</option>
     35<option value="GA" selected="selected">Georgia</option>
     36<option value="HI">Hawaii</option>
     37<option value="ID">Idaho</option>
     38<option value="IL">Illinois</option>
     39<option value="IN">Indiana</option>
     40<option value="IA">Iowa</option>
     41<option value="KS">Kansas</option>
     42<option value="KY">Kentucky</option>
     43<option value="LA">Louisiana</option>
     44<option value="ME">Maine</option>
     45<option value="MD">Maryland</option>
     46<option value="MA">Massachusetts</option>
     47<option value="MI">Michigan</option>
     48<option value="MN">Minnesota</option>
     49<option value="MS">Mississippi</option>
     50<option value="MO">Missouri</option>
     51<option value="MT">Montana</option>
     52<option value="NE">Nebraska</option>
     53<option value="NV">Nevada</option>
     54<option value="NH">New Hampshire</option>
     55<option value="NJ">New Jersey</option>
     56<option value="NM">New Mexico</option>
     57<option value="NY">New York</option>
     58<option value="NC">North Carolina</option>
     59<option value="ND">North Dakota</option>
     60<option value="OH">Ohio</option>
     61<option value="OK">Oklahoma</option>
     62<option value="OR">Oregon</option>
     63<option value="PA">Pennsylvania</option>
     64<option value="RI">Rhode Island</option>
     65<option value="SC">South Carolina</option>
     66<option value="SD">South Dakota</option>
     67<option value="TN">Tennessee</option>
     68<option value="TX">Texas</option>
     69<option value="UT">Utah</option>
     70<option value="VT">Vermont</option>
     71<option value="VA">Virginia</option>
     72<option value="WA">Washington</option>
     73<option value="WV">West Virginia</option>
     74<option value="WI">Wisconsin</option>
     75<option value="WY">Wyoming</option>
    2776<option value="AS">American Samoa</option>
     77<option value="GU">Guam</option>
     78<option value="MP">Northern Mariana Islands</option>
     79<option value="PR">Puerto Rico</option>
     80<option value="VI">Virgin Islands</option>
     81<option value="AA">Armed Forces Americas</option>
     82<option value="AE">Armed Forces Europe</option>
     83<option value="AP">Armed Forces Pacific</option>
     84</select>"""
     85        self.assertEqual(str(self.form['state']), state_select_html)
     86
     87    def test_full_postal_code_list(self):
     88        """Test that the full USPS code field is really the full list."""
     89        usps_select_html = """\
     90<select name="postal_code" id="id_postal_code">
     91<option value="">---------</option>
     92<option value="AL">Alabama</option>
     93<option value="AK">Alaska</option>
    2894<option value="AZ">Arizona</option>
    2995<option value="AR">Arkansas</option>
    3096<option value="CA">California</option>
     
    34100<option value="DC">District of Columbia</option>
    35101<option value="FL">Florida</option>
    36102<option value="GA" selected="selected">Georgia</option>
    37 <option value="GU">Guam</option>
    38103<option value="HI">Hawaii</option>
    39104<option value="ID">Idaho</option>
    40105<option value="IL">Illinois</option>
     
    59124<option value="NY">New York</option>
    60125<option value="NC">North Carolina</option>
    61126<option value="ND">North Dakota</option>
    62 <option value="MP">Northern Mariana Islands</option>
    63127<option value="OH">Ohio</option>
    64128<option value="OK">Oklahoma</option>
    65129<option value="OR">Oregon</option>
    66130<option value="PA">Pennsylvania</option>
    67 <option value="PR">Puerto Rico</option>
    68131<option value="RI">Rhode Island</option>
    69132<option value="SC">South Carolina</option>
    70133<option value="SD">South Dakota</option>
     
    72135<option value="TX">Texas</option>
    73136<option value="UT">Utah</option>
    74137<option value="VT">Vermont</option>
    75 <option value="VI">Virgin Islands</option>
    76138<option value="VA">Virginia</option>
    77139<option value="WA">Washington</option>
    78140<option value="WV">West Virginia</option>
    79141<option value="WI">Wisconsin</option>
    80142<option value="WY">Wyoming</option>
     143<option value="AS">American Samoa</option>
     144<option value="GU">Guam</option>
     145<option value="MP">Northern Mariana Islands</option>
     146<option value="PR">Puerto Rico</option>
     147<option value="VI">Virgin Islands</option>
     148<option value="AA">Armed Forces Americas</option>
     149<option value="AE">Armed Forces Europe</option>
     150<option value="AP">Armed Forces Pacific</option>
     151<option value="FM">Federated States of Micronesia</option>
     152<option value="MH">Marshall Islands</option>
     153<option value="PW">Palau</option>
    81154</select>"""
    82         self.assertEqual(str(self.form['state']), state_select_html)
     155        self.assertEqual(str(self.form['postal_code']), usps_select_html)
  • tests/regressiontests/localflavor/us/models.py

     
    11from django.db import models
    22from django.contrib.localflavor.us.models import USStateField
     3from django.contrib.localflavor.us.models import USPostalCodeField
    34
    45# When creating models you need to remember to add a app_label as
    56# 'localflavor', so your model can be found
     
    89    state = USStateField(blank=True)
    910    state_req = USStateField()
    1011    state_default = USStateField(default="CA", blank=True)
     12    postal_code = USPostalCodeField(blank=True)
    1113    name = models.CharField(max_length=20)
    1214    class Meta:
    1315        app_label = 'localflavor'
  • docs/ref/contrib/localflavor.txt

     
    937937    A form ``Select`` widget that uses a list of U.S. states/territories as its
    938938    choices.
    939939
     940.. class:: us.forms.USPSSelect
     941
     942    A form ``Select`` widget that uses a list of U.S Postal Service
     943    state, territory and country abbreviations as its choices.
     944
    940945.. class:: us.models.PhoneNumberField
    941946
    942947    A :class:`CharField` that checks that the value is a valid U.S.A.-style phone
     
    947952    A model field that forms represent as a ``forms.USStateField`` field and
    948953    stores the two-letter U.S. state abbreviation in the database.
    949954
     955.. class:: us.models.USPostalCodeField
     956
     957    A model field that forms represent as a ``forms.USPSSelect`` field
     958    and stores the two-letter U.S Postal Service abbreviation in the
     959    database.
     960
     961Additionally, a variety of choice tuples are provided in
     962``django.contrib.localflavor.us.us_states``, allowing customized model
     963and form fields, and form presentations, for subsets of U.S states,
     964territories and U.S Postal Service abbreviations:
     965
     966.. data:: us.us_states.CONTIGUOUS_STATES
     967
     968    A tuple of choices of the postal abbreviations for the
     969    contiguous or "lower 48" states (i.e., all except Alaska and
     970    Hawaii), plus the District of Columbia.
     971
     972.. data:: us.us_states.US_STATES
     973
     974    A tuple of choices of the postal abbreviations for all
     975    50 U.S. states, plus the District of Columbia.
     976
     977.. data:: us.us_states.US_TERRITORIES
     978
     979    A tuple of choices of the postal abbreviations for U.S
     980    territories: American Samoa, Guam, the Northern Mariana Islands,
     981    Puerto Rico and the U.S. Virgin Islands.
     982
     983.. data:: us.us_states.ARMED_FORCES_STATES
     984
     985    A tuple of choices of the postal abbreviations of the three U.S
     986    military postal "states": Armed Forces Americas, Armed Forces
     987    Europe and Armed Forces Pacific.
     988
     989.. data:: us.us_states.COFA_STATES
     990
     991    A tuple of choices of the postal abbreviations of the three
     992    independent nations which, under the Compact of Free Association,
     993    are served by the U.S. Postal Service: the Federated States of
     994    Micronesia, the Marshall Islands and Palau.
     995
     996.. data:: us.us_states.OBSOLETE_STATES
     997
     998    A tuple of choices of obsolete U.S Postal Service state
     999    abbreviations: the former abbreviation for the Northern Mariana
     1000    Islands, plus the Panama Canal Zone, the Philippines and the
     1001    former Pacific trust territories.
     1002
     1003.. data:: us.us_states.STATE_CHOICES
     1004
     1005    A tuple of choices of all postal abbreviations corresponding to U.S states or
     1006    territories, and the District of Columbia..
     1007
     1008.. data:: us.us_states.USPS_CHOICES
     1009
     1010    A tuple of choices of all postal abbreviations recognized by the
     1011    U.S Postal Service (including all states and territories, the
     1012    District of Columbia, armed forces "states" and independent
     1013    nations serviced by USPS).
     1014
    9501015Uruguay (``uy``)
    9511016================
    9521017
Back to Top