From 907241e299a4ed9077bbeb6fbfdccce738a8e2a8 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 20 Feb 2007 00:30:22 +0000 Subject: [PATCH] Fixed #2455 -- Added newforms USStateSelect widget in django.contrib.localflavor.usa git-svn-id: http://code.djangoproject.com/svn/django/trunk@4545 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/usa/forms.py | 10 ++- django/contrib/localflavor/usa/us_states.py | 64 +++++++++++++++++- tests/regressiontests/forms/tests.py | 72 ++++++++++++++++++++- 3 files changed, 142 insertions(+), 4 deletions(-) diff --git a/django/contrib/localflavor/usa/forms.py b/django/contrib/localflavor/usa/forms.py index e56087545a7..4d2cb5e90a3 100644 --- a/django/contrib/localflavor/usa/forms.py +++ b/django/contrib/localflavor/usa/forms.py @@ -3,7 +3,7 @@ USA-specific Form helpers """ from django.newforms import ValidationError -from django.newforms.fields import Field, RegexField, EMPTY_VALUES +from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES from django.utils.translation import gettext class USZipCodeField(RegexField): @@ -34,3 +34,11 @@ class USStateField(Field): except KeyError: pass raise ValidationError(u'Enter a U.S. state or territory.') + +class USStateSelect(Select): + """ + A Select widget that uses a list of U.S. states/territories as its choices. + """ + def __init__(self, attrs=None): + from us_states import STATE_CHOICES # relative import + super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/django/contrib/localflavor/usa/us_states.py b/django/contrib/localflavor/usa/us_states.py index 46bd0019e52..53337636455 100644 --- a/django/contrib/localflavor/usa/us_states.py +++ b/django/contrib/localflavor/usa/us_states.py @@ -1,10 +1,72 @@ """ -A mapping of state misspellings/abbreviations to normalized abbreviations. +A mapping of state misspellings/abbreviations to normalized abbreviations, and +an alphabetical list of states for use as `choices` in a formfield. This exists in this standalone file so that it's only imported into memory when explicitly needed. """ +STATE_CHOICES = ( + ('AL', 'Alabama'), + ('AK', 'Alaska'), + ('AS', 'American Samoa'), + ('AZ', 'Arizona'), + ('AR', 'Arkansas'), + ('CA', 'California'), + ('CO', 'Colorado'), + ('CT', 'Connecticut'), + ('DE', 'Deleware'), + ('DC', 'District of Columbia'), + ('FM', 'Federated States of Micronesia'), + ('FL', 'Florida'), + ('GA', 'Georgia'), + ('GU', 'Guam'), + ('HI', 'Hawaii'), + ('ID', 'Idaho'), + ('IL', 'Illinois'), + ('IN', 'Indiana'), + ('IA', 'Iowa'), + ('KS', 'Kansas'), + ('KY', 'Kentucky'), + ('LA', 'Louisiana'), + ('ME', 'Maine'), + ('MH', 'Marshall Islands'), + ('MD', 'Maryland'), + ('MI', 'Michigan'), + ('MN', 'Minnesota'), + ('MS', 'Mississippi'), + ('MO', 'Missouri'), + ('MT', 'Montana'), + ('NE', 'Nebraska'), + ('NV', 'Nevada'), + ('NH', 'New Hampshire'), + ('NJ', 'New Jersey'), + ('NM', 'New Mexico'), + ('NY', 'New York'), + ('NC', 'North Carolina'), + ('ND', 'North Dakota'), + ('MP', 'Northern Mariana Islands'), + ('OH', 'Ohio'), + ('OK', 'Oklahoma'), + ('OR', 'Oregon'), + ('PW', 'Palau'), + ('PA', 'Pennsylvania'), + ('PR', 'Puerto Rico'), + ('RI', 'Rhode Island'), + ('SC', 'South Carolina'), + ('SD', 'South Dakota'), + ('TN', 'Tennessee'), + ('TX', 'Texas'), + ('UT', 'Utah'), + ('VT', 'Vermont'), + ('VI', 'Virgin Islands'), + ('VA', 'Virginia'), + ('WA', 'Washington'), + ('WV', 'West Virginia'), + ('WI', 'Wisconsin'), + ('WY', 'Wyoming'), +) + STATES_NORMALIZED = { 'ak': 'AK', 'al': 'AL', diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 34f1907c5e1..c4502b2202d 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2222,7 +2222,7 @@ Validation errors are HTML-escaped when output as HTML. ... special_name = CharField() ... def clean_special_name(self): ... raise ValidationError("Something's wrong with '%s'" % self.clean_data['special_name']) - + >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) >>> print f Special name: @@ -3349,7 +3349,75 @@ u'' >>> f.clean('') u'' -# UKPostcodeField ############################################################## +# USStateSelect ############################################################### + +USStateSelect is a Select widget that uses a list of U.S. states/territories +as its choices. +>>> from django.contrib.localflavor.usa.forms import USStateSelect +>>> w = USStateSelect() +>>> print w.render('state', 'IL') + + +# UKPostcodeField ############################################################# UKPostcodeField validates that the data is a valid UK postcode. >>> from django.contrib.localflavor.uk.forms import UKPostcodeField