diff --git a/docs/index.rst b/docs/index.rst index 001ff75ec..8c822d508 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -73,6 +73,7 @@ validate Finnish social security numbers. * :doc:`localflavor/sk` * :doc:`localflavor/tr` * :doc:`localflavor/us` + * :doc:`localflavor/us_ny` * :doc:`localflavor/uy` * :doc:`localflavor/za` diff --git a/docs/localflavor/us_ny.rst b/docs/localflavor/us_ny.rst new file mode 100644 index 000000000..e0a09c303 --- /dev/null +++ b/docs/localflavor/us_ny.rst @@ -0,0 +1,21 @@ +New York State (``us.ny``) +================================= + +Forms +----- + +.. automodule:: localflavor.us.ny.forms + :members: + +Models +------ + +.. automodule:: localflavor.us.ny.models + :members: + +Data +---- + +.. autodata:: localflavor.us.ny.nys_detail.NYS_COUNTIES + +.. autodata:: localflavor.us.ny.nys_detail.NYS_COUNTY_CHOICES diff --git a/localflavor/us/ny/__init__.py b/localflavor/us/ny/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/localflavor/us/ny/forms.py b/localflavor/us/ny/forms.py new file mode 100644 index 000000000..158939aca --- /dev/null +++ b/localflavor/us/ny/forms.py @@ -0,0 +1,16 @@ +""" +New York State Form helpers +""" + +from __future__ import absolute_import, unicode_literals + +from django.forms.fields import Select + + +class NYSCountySelect(Select): + """ + A Select widget that uses a list of U.S. states/territories as its choices. + """ + def __init__(self, attrs=None): + from .nys_detail import NYS_COUNTY_CHOICES + super(NYSCountySelect, self).__init__(attrs, choices=NYS_COUNTY_CHOICES) diff --git a/localflavor/us/ny/models.py b/localflavor/us/ny/models.py new file mode 100644 index 000000000..73904ef66 --- /dev/null +++ b/localflavor/us/ny/models.py @@ -0,0 +1,17 @@ +from django.utils.translation import ugettext_lazy as _ +from django.db.models.fields import CharField + +from .nys_detail import NYS_COUNTY_CHOICES + + +class NYSCountyField(CharField): + """ + A model field that forms represent as a ``forms.NYSCountyField`` field and + stores the three-digit county code. + """ + description = _("New York State County Three-Digit Codes") + + def __init__(self, *args, **kwargs): + kwargs['choices'] = NYS_COUNTY_CHOICES + kwargs['max_length'] = 3 + super(NYSCountyField, self).__init__(*args, **kwargs) diff --git a/localflavor/us/ny/nys_detail.py b/localflavor/us/ny/nys_detail.py new file mode 100644 index 000000000..6de6aacff --- /dev/null +++ b/localflavor/us/ny/nys_detail.py @@ -0,0 +1,72 @@ +""" +File for mapping of New York State counties, and cities. +""" + +# County codes for each county +NYS_COUNTIES = ( + ('001', 'Albany'), + ('003', 'Allegany'), + ('005', 'Bronx'), + ('007', 'Broome'), + ('009', 'Cattaraugus'), + ('011', 'Cayuga'), + ('013', 'Chautauqua'), + ('015', 'Chemung'), + ('017', 'Chenango'), + ('019', 'Clinton'), + ('021', 'Columbia'), + ('023', 'Cortland'), + ('025', 'Delaware'), + ('027', 'Dutchess'), + ('029', 'Erie'), + ('031', 'Essex'), + ('033', 'Franklin'), + ('035', 'Fulton'), + ('037', 'Genesee'), + ('039', 'Greene'), + ('041', 'Hamilton'), + ('043', 'Herkimer'), + ('045', 'Jefferson'), + ('047', 'Kings'), + ('049', 'Lewis'), + ('051', 'Livingston'), + ('053', 'Madison'), + ('055', 'Monroe'), + ('057', 'Montgomery'), + ('059', 'Nassau'), + ('061', 'New York'), + ('063', 'Niagara'), + ('065', 'Oneida'), + ('067', 'Onondaga'), + ('069', 'Ontario'), + ('071', 'Orange'), + ('073', 'Orleans'), + ('075', 'Oswego'), + ('077', 'Otsego'), + ('079', 'Putnam'), + ('081', 'Queens'), + ('083', 'Rensselaer'), + ('085', 'Richmond'), + ('087', 'Rockland'), + ('091', 'Saratoga'), + ('093', 'Schenectady'), + ('095', 'Schoharie'), + ('097', 'Schuyler'), + ('099', 'Seneca'), + ('089', 'St. Lawrence'), + ('101', 'Steuben'), + ('103', 'Suffolk'), + ('105', 'Sullivan'), + ('107', 'Tioga'), + ('109', 'Tompkins'), + ('111', 'Ulster'), + ('113', 'Warren'), + ('115', 'Washington'), + ('117', 'Wayne'), + ('119', 'Westchester'), + ('121', 'Wyoming'), + ('123', 'Yates'), +) + +#: All NY State Counties +NYS_COUNTY_CHOICES = tuple(sorted(NYS_COUNTIES, key=lambda obj: obj[1])) diff --git a/tests/test_us/test_ny/tests.py b/tests/test_us/test_ny/tests.py new file mode 100644 index 000000000..058b03412 --- /dev/null +++ b/tests/test_us/test_ny/tests.py @@ -0,0 +1,72 @@ +from __future__ import unicode_literals +from django.test import SimpleTestCase +from localflavor.us.ny.forms import NYSCountySelect + +class US_NYSLocalFlavorTests(SimpleTestCase): + def test_NYSCountySelect(self): + f = NYSCountySelect() + out = '''''' + self.assertHTMLEqual(f.render('counties', '061'), out)