Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit f2861dad432c7f3a21c5e76f6955ada35ace6128 1 parent e2c04e8
Adrian Holovaty authored March 25, 2007
38  django/contrib/localflavor/jp/forms.py
... ...
@@ -0,0 +1,38 @@
  1
+"""
  2
+JP-specific Form helpers
  3
+"""
  4
+
  5
+from django.core import validators
  6
+from django.newforms import ValidationError
  7
+from django.utils.translation import gettext
  8
+from django.newforms.fields import RegexField, Select
  9
+
  10
+import re
  11
+
  12
+class JPPostalCodeField(RegexField):
  13
+    """
  14
+    A form field that validates its input is a Japanese postcode.
  15
+
  16
+    Accepts 7 digits, with or without a hyphen.
  17
+    """
  18
+    def __init__(self, *args, **kwargs):
  19
+        super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
  20
+            max_length=None, min_length=None,
  21
+            error_message=gettext(u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
  22
+            *args, **kwargs)
  23
+
  24
+    def clean(self, value):
  25
+        """
  26
+        Validates the input and returns a string that contains only numbers.
  27
+        Returns an empty string for empty values.
  28
+        """
  29
+        v = super(JPPostalCodeField, self).clean(value)
  30
+        return v.replace('-', '')
  31
+
  32
+class JPPrefectureSelect(Select):
  33
+    """
  34
+    A Select widget that uses a list of Japanese prefectures as its choices.
  35
+    """
  36
+    def __init__(self, attrs=None):
  37
+        from jp_prefectures import JP_PREFECTURES
  38
+        super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)
51  django/contrib/localflavor/jp/jp_prefectures.py
... ...
@@ -0,0 +1,51 @@
  1
+from django.utils.translation import gettext_lazy as gettext_lazy
  2
+
  3
+JP_PREFECTURES = (
  4
+    ('hokkaido', gettext_lazy('Hokkaido'),),
  5
+    ('aomori', gettext_lazy('Aomori'),),
  6
+    ('iwate', gettext_lazy('Iwate'),),
  7
+    ('miyagi', gettext_lazy('Miyagi'),),
  8
+    ('akita', gettext_lazy('Akita'),),
  9
+    ('yamagata', gettext_lazy('Yamagata'),),
  10
+    ('fukushima', gettext_lazy('Fukushima'),),
  11
+    ('ibaraki', gettext_lazy('Ibaraki'),),
  12
+    ('tochigi', gettext_lazy('Tochigi'),),
  13
+    ('gunma', gettext_lazy('Gunma'),),
  14
+    ('saitama', gettext_lazy('Saitama'),),
  15
+    ('chiba', gettext_lazy('Chiba'),),
  16
+    ('tokyo', gettext_lazy('Tokyo'),),
  17
+    ('kanagawa', gettext_lazy('Kanagawa'),),
  18
+    ('yamanashi', gettext_lazy('Yamanashi'),),
  19
+    ('nagano', gettext_lazy('Nagano'),),
  20
+    ('niigata', gettext_lazy('Niigata'),),
  21
+    ('toyama', gettext_lazy('Toyama'),),
  22
+    ('ishikawa', gettext_lazy('Ishikawa'),),
  23
+    ('fukui', gettext_lazy('Fukui'),),
  24
+    ('gifu', gettext_lazy('Gifu'),),
  25
+    ('shizuoka', gettext_lazy('Shizuoka'),),
  26
+    ('aichi', gettext_lazy('Aichi'),),
  27
+    ('mie', gettext_lazy('Mie'),),
  28
+    ('shiga', gettext_lazy('Shiga'),),
  29
+    ('kyoto', gettext_lazy('Kyoto'),),
  30
+    ('osaka', gettext_lazy('Osaka'),),
  31
+    ('hyogo', gettext_lazy('Hyogo'),),
  32
+    ('nara', gettext_lazy('Nara'),),
  33
+    ('wakayama', gettext_lazy('Wakayama'),),
  34
+    ('tottori', gettext_lazy('Tottori'),),
  35
+    ('shimane', gettext_lazy('Shimane'),),
  36
+    ('okayama', gettext_lazy('Okayama'),),
  37
+    ('hiroshima', gettext_lazy('Hiroshima'),),
  38
+    ('yamaguchi', gettext_lazy('Yamaguchi'),),
  39
+    ('tokushima', gettext_lazy('Tokushima'),),
  40
+    ('kagawa', gettext_lazy('Kagawa'),),
  41
+    ('ehime', gettext_lazy('Ehime'),),
  42
+    ('kochi', gettext_lazy('Kochi'),),
  43
+    ('fukuoka', gettext_lazy('Fukuoka'),),
  44
+    ('saga', gettext_lazy('Saga'),),
  45
+    ('nagasaki', gettext_lazy('Nagasaki'),),
  46
+    ('kumamoto', gettext_lazy('Kumamoto'),),
  47
+    ('oita', gettext_lazy('Oita'),),
  48
+    ('miyazaki', gettext_lazy('Miyazaki'),),
  49
+    ('kagoshima', gettext_lazy('Kagoshima'),),
  50
+    ('okinawa', gettext_lazy('Okinawa'),),
  51
+)
0  localflavor/jp/__init__.py b/django/contrib/localflavor/jp/__init__.py
No changes.
103  tests/regressiontests/forms/tests.py
@@ -3776,6 +3776,109 @@
3776 3776
 <option value="988">988 - Nouvelle-Caledonie</option>
3777 3777
 </select>
3778 3778
 
  3779
+# JPPostalCodeField ###############################################################
  3780
+
  3781
+A form field that validates its input is a Japanese postcode.
  3782
+
  3783
+Accepts 7 digits(with/out hyphen).
  3784
+>>> from django.contrib.localflavor.jp.forms import JPPostalCodeField
  3785
+>>> f = JPPostalCodeField()
  3786
+>>> f.clean('251-0032')
  3787
+u'2510032'
  3788
+>>> f.clean('2510032')
  3789
+u'2510032'
  3790
+>>> f.clean('2510-032')
  3791
+Traceback (most recent call last):
  3792
+...
  3793
+ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  3794
+>>> f.clean('251a0032')
  3795
+Traceback (most recent call last):
  3796
+...
  3797
+ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  3798
+>>> f.clean('a51-0032')
  3799
+Traceback (most recent call last):
  3800
+...
  3801
+ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  3802
+>>> f.clean('25100321')
  3803
+Traceback (most recent call last):
  3804
+...
  3805
+ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  3806
+>>> f.clean('')
  3807
+Traceback (most recent call last):
  3808
+...
  3809
+ValidationError: [u'This field is required.']
  3810
+
  3811
+>>> f = JPPostalCodeField(required=False)
  3812
+>>> f.clean('251-0032')
  3813
+u'2510032'
  3814
+>>> f.clean('2510032')
  3815
+u'2510032'
  3816
+>>> f.clean('2510-032')
  3817
+Traceback (most recent call last):
  3818
+...
  3819
+ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  3820
+>>> f.clean('')
  3821
+u''
  3822
+>>> f.clean(None)
  3823
+u''
  3824
+
  3825
+# JPPrefectureSelect ###############################################################
  3826
+
  3827
+A Select widget that uses a list of Japanese prefectures as its choices.
  3828
+>>> from django.contrib.localflavor.jp.forms import JPPrefectureSelect
  3829
+>>> w = JPPrefectureSelect()
  3830
+>>> print w.render('prefecture', 'kanagawa')
  3831
+<select name="prefecture">
  3832
+<option value="hokkaido">Hokkaido</option>
  3833
+<option value="aomori">Aomori</option>
  3834
+<option value="iwate">Iwate</option>
  3835
+<option value="miyagi">Miyagi</option>
  3836
+<option value="akita">Akita</option>
  3837
+<option value="yamagata">Yamagata</option>
  3838
+<option value="fukushima">Fukushima</option>
  3839
+<option value="ibaraki">Ibaraki</option>
  3840
+<option value="tochigi">Tochigi</option>
  3841
+<option value="gunma">Gunma</option>
  3842
+<option value="saitama">Saitama</option>
  3843
+<option value="chiba">Chiba</option>
  3844
+<option value="tokyo">Tokyo</option>
  3845
+<option value="kanagawa" selected="selected">Kanagawa</option>
  3846
+<option value="yamanashi">Yamanashi</option>
  3847
+<option value="nagano">Nagano</option>
  3848
+<option value="niigata">Niigata</option>
  3849
+<option value="toyama">Toyama</option>
  3850
+<option value="ishikawa">Ishikawa</option>
  3851
+<option value="fukui">Fukui</option>
  3852
+<option value="gifu">Gifu</option>
  3853
+<option value="shizuoka">Shizuoka</option>
  3854
+<option value="aichi">Aichi</option>
  3855
+<option value="mie">Mie</option>
  3856
+<option value="shiga">Shiga</option>
  3857
+<option value="kyoto">Kyoto</option>
  3858
+<option value="osaka">Osaka</option>
  3859
+<option value="hyogo">Hyogo</option>
  3860
+<option value="nara">Nara</option>
  3861
+<option value="wakayama">Wakayama</option>
  3862
+<option value="tottori">Tottori</option>
  3863
+<option value="shimane">Shimane</option>
  3864
+<option value="okayama">Okayama</option>
  3865
+<option value="hiroshima">Hiroshima</option>
  3866
+<option value="yamaguchi">Yamaguchi</option>
  3867
+<option value="tokushima">Tokushima</option>
  3868
+<option value="kagawa">Kagawa</option>
  3869
+<option value="ehime">Ehime</option>
  3870
+<option value="kochi">Kochi</option>
  3871
+<option value="fukuoka">Fukuoka</option>
  3872
+<option value="saga">Saga</option>
  3873
+<option value="nagasaki">Nagasaki</option>
  3874
+<option value="kumamoto">Kumamoto</option>
  3875
+<option value="oita">Oita</option>
  3876
+<option value="miyazaki">Miyazaki</option>
  3877
+<option value="kagoshima">Kagoshima</option>
  3878
+<option value="okinawa">Okinawa</option>
  3879
+</select>
  3880
+
  3881
+
3779 3882
 #################################
3780 3883
 # Tests of underlying functions #
3781 3884
 #################################

0 notes on commit f2861da

Please sign in to comment.
Something went wrong with that request. Please try again.