Ticket #14937: us_postal_codes.diff
File us_postal_codes.diff, 15.3 KB (added by , 14 years ago) |
---|
-
django/contrib/localflavor/us/us_states.py
1 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. 2 A mapping of state misspellings/abbreviations to normalized 3 abbreviations, and alphabetical lists of US states, territories, 4 military mail regions and non-US states to which the US provides 5 postal service. 4 6 5 7 This exists in this standalone file so that it's only imported into memory 6 8 when explicitly needed. 7 9 """ 8 10 9 STATE_CHOICES = ( 11 # The 48 contiguous states, plus the District of Columbia. 12 CONTIGUOUS_STATES = ( 10 13 ('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. 65 US_STATES = ( 66 ('AL', 'Alabama'), 11 67 ('AK', 'Alaska'), 12 ('AS', 'American Samoa'),13 68 ('AZ', 'Arizona'), 14 69 ('AR', 'Arkansas'), 15 70 ('CA', 'California'), … … 19 74 ('DC', 'District of Columbia'), 20 75 ('FL', 'Florida'), 21 76 ('GA', 'Georgia'), 22 ('GU', 'Guam'),23 77 ('HI', 'Hawaii'), 24 78 ('ID', 'Idaho'), 25 79 ('IL', 'Illinois'), … … 44 98 ('NY', 'New York'), 45 99 ('NC', 'North Carolina'), 46 100 ('ND', 'North Dakota'), 47 ('MP', 'Northern Mariana Islands'),48 101 ('OH', 'Ohio'), 49 102 ('OK', 'Oklahoma'), 50 103 ('OR', 'Oregon'), 51 104 ('PA', 'Pennsylvania'), 52 ('PR', 'Puerto Rico'),53 105 ('RI', 'Rhode Island'), 54 106 ('SC', 'South Carolina'), 55 107 ('SD', 'South Dakota'), … … 57 109 ('TX', 'Texas'), 58 110 ('UT', 'Utah'), 59 111 ('VT', 'Vermont'), 60 ('VI', 'Virgin Islands'),61 112 ('VA', 'Virginia'), 62 113 ('WA', 'Washington'), 63 114 ('WV', 'West Virginia'), … … 65 116 ('WY', 'Wyoming'), 66 117 ) 67 118 119 # Non-state territories. 120 US_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. 130 ARMED_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). 138 COFA_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). 146 OBSOLETE_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. 155 STATE_CHOICES = US_STATES + US_TERRITORIES + ARMED_FORCES_STATES 156 157 # All US Postal Service locations. 158 USPS_CHOICES = US_STATES + US_TERRITORIES + ARMED_FORCES_STATES + COFA_STATES 159 68 160 STATES_NORMALIZED = { 69 161 'ak': 'AK', 70 162 'al': 'AL', -
django/contrib/localflavor/us/models.py
2 2 from django.utils.translation import ugettext_lazy as _ 3 3 from django.db.models.fields import CharField 4 4 from django.contrib.localflavor.us.us_states import STATE_CHOICES 5 from django.contrib.localflavor.us.us_states import USPS_CHOICES 5 6 6 7 class USStateField(CharField): 7 8 … … 12 13 kwargs['max_length'] = 2 13 14 super(USStateField, self).__init__(*args, **kwargs) 14 15 16 class 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 15 25 class PhoneNumberField(CharField): 16 26 17 27 description = _("Phone number") -
django/contrib/localflavor/us/forms.py
111 111 def __init__(self, attrs=None): 112 112 from us_states import STATE_CHOICES 113 113 super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 114 115 class 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
3 3 4 4 class USLocalflavorTests(TestCase): 5 5 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'}) 7 7 8 8 def test_get_display_methods(self): 9 9 """Test that the get_*_display() methods are added to the model instances.""" … … 24 24 <option value="">---------</option> 25 25 <option value="AL">Alabama</option> 26 26 <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> 27 76 <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> 28 94 <option value="AZ">Arizona</option> 29 95 <option value="AR">Arkansas</option> 30 96 <option value="CA">California</option> … … 34 100 <option value="DC">District of Columbia</option> 35 101 <option value="FL">Florida</option> 36 102 <option value="GA" selected="selected">Georgia</option> 37 <option value="GU">Guam</option>38 103 <option value="HI">Hawaii</option> 39 104 <option value="ID">Idaho</option> 40 105 <option value="IL">Illinois</option> … … 59 124 <option value="NY">New York</option> 60 125 <option value="NC">North Carolina</option> 61 126 <option value="ND">North Dakota</option> 62 <option value="MP">Northern Mariana Islands</option>63 127 <option value="OH">Ohio</option> 64 128 <option value="OK">Oklahoma</option> 65 129 <option value="OR">Oregon</option> 66 130 <option value="PA">Pennsylvania</option> 67 <option value="PR">Puerto Rico</option>68 131 <option value="RI">Rhode Island</option> 69 132 <option value="SC">South Carolina</option> 70 133 <option value="SD">South Dakota</option> … … 72 135 <option value="TX">Texas</option> 73 136 <option value="UT">Utah</option> 74 137 <option value="VT">Vermont</option> 75 <option value="VI">Virgin Islands</option>76 138 <option value="VA">Virginia</option> 77 139 <option value="WA">Washington</option> 78 140 <option value="WV">West Virginia</option> 79 141 <option value="WI">Wisconsin</option> 80 142 <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> 81 154 </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
1 1 from django.db import models 2 2 from django.contrib.localflavor.us.models import USStateField 3 from django.contrib.localflavor.us.models import USPostalCodeField 3 4 4 5 # When creating models you need to remember to add a app_label as 5 6 # 'localflavor', so your model can be found … … 8 9 state = USStateField(blank=True) 9 10 state_req = USStateField() 10 11 state_default = USStateField(default="CA", blank=True) 12 postal_code = USPostalCodeField(blank=True) 11 13 name = models.CharField(max_length=20) 12 14 class Meta: 13 15 app_label = 'localflavor' -
docs/ref/contrib/localflavor.txt
937 937 A form ``Select`` widget that uses a list of U.S. states/territories as its 938 938 choices. 939 939 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 940 945 .. class:: us.models.PhoneNumberField 941 946 942 947 A :class:`CharField` that checks that the value is a valid U.S.A.-style phone … … 947 952 A model field that forms represent as a ``forms.USStateField`` field and 948 953 stores the two-letter U.S. state abbreviation in the database. 949 954 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 961 Additionally, a variety of choice tuples are provided in 962 ``django.contrib.localflavor.us.us_states``, allowing customized model 963 and form fields, and form presentations, for subsets of U.S states, 964 territories 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 950 1015 Uruguay (``uy``) 951 1016 ================ 952 1017