Fixed #3715 -- Added Japanese package to django.contrib.localflavor. Thanks, Makoto Tsuyuki

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4822 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-03-25 21:26:44 +00:00
parent e2c04e8cf2
commit f2861dad43
4 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,38 @@
"""
JP-specific Form helpers
"""
from django.core import validators
from django.newforms import ValidationError
from django.utils.translation import gettext
from django.newforms.fields import RegexField, Select
import re
class JPPostalCodeField(RegexField):
"""
A form field that validates its input is a Japanese postcode.
Accepts 7 digits, with or without a hyphen.
"""
def __init__(self, *args, **kwargs):
super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
max_length=None, min_length=None,
error_message=gettext(u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
*args, **kwargs)
def clean(self, value):
"""
Validates the input and returns a string that contains only numbers.
Returns an empty string for empty values.
"""
v = super(JPPostalCodeField, self).clean(value)
return v.replace('-', '')
class JPPrefectureSelect(Select):
"""
A Select widget that uses a list of Japanese prefectures as its choices.
"""
def __init__(self, attrs=None):
from jp_prefectures import JP_PREFECTURES
super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)

View File

@ -0,0 +1,51 @@
from django.utils.translation import gettext_lazy as gettext_lazy
JP_PREFECTURES = (
('hokkaido', gettext_lazy('Hokkaido'),),
('aomori', gettext_lazy('Aomori'),),
('iwate', gettext_lazy('Iwate'),),
('miyagi', gettext_lazy('Miyagi'),),
('akita', gettext_lazy('Akita'),),
('yamagata', gettext_lazy('Yamagata'),),
('fukushima', gettext_lazy('Fukushima'),),
('ibaraki', gettext_lazy('Ibaraki'),),
('tochigi', gettext_lazy('Tochigi'),),
('gunma', gettext_lazy('Gunma'),),
('saitama', gettext_lazy('Saitama'),),
('chiba', gettext_lazy('Chiba'),),
('tokyo', gettext_lazy('Tokyo'),),
('kanagawa', gettext_lazy('Kanagawa'),),
('yamanashi', gettext_lazy('Yamanashi'),),
('nagano', gettext_lazy('Nagano'),),
('niigata', gettext_lazy('Niigata'),),
('toyama', gettext_lazy('Toyama'),),
('ishikawa', gettext_lazy('Ishikawa'),),
('fukui', gettext_lazy('Fukui'),),
('gifu', gettext_lazy('Gifu'),),
('shizuoka', gettext_lazy('Shizuoka'),),
('aichi', gettext_lazy('Aichi'),),
('mie', gettext_lazy('Mie'),),
('shiga', gettext_lazy('Shiga'),),
('kyoto', gettext_lazy('Kyoto'),),
('osaka', gettext_lazy('Osaka'),),
('hyogo', gettext_lazy('Hyogo'),),
('nara', gettext_lazy('Nara'),),
('wakayama', gettext_lazy('Wakayama'),),
('tottori', gettext_lazy('Tottori'),),
('shimane', gettext_lazy('Shimane'),),
('okayama', gettext_lazy('Okayama'),),
('hiroshima', gettext_lazy('Hiroshima'),),
('yamaguchi', gettext_lazy('Yamaguchi'),),
('tokushima', gettext_lazy('Tokushima'),),
('kagawa', gettext_lazy('Kagawa'),),
('ehime', gettext_lazy('Ehime'),),
('kochi', gettext_lazy('Kochi'),),
('fukuoka', gettext_lazy('Fukuoka'),),
('saga', gettext_lazy('Saga'),),
('nagasaki', gettext_lazy('Nagasaki'),),
('kumamoto', gettext_lazy('Kumamoto'),),
('oita', gettext_lazy('Oita'),),
('miyazaki', gettext_lazy('Miyazaki'),),
('kagoshima', gettext_lazy('Kagoshima'),),
('okinawa', gettext_lazy('Okinawa'),),
)

View File

@ -3776,6 +3776,109 @@ including DOM TOM
<option value="988">988 - Nouvelle-Caledonie</option>
</select>
# JPPostalCodeField ###############################################################
A form field that validates its input is a Japanese postcode.
Accepts 7 digits(with/out hyphen).
>>> from django.contrib.localflavor.jp.forms import JPPostalCodeField
>>> f = JPPostalCodeField()
>>> f.clean('251-0032')
u'2510032'
>>> f.clean('2510032')
u'2510032'
>>> f.clean('2510-032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('251a0032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('a51-0032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('25100321')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = JPPostalCodeField(required=False)
>>> f.clean('251-0032')
u'2510032'
>>> f.clean('2510032')
u'2510032'
>>> f.clean('2510-032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('')
u''
>>> f.clean(None)
u''
# JPPrefectureSelect ###############################################################
A Select widget that uses a list of Japanese prefectures as its choices.
>>> from django.contrib.localflavor.jp.forms import JPPrefectureSelect
>>> w = JPPrefectureSelect()
>>> print w.render('prefecture', 'kanagawa')
<select name="prefecture">
<option value="hokkaido">Hokkaido</option>
<option value="aomori">Aomori</option>
<option value="iwate">Iwate</option>
<option value="miyagi">Miyagi</option>
<option value="akita">Akita</option>
<option value="yamagata">Yamagata</option>
<option value="fukushima">Fukushima</option>
<option value="ibaraki">Ibaraki</option>
<option value="tochigi">Tochigi</option>
<option value="gunma">Gunma</option>
<option value="saitama">Saitama</option>
<option value="chiba">Chiba</option>
<option value="tokyo">Tokyo</option>
<option value="kanagawa" selected="selected">Kanagawa</option>
<option value="yamanashi">Yamanashi</option>
<option value="nagano">Nagano</option>
<option value="niigata">Niigata</option>
<option value="toyama">Toyama</option>
<option value="ishikawa">Ishikawa</option>
<option value="fukui">Fukui</option>
<option value="gifu">Gifu</option>
<option value="shizuoka">Shizuoka</option>
<option value="aichi">Aichi</option>
<option value="mie">Mie</option>
<option value="shiga">Shiga</option>
<option value="kyoto">Kyoto</option>
<option value="osaka">Osaka</option>
<option value="hyogo">Hyogo</option>
<option value="nara">Nara</option>
<option value="wakayama">Wakayama</option>
<option value="tottori">Tottori</option>
<option value="shimane">Shimane</option>
<option value="okayama">Okayama</option>
<option value="hiroshima">Hiroshima</option>
<option value="yamaguchi">Yamaguchi</option>
<option value="tokushima">Tokushima</option>
<option value="kagawa">Kagawa</option>
<option value="ehime">Ehime</option>
<option value="kochi">Kochi</option>
<option value="fukuoka">Fukuoka</option>
<option value="saga">Saga</option>
<option value="nagasaki">Nagasaki</option>
<option value="kumamoto">Kumamoto</option>
<option value="oita">Oita</option>
<option value="miyazaki">Miyazaki</option>
<option value="kagoshima">Kagoshima</option>
<option value="okinawa">Okinawa</option>
</select>
#################################
# Tests of underlying functions #
#################################