Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding US State Level localflavors #137

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
21 changes: 21 additions & 0 deletions docs/localflavor/us_ny.rst
Original file line number Diff line number Diff line change
@@ -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
Empty file added localflavor/us/ny/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions localflavor/us/ny/forms.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions localflavor/us/ny/models.py
Original file line number Diff line number Diff line change
@@ -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)
72 changes: 72 additions & 0 deletions localflavor/us/ny/nys_detail.py
Original file line number Diff line number Diff line change
@@ -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]))
72 changes: 72 additions & 0 deletions tests/test_us/test_ny/tests.py
Original file line number Diff line number Diff line change
@@ -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 = '''<select name="counties">
<option value="001">Albany</option>
<option value="003">Allegany</option>
<option value="005">Bronx</option>
<option value="007">Broome</option>
<option value="009">Cattaraugus</option>
<option value="011">Cayuga</option>
<option value="013">Chautauqua</option>
<option value="015">Chemung</option>
<option value="017">Chenango</option>
<option value="019">Clinton</option>
<option value="021">Columbia</option>
<option value="023">Cortland</option>
<option value="025">Delaware</option>
<option value="027">Dutchess</option>
<option value="029">Erie</option>
<option value="031">Essex</option>
<option value="033">Franklin</option>
<option value="035">Fulton</option>
<option value="037">Genesee</option>
<option value="039">Greene</option>
<option value="041">Hamilton</option>
<option value="043">Herkimer</option>
<option value="045">Jefferson</option>
<option value="047">Kings</option>
<option value="049">Lewis</option>
<option value="051">Livingston</option>
<option value="053">Madison</option>
<option value="055">Monroe</option>
<option value="057">Montgomery</option>
<option value="059">Nassau</option>
<option value="061" selected="selected">New York</option>
<option value="063">Niagara</option>
<option value="065">Oneida</option>
<option value="067">Onondaga</option>
<option value="069">Ontario</option>
<option value="071">Orange</option>
<option value="073">Orleans</option>
<option value="075">Oswego</option>
<option value="077">Otsego</option>
<option value="079">Putnam</option>
<option value="081">Queens</option>
<option value="083">Rensselaer</option>
<option value="085">Richmond</option>
<option value="087">Rockland</option>
<option value="091">Saratoga</option>
<option value="093">Schenectady</option>
<option value="095">Schoharie</option>
<option value="097">Schuyler</option>
<option value="099">Seneca</option>
<option value="089">St. Lawrence</option>
<option value="101">Steuben</option>
<option value="103">Suffolk</option>
<option value="105">Sullivan</option>
<option value="107">Tioga</option>
<option value="109">Tompkins</option>
<option value="111">Ulster</option>
<option value="113">Warren</option>
<option value="115">Washington</option>
<option value="117">Wayne</option>
<option value="119">Westchester</option>
<option value="121">Wyoming</option>
<option value="123">Yates</option>
</select>'''
self.assertHTMLEqual(f.render('counties', '061'), out)