Created
June 3, 2011 17:21
-
-
Save melinath/1006721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.localflavor.us import us_states | |
from django.contrib.localflavor.us.models import USStateField | |
from django.db import models | |
from .utils import US_STATE_CHAR2FIPS, US_STATE_FIPS, US_STATE_FIPS_SHORT | |
class USStateFipsCode(object): | |
"""A US state class which includes FIPS characters and numbers.""" | |
def __init__(self, number): | |
self.number = number | |
self.code = US_STATE_FIPS_SHORT[number] | |
self.name = US_STATE_FIPS[number] | |
def __unicode__(self): | |
return self.name | |
def __repr__(self): | |
return "<%s: %s>" % (self.__class__.__name__, self.code) | |
class InvalidFIPS(Exception): | |
def __init__(self, value): | |
self.value = value | |
def __str__(self): | |
return '%s is an invalid FIPS code' % repr(self.value) | |
class USStateFipsField(USStateField): | |
__metaclass__ = models.SubfieldBase # Ensures to_python is always called. | |
def to_python(self, value): | |
if isinstance(value, USStateFipsCode): | |
return value | |
else: | |
if value in US_STATE_CHAR2FIPS: | |
value = US_STATE_CHAR2FIPS[value] | |
else: | |
try: | |
value = int(value) | |
except TypeError: | |
raise InvalidFIPS(value) | |
return USStateFipsCode(value) | |
def get_prep_value(self, value): | |
return value.number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
US_STATE_FIPS = { | |
1: 'Alabama', | |
2: 'Alaska', | |
4: 'Arizona', | |
5: 'Arkansas', | |
6: 'California', | |
8: 'Colorado', | |
9: 'Connecticut', | |
10: 'Delaware', | |
11: 'District of Columbia', | |
12: 'Florida', | |
13: 'Georgia', | |
15: 'Hawaii', | |
16: 'Idaho', | |
17: 'Illinois', | |
18: 'Indiana', | |
19: 'Iowa', | |
20: 'Kansas', | |
21: 'Kentucky', | |
22: 'Louisiana', | |
23: 'Maine', | |
24: 'Maryland', | |
25: 'Massachusetts', | |
26: 'Michigan', | |
27: 'Minnesota', | |
28: 'Mississippi', | |
29: 'Missouri', | |
30: 'Montana', | |
31: 'Nebraska', | |
32: 'Nevada', | |
33: 'New Hampshire', | |
34: 'New Jersey', | |
35: 'New Mexico', | |
36: 'New York', | |
37: 'North Carolina', | |
38: 'North Dakota', | |
39: 'Ohio', | |
40: 'Oklahoma', | |
41: 'Oregon', | |
42: 'Pennsylvania', | |
44: 'Rhode Island', | |
45: 'South Carolina', | |
46: 'South Dakota', | |
47: 'Tennessee', | |
48: 'Texas', | |
49: 'Utah', | |
50: 'Vermont', | |
51: 'Virginia', | |
53: 'Washington', | |
54: 'West Virginia', | |
55: 'Wisconsin', | |
56: 'Wyoming', | |
66: 'Guam', | |
72: 'Puerto Rico', | |
78: 'Virgin Islands', | |
} | |
US_STATE_FIPS_SHORT = { | |
1: 'AL', | |
2: 'AK', | |
4: 'AZ', | |
5: 'AR', | |
6: 'CA', | |
8: 'CO', | |
9: 'CT', | |
10: 'DE', | |
11: 'DC', | |
12: 'FL', | |
13: 'GA', | |
15: 'HI', | |
16: 'ID', | |
17: 'IL', | |
18: 'IN', | |
19: 'IA', | |
20: 'KS', | |
21: 'KY', | |
22: 'LA', | |
23: 'ME', | |
24: 'MD', | |
25: 'MA', | |
26: 'MI', | |
27: 'MN', | |
28: 'MS', | |
29: 'MO', | |
30: 'MT', | |
31: 'NE', | |
32: 'NV', | |
33: 'NH', | |
34: 'NJ', | |
35: 'NM', | |
36: 'NY', | |
37: 'NC', | |
38: 'ND', | |
39: 'OH', | |
40: 'OK', | |
41: 'OR', | |
42: 'PA', | |
44: 'RI', | |
45: 'SC', | |
46: 'SD', | |
47: 'TN', | |
48: 'TX', | |
49: 'UT', | |
50: 'VT', | |
51: 'VA', | |
53: 'WA', | |
54: 'WV', | |
55: 'WI', | |
56: 'WY', | |
66: 'GU', | |
72: 'PR', | |
78: 'VI', | |
} | |
US_STATE_CHAR2FIPS = dict([v,k] for k, v in US_STATE_FIPS_SHORT.iteritems()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment