Ticket #13838: localflavor_jp.patch
File localflavor_jp.patch, 10.4 KB (added by , 14 years ago) |
---|
-
django/contrib/localflavor/jp/forms.py
diff --git a/django/contrib/localflavor/jp/forms.py b/django/contrib/localflavor/jp/forms.py index 1d765c8..e6426f6 100644
a b 1 #:coding=utf-8: 1 2 """ 2 3 JP-specific Form helpers 3 4 """ … … class JPPrefectureSelect(Select): 35 36 def __init__(self, attrs=None): 36 37 from jp_prefectures import JP_PREFECTURES 37 38 super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES) 39 40 class JPPrefectureCodeSelect(Select): 41 """ 42 A Select widget that uses a list of Japanese prefectures as its choices 43 and the prefecture code as the post value. 44 """ 45 def __init__(self, attrs=None): 46 from jp_prefectures import JP_PREFECTURE_CODES 47 super(JPPrefectureCodeSelect, self).__init__(attrs, choices=JP_PREFECTURE_CODES) 48 49 # Common character validation fields for Japanese. 50 # Must be used with unicode objects. 51 52 HIRAGANA_RE = ur'^[ぁ-ゞー〜~& 、・]*$' 53 class HiraganaCharField(RegexField): 54 """ 55 A field for accepting only Hiragana characters. 56 """ 57 default_error_messages = { 58 'invalid': _('Enter Hiragana characters only.'), 59 } 60 def __init__(self, *args, **kwargs): 61 super(HiraganaCharField, self).__init__(HIRAGANA_RE, *args, **kwargs) 62 63 KATAKANA_RE = ur'^[ァ-ヴー〜~& 、・]*$' 64 class KatakanaCharField(RegexField): 65 """ 66 A field for accepting only full width Katakana characters. 67 """ 68 default_error_messages = { 69 'invalid': _('Enter Katakana characters only.'), 70 } 71 def __init__(self, *args, **kwargs): 72 super(KatakanaCharField, self).__init__(KATAKANA_RE, *args, **kwargs) 73 74 FULL_WIDTH_RE = ur'^[一-龠]*|[ぁ-ゞ]*|[ァ-ヴ]*|[0-9]*$' 75 class FullWidthCharField(RegexField): 76 """ 77 A field for accepting only full width Japanese characters. 78 """ 79 default_error_messages = { 80 'invalid': _('Enter full width characters only.'), 81 } 82 def __init__(self, *args, **kwargs): 83 super(FullWidthCharField, self).__init__(FULL_WIDTH_RE, *args, **kwargs) 84 85 HALF_WIDTH_KATAKANA_RE = ur'^[ァ-゚-~& ,]*$' 86 class HalfWidthKatakanaCharField(RegexField): 87 """ 88 A field for accepting only half width Katakana characters. 89 """ 90 default_error_messages = { 91 'invalid': _('Enter half width Katakana characters only.'), 92 } 93 def __init__(self, *args, **kwargs): 94 super(HalfWidthKatakanaCharField, self).__init__(FULL_WIDTH_RE, *args, **kwargs) -
django/contrib/localflavor/jp/jp_prefectures.py
diff --git a/django/contrib/localflavor/jp/jp_prefectures.py b/django/contrib/localflavor/jp/jp_prefectures.py index f079fe6..2d4e00a 100644
a b 1 1 from django.utils.translation import ugettext_lazy 2 2 3 """ 4 Prefectures ordered to conform with the Japanese entry 5 of ISO-3166 as this ordering is widely used in Japan. 6 7 See: 8 http://en.wikipedia.org/wiki/ISO_3166-2:JP 9 """ 10 3 11 JP_PREFECTURES = ( 4 12 ('hokkaido', ugettext_lazy('Hokkaido'),), 5 13 ('aomori', ugettext_lazy('Aomori'),), … … JP_PREFECTURES = ( 15 23 ('chiba', ugettext_lazy('Chiba'),), 16 24 ('tokyo', ugettext_lazy('Tokyo'),), 17 25 ('kanagawa', ugettext_lazy('Kanagawa'),), 18 ('yamanashi', ugettext_lazy('Yamanashi'),),19 ('nagano', ugettext_lazy('Nagano'),),20 26 ('niigata', ugettext_lazy('Niigata'),), 21 27 ('toyama', ugettext_lazy('Toyama'),), 22 28 ('ishikawa', ugettext_lazy('Ishikawa'),), 23 29 ('fukui', ugettext_lazy('Fukui'),), 30 ('yamanashi', ugettext_lazy('Yamanashi'),), 31 ('nagano', ugettext_lazy('Nagano'),), 24 32 ('gifu', ugettext_lazy('Gifu'),), 25 33 ('shizuoka', ugettext_lazy('Shizuoka'),), 26 34 ('aichi', ugettext_lazy('Aichi'),), … … JP_PREFECTURES = ( 49 57 ('kagoshima', ugettext_lazy('Kagoshima'),), 50 58 ('okinawa', ugettext_lazy('Okinawa'),), 51 59 ) 60 61 JP_PREFECTURE_CODES = ( 62 (1, ugettext_lazy('Hokkaido'),), 63 (2, ugettext_lazy('Aomori'),), 64 (3, ugettext_lazy('Iwate'),), 65 (4, ugettext_lazy('Miyagi'),), 66 (5, ugettext_lazy('Akita'),), 67 (6, ugettext_lazy('Yamagata'),), 68 (7, ugettext_lazy('Fukushima'),), 69 (8, ugettext_lazy('Ibaraki'),), 70 (9, ugettext_lazy('Tochigi'),), 71 (10, ugettext_lazy('Gunma'),), 72 (11, ugettext_lazy('Saitama'),), 73 (12, ugettext_lazy('Chiba'),), 74 (13, ugettext_lazy('Tokyo'),), 75 (14, ugettext_lazy('Kanagawa'),), 76 (15, ugettext_lazy('Niigata'),), 77 (16, ugettext_lazy('Toyama'),), 78 (17, ugettext_lazy('Ishikawa'),), 79 (18, ugettext_lazy('Fukui'),), 80 (19, ugettext_lazy('Yamanashi'),), 81 (20, ugettext_lazy('Nagano'),), 82 (21, ugettext_lazy('Gifu'),), 83 (22, ugettext_lazy('Shizuoka'),), 84 (23, ugettext_lazy('Aichi'),), 85 (24, ugettext_lazy('Mie'),), 86 (25, ugettext_lazy('Shiga'),), 87 (26, ugettext_lazy('Kyoto'),), 88 (27, ugettext_lazy('Osaka'),), 89 (28, ugettext_lazy('Hyogo'),), 90 (29, ugettext_lazy('Nara'),), 91 (30, ugettext_lazy('Wakayama'),), 92 (31, ugettext_lazy('Tottori'),), 93 (32, ugettext_lazy('Shimane'),), 94 (33, ugettext_lazy('Okayama'),), 95 (34, ugettext_lazy('Hiroshima'),), 96 (35, ugettext_lazy('Yamaguchi'),), 97 (36, ugettext_lazy('Tokushima'),), 98 (37, ugettext_lazy('Kagawa'),), 99 (38, ugettext_lazy('Ehime'),), 100 (39, ugettext_lazy('Kochi'),), 101 (40, ugettext_lazy('Fukuoka'),), 102 (41, ugettext_lazy('Saga'),), 103 (42, ugettext_lazy('Nagasaki'),), 104 (43, ugettext_lazy('Kumamoto'),), 105 (44, ugettext_lazy('Oita'),), 106 (45, ugettext_lazy('Miyazaki'),), 107 (46, ugettext_lazy('Kagoshima'),), 108 (47, ugettext_lazy('Okinawa'),), 109 ) -
tests/regressiontests/forms/localflavor/jp.py
diff --git a/tests/regressiontests/forms/localflavor/jp.py b/tests/regressiontests/forms/localflavor/jp.py index 7b5b826..7ec5070 100644
a b 1 1 # -*- coding: utf-8 -*- 2 2 # Tests for the contrib/localflavor/ JP form fields. 3 from django.test import TestCase 3 4 4 5 tests = r""" 5 6 # JPPostalCodeField ############################################################### … … A Select widget that uses a list of Japanese prefectures as its choices. 69 70 <option value="chiba">Chiba</option> 70 71 <option value="tokyo">Tokyo</option> 71 72 <option value="kanagawa" selected="selected">Kanagawa</option> 72 <option value="yamanashi">Yamanashi</option>73 <option value="nagano">Nagano</option>74 73 <option value="niigata">Niigata</option> 75 74 <option value="toyama">Toyama</option> 76 75 <option value="ishikawa">Ishikawa</option> 77 76 <option value="fukui">Fukui</option> 77 <option value="yamanashi">Yamanashi</option> 78 <option value="nagano">Nagano</option> 78 79 <option value="gifu">Gifu</option> 79 80 <option value="shizuoka">Shizuoka</option> 80 81 <option value="aichi">Aichi</option> … … A Select widget that uses a list of Japanese prefectures as its choices. 103 104 <option value="kagoshima">Kagoshima</option> 104 105 <option value="okinawa">Okinawa</option> 105 106 </select> 107 108 >>> from django.contrib.localflavor.jp.forms import JPPrefectureCodeSelect 109 >>> w = JPPrefectureCodeSelect() 110 >>> print w.render('prefecture', 'kanagawa') 111 <select name="prefecture"> 112 <option value="1">Hokkaido</option> 113 <option value="2">Aomori</option> 114 <option value="3">Iwate</option> 115 <option value="4">Miyagi</option> 116 <option value="5">Akita</option> 117 <option value="6">Yamagata</option> 118 <option value="7">Fukushima</option> 119 <option value="8">Ibaraki</option> 120 <option value="9">Tochigi</option> 121 <option value="10">Gunma</option> 122 <option value="11">Saitama</option> 123 <option value="12">Chiba</option> 124 <option value="13">Tokyo</option> 125 <option value="14">Kanagawa</option> 126 <option value="15">Niigata</option> 127 <option value="16">Toyama</option> 128 <option value="17">Ishikawa</option> 129 <option value="18">Fukui</option> 130 <option value="19">Yamanashi</option> 131 <option value="20">Nagano</option> 132 <option value="21">Gifu</option> 133 <option value="22">Shizuoka</option> 134 <option value="23">Aichi</option> 135 <option value="24">Mie</option> 136 <option value="25">Shiga</option> 137 <option value="26">Kyoto</option> 138 <option value="27">Osaka</option> 139 <option value="28">Hyogo</option> 140 <option value="29">Nara</option> 141 <option value="30">Wakayama</option> 142 <option value="31">Tottori</option> 143 <option value="32">Shimane</option> 144 <option value="33">Okayama</option> 145 <option value="34">Hiroshima</option> 146 <option value="35">Yamaguchi</option> 147 <option value="36">Tokushima</option> 148 <option value="37">Kagawa</option> 149 <option value="38">Ehime</option> 150 <option value="39">Kochi</option> 151 <option value="40">Fukuoka</option> 152 <option value="41">Saga</option> 153 <option value="42">Nagasaki</option> 154 <option value="43">Kumamoto</option> 155 <option value="44">Oita</option> 156 <option value="45">Miyazaki</option> 157 <option value="46">Kagoshima</option> 158 <option value="47">Okinawa</option> 159 </select> 106 160 """ 161 162 class JPLocalFlavorFormFieldTests(object): 163 def test_validation(self): 164 for text in self.texts: 165 form = self.formcls({"text": text}) 166 self.assertTrue(form.is_valid()) 167 168 for text in self.bad_texts: 169 form = self.formcls({"text": text}) 170 self.assertFalse(form.is_valid()) 171 172 class JPHiraganaTest(JPLocalFlavorFormFieldTests, TestCase): 173 def setUp(self): 174 self.formcls = HiraganaForm 175 self.texts = [u"ひらがな", u"~ひら&かなー", u"ほげほげ"] 176 self.bad_texts = [u"スパム", u"エッグス", u"漢字", u"ながすぎるてきすと"] 177 178 class JPKatakanaTest(JPLocalFlavorFormFieldTests, TestCase): 179 def setUp(self): 180 self.formcls = KatakanaForm 181 self.texts = [u"カタカナ", u"~カタ&カナー", u"ホゲホゲ"] 182 self.bad_texts = [u"すぱむ", u"えっぐず", u"漢字", u"ナガスギルテキスト"] 183 184 class JPFullWidthTest(JPLocalFlavorFormFieldTests, TestCase): 185 def setUp(self): 186 self.formcls = FullWidthForm 187 self.texts = [u"カタカナ", u"~ひら&カナー", u"ホゲはげ", u"番号0284"] 188 self.bad_texts = [u"ハンカク", u"en 英語", u"ぜんmo", u"1234", u"長すぎる文字テスト"] 189 190 class JPHalfWidthKatakanaTest(JPLocalFlavorFormFieldTests, TestCase): 191 def setUp(self): 192 self.formcls = FullWidthForm 193 self.texts = [u"ハンカク", u"イイナ~", u"ハン&カク", u"-テスト"] 194 self.bad_texts = [u"ハンカク2", u"moji", u"文字", u"1234", "ナガスギルテスト"]