Skip to content

Commit 6a5bada

Browse files
authored
feat(tw): init tw with administrative division select (#530)
1 parent 8668802 commit 6a5bada

File tree

8 files changed

+96
-1
lines changed

8 files changed

+96
-1
lines changed

docs/authors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Authors
9898
* Martin Ogden
9999
* Marti Raudsepp
100100
* Matias Dinota
101+
* Matt Wang
101102
* Michał Sałaban
102103
* Mike Lissner
103104
* Mohammed Al-Abdulhadi

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Changelog
66

77
New flavors:
88

9-
- None
9+
- Added local flavor for Taiwan
10+
(`gh-530 <https://github.com/django/django-localflavor/pull/530>`_).
1011

1112
New fields for existing flavors:
1213

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ validate Finnish social security numbers.
7575
* :doc:`localflavor/sk`
7676
* :doc:`localflavor/tn`
7777
* :doc:`localflavor/tr`
78+
* :doc:`localflavor/tw`
7879
* :doc:`localflavor/us`
7980
* :doc:`localflavor/uy`
8081
* :doc:`localflavor/za`

docs/localflavor/tw.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Taiwan (``tw``)
2+
===============
3+
4+
Forms
5+
-----
6+
7+
.. automodule:: localflavor.tw.forms
8+
:members:
9+
10+
.. autodata:: localflavor.tw.tw_choices.TW_ADMINISTRATIVE_DIVISION_CHOICES

localflavor/tw/__init__.py

Whitespace-only changes.

localflavor/tw/forms.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Taiwan-specific Form helpers."""
2+
3+
from django.forms.fields import Select
4+
5+
from .tw_choices import TW_ADMINISTRATIVE_DIVISION_CHOICES
6+
7+
__all__ = (
8+
'TWAdministrativeDivisionSelect',
9+
)
10+
11+
12+
class TWAdministrativeDivisionSelect(Select):
13+
"""
14+
A select widget providing the list of administrative divisions in Taiwan as choices.
15+
16+
.. versionadded:: 5.1
17+
"""
18+
19+
def __init__(self, attrs=None):
20+
super().__init__(attrs, choices=TW_ADMINISTRATIVE_DIVISION_CHOICES)

localflavor/tw/tw_choices.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from django.utils.translation import gettext_lazy as _
2+
3+
#: An alphabetical list of cities and counties for use as `choices` in a formfield.
4+
#: Based on the administrative divisions of Taiwan.
5+
#: https://en.wikipedia.org/wiki/List_of_administrative_divisions_of_Taiwan
6+
TW_ADMINISTRATIVE_DIVISION_CHOICES = (
7+
("changhua_county", _("Changhua County")), # 彰化縣
8+
("chiayi_city", _("Chiayi City")), # 嘉義市
9+
("chiayi_county", _("Chiayi County")), # 嘉義縣
10+
("hsinchu_city", _("Hsinchu City")), # 新竹市
11+
("hsinchu_county", _("Hsinchu County")), # 新竹縣
12+
("hualien_county", _("Hualien County")), # 花蓮縣
13+
("kaohsiung_city", _("Kaohsiung City")), # 高雄市
14+
("keelung_city", _("Keelung City")), # 基隆市
15+
("kinmen_county", _("Kinmen County")), # 金門縣
16+
("lienchiang_county", _("Lienchiang County")), # 連江縣
17+
("miaoli_county", _("Miaoli County")), # 苗栗縣
18+
("nantou_county", _("Nantou County")), # 南投縣
19+
("new_taipei_city", _("New Taipei City")), # 新北市
20+
("penghu_county", _("Penghu County")), # 澎湖縣
21+
("pingtung_county", _("Pingtung County")), # 屏東縣
22+
("taichung_city", _("Taichung City")), # 台中市
23+
("tainan_city", _("Tainan City")), # 台南市
24+
("taipei_city", _("Taipei City")), # 台北市
25+
("taitung_county", _("Taitung County")), # 台東縣
26+
("taoyuan_city", _("Taoyuan City")), # 桃園市
27+
("yilan_county", _("Yilan County")), # 宜蘭縣
28+
("yunlin_county", _("Yunlin County")), # 雲林縣
29+
)

tests/test_tw.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.test import SimpleTestCase
2+
3+
from localflavor.tw.forms import TWAdministrativeDivisionSelect
4+
5+
6+
class TWLocalFlavorTests(SimpleTestCase):
7+
def test_TWAdministrativeDivisionSelect(self):
8+
f = TWAdministrativeDivisionSelect()
9+
correct_output = '''<select name="administrative_divisions">
10+
<option value="changhua_county">Changhua County</option>
11+
<option value="chiayi_city">Chiayi City</option>
12+
<option value="chiayi_county">Chiayi County</option>
13+
<option value="hsinchu_city">Hsinchu City</option>
14+
<option value="hsinchu_county">Hsinchu County</option>
15+
<option value="hualien_county">Hualien County</option>
16+
<option value="kaohsiung_city">Kaohsiung City</option>
17+
<option value="keelung_city">Keelung City</option>
18+
<option value="kinmen_county">Kinmen County</option>
19+
<option value="lienchiang_county">Lienchiang County</option>
20+
<option value="miaoli_county">Miaoli County</option>
21+
<option value="nantou_county">Nantou County</option>
22+
<option value="new_taipei_city">New Taipei City</option>
23+
<option value="penghu_county">Penghu County</option>
24+
<option value="pingtung_county">Pingtung County</option>
25+
<option value="taichung_city">Taichung City</option>
26+
<option value="tainan_city">Tainan City</option>
27+
<option value="taipei_city" selected="selected">Taipei City</option>
28+
<option value="taitung_county">Taitung County</option>
29+
<option value="taoyuan_city">Taoyuan City</option>
30+
<option value="yilan_county">Yilan County</option>
31+
<option value="yunlin_county">Yunlin County</option>
32+
</select>'''
33+
self.assertHTMLEqual(f.render('administrative_divisions', 'taipei_city'), correct_output)

0 commit comments

Comments
 (0)