Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implented newforms USStateField in django.contrib.localflavor.usa. Refs

#2455

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4525 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1ce1442d95730e7b4e0d54f971aaeb6cf744f04c 1 parent 27b2861
Adrian Holovaty authored February 15, 2007
25  django/contrib/localflavor/usa/forms.py
@@ -2,7 +2,8 @@
2 2
 USA-specific Form helpers
3 3
 """
4 4
 
5  
-from django.newforms.fields import RegexField
  5
+from django.newforms import ValidationError
  6
+from django.newforms.fields import Field, RegexField, EMPTY_VALUES
6 7
 from django.utils.translation import gettext
7 8
 
8 9
 class USZipCodeField(RegexField):
@@ -11,3 +12,25 @@ def __init__(self, *args, **kwargs):
11 12
             max_length=None, min_length=None,
12 13
             error_message=gettext(u'Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
13 14
             *args, **kwargs)
  15
+
  16
+class USStateField(Field):
  17
+    """
  18
+    A form field that validates its input is a U.S. state name or abbreviation.
  19
+    It normalizes the input to the standard two-leter postal service
  20
+    abbreviation for the given state.
  21
+    """
  22
+    def clean(self, value):
  23
+        from us_states import STATES_NORMALIZED # relative import
  24
+        super(USStateField, self).clean(value)
  25
+        if value in EMPTY_VALUES:
  26
+            return u''
  27
+        try:
  28
+            value = value.strip().lower()
  29
+        except AttributeError:
  30
+            pass
  31
+        else:
  32
+            try:
  33
+                return STATES_NORMALIZED[value.strip().lower()].decode('ascii')
  34
+            except KeyError:
  35
+                pass
  36
+        raise ValidationError(u'Enter a U.S. state or territory.')
176  django/contrib/localflavor/usa/us_states.py
... ...
@@ -0,0 +1,176 @@
  1
+"""
  2
+A mapping of state misspellings/abbreviations to normalized abbreviations.
  3
+
  4
+This exists in this standalone file so that it's only imported into memory
  5
+when explicitly needed.
  6
+"""
  7
+
  8
+STATES_NORMALIZED = {
  9
+    'ak': 'AK',
  10
+    'al': 'AL',
  11
+    'ala': 'AL',
  12
+    'alabama': 'AL',
  13
+    'alaska': 'AK',
  14
+    'american samao': 'AS',
  15
+    'american samoa': 'AS',
  16
+    'ar': 'AR',
  17
+    'ariz': 'AZ',
  18
+    'arizona': 'AZ',
  19
+    'ark': 'AR',
  20
+    'arkansas': 'AR',
  21
+    'as': 'AS',
  22
+    'az': 'AZ',
  23
+    'ca': 'CA',
  24
+    'calf': 'CA',
  25
+    'calif': 'CA',
  26
+    'california': 'CA',
  27
+    'co': 'CO',
  28
+    'colo': 'CO',
  29
+    'colorado': 'CO',
  30
+    'conn': 'CT',
  31
+    'connecticut': 'CT',
  32
+    'ct': 'CT',
  33
+    'dc': 'DC',
  34
+    'de': 'DE',
  35
+    'del': 'DE',
  36
+    'delaware': 'DE',
  37
+    'district of columbia': 'DC',
  38
+    'federated states of micronesia': 'FM',
  39
+    'fl': 'FL',
  40
+    'fla': 'FL',
  41
+    'florida': 'FL',
  42
+    'fm': 'FM',
  43
+    'ga': 'GA',
  44
+    'georgia': 'GA',
  45
+    'gu': 'GU',
  46
+    'guam': 'GU',
  47
+    'hawaii': 'HI',
  48
+    'hi': 'HI',
  49
+    'ia': 'IA',
  50
+    'id': 'ID',
  51
+    'idaho': 'ID',
  52
+    'il': 'IL',
  53
+    'ill': 'IL',
  54
+    'illinois': 'IL',
  55
+    'in': 'IN',
  56
+    'ind': 'IN',
  57
+    'indiana': 'IN',
  58
+    'iowa': 'IA',
  59
+    'kan': 'KS',
  60
+    'kans': 'KS',
  61
+    'kansas': 'KS',
  62
+    'kentucky': 'KY',
  63
+    'ks': 'KS',
  64
+    'ky': 'KY',
  65
+    'la': 'LA',
  66
+    'louisiana': 'LA',
  67
+    'ma': 'MA',
  68
+    'maine': 'ME',
  69
+    'marianas islands': 'MP',
  70
+    'marianas islands of the pacific': 'MP',
  71
+    'marinas islands of the pacific': 'MP',
  72
+    'maryland': 'MD',
  73
+    'mass': 'MA',
  74
+    'massachusetts': 'MA',
  75
+    'massachussetts': 'MA',
  76
+    'md': 'MD',
  77
+    'me': 'ME',
  78
+    'mi': 'MI',
  79
+    'mich': 'MI',
  80
+    'michigan': 'MI',
  81
+    'micronesia': 'FM',
  82
+    'minn': 'MN',
  83
+    'minnesota': 'MN',
  84
+    'miss': 'MS',
  85
+    'mississippi': 'MS',
  86
+    'missouri': 'MO',
  87
+    'mn': 'MN',
  88
+    'mo': 'MO',
  89
+    'mont': 'MT',
  90
+    'montana': 'MT',
  91
+    'mp': 'MP',
  92
+    'ms': 'MS',
  93
+    'mt': 'MT',
  94
+    'n d': 'ND',
  95
+    'n dak': 'ND',
  96
+    'n h': 'NH',
  97
+    'n j': 'NJ',
  98
+    'n m': 'NM',
  99
+    'n mex': 'NM',
  100
+    'nc': 'NC',
  101
+    'nd': 'ND',
  102
+    'ne': 'NE',
  103
+    'neb': 'NE',
  104
+    'nebr': 'NE',
  105
+    'nebraska': 'NE',
  106
+    'nev': 'NV',
  107
+    'nevada': 'NV',
  108
+    'new hampshire': 'NH',
  109
+    'new jersey': 'NJ',
  110
+    'new mexico': 'NM',
  111
+    'new york': 'NY',
  112
+    'nh': 'NH',
  113
+    'nj': 'NJ',
  114
+    'nm': 'NM',
  115
+    'nmex': 'NM',
  116
+    'north carolina': 'NC',
  117
+    'north dakota': 'ND',
  118
+    'northern mariana islands': 'MP',
  119
+    'nv': 'NV',
  120
+    'ny': 'NY',
  121
+    'oh': 'OH',
  122
+    'ohio': 'OH',
  123
+    'ok': 'OK',
  124
+    'okla': 'OK',
  125
+    'oklahoma': 'OK',
  126
+    'or': 'OR',
  127
+    'ore': 'OR',
  128
+    'oreg': 'OR',
  129
+    'oregon': 'OR',
  130
+    'pa': 'PA',
  131
+    'penn': 'PA',
  132
+    'pennsylvania': 'PA',
  133
+    'pr': 'PR',
  134
+    'puerto rico': 'PR',
  135
+    'rhode island': 'RI',
  136
+    'ri': 'RI',
  137
+    's dak': 'SD',
  138
+    'sc': 'SC',
  139
+    'sd': 'SD',
  140
+    'sdak': 'SD',
  141
+    'south carolina': 'SC',
  142
+    'south dakota': 'SD',
  143
+    'tenn': 'TN',
  144
+    'tennessee': 'TN',
  145
+    'territory of hawaii': 'HI',
  146
+    'tex': 'TX',
  147
+    'texas': 'TX',
  148
+    'tn': 'TN',
  149
+    'tx': 'TX',
  150
+    'us virgin islands': 'VI',
  151
+    'usvi': 'VI',
  152
+    'ut': 'UT',
  153
+    'utah': 'UT',
  154
+    'va': 'VA',
  155
+    'vermont': 'VT',
  156
+    'vi': 'VI',
  157
+    'viginia': 'VA',
  158
+    'virgin islands': 'VI',
  159
+    'virgina': 'VA',
  160
+    'virginia': 'VA',
  161
+    'vt': 'VT',
  162
+    'w va': 'WV',
  163
+    'wa': 'WA',
  164
+    'wash': 'WA',
  165
+    'washington': 'WA',
  166
+    'west virginia': 'WV',
  167
+    'wi': 'WI',
  168
+    'wis': 'WI',
  169
+    'wisc': 'WI',
  170
+    'wisconsin': 'WI',
  171
+    'wv': 'WV',
  172
+    'wva': 'WV',
  173
+    'wy': 'WY',
  174
+    'wyo': 'WY',
  175
+    'wyoming': 'WY',
  176
+}
45  tests/regressiontests/forms/tests.py
@@ -3282,6 +3282,51 @@
3282 3282
 >>> f.clean('')
3283 3283
 u''
3284 3284
 
  3285
+# USStateField ################################################################
  3286
+
  3287
+USStateField validates that the data is either an abbreviation or name of a
  3288
+U.S. state.
  3289
+>>> from django.contrib.localflavor.usa.forms import USStateField
  3290
+>>> f = USStateField()
  3291
+>>> f.clean('il')
  3292
+u'IL'
  3293
+>>> f.clean('IL')
  3294
+u'IL'
  3295
+>>> f.clean('illinois')
  3296
+u'IL'
  3297
+>>> f.clean('  illinois ')
  3298
+u'IL'
  3299
+>>> f.clean(60606)
  3300
+Traceback (most recent call last):
  3301
+...
  3302
+ValidationError: [u'Enter a U.S. state or territory.']
  3303
+>>> f.clean(None)
  3304
+Traceback (most recent call last):
  3305
+...
  3306
+ValidationError: [u'This field is required.']
  3307
+>>> f.clean('')
  3308
+Traceback (most recent call last):
  3309
+...
  3310
+ValidationError: [u'This field is required.']
  3311
+
  3312
+>>> f = USStateField(required=False)
  3313
+>>> f.clean('il')
  3314
+u'IL'
  3315
+>>> f.clean('IL')
  3316
+u'IL'
  3317
+>>> f.clean('illinois')
  3318
+u'IL'
  3319
+>>> f.clean('  illinois ')
  3320
+u'IL'
  3321
+>>> f.clean(60606)
  3322
+Traceback (most recent call last):
  3323
+...
  3324
+ValidationError: [u'Enter a U.S. state or territory.']
  3325
+>>> f.clean(None)
  3326
+u''
  3327
+>>> f.clean('')
  3328
+u''
  3329
+
3285 3330
 #################################
3286 3331
 # Tests of underlying functions #
3287 3332
 #################################

0 notes on commit 1ce1442

Please sign in to comment.
Something went wrong with that request. Please try again.