Code

Ticket #13838: localflavor_jp.patch

File localflavor_jp.patch, 10.4 KB (added by IanLewis, 4 years ago)

A patch that enhances the Japanese localflavor module.

  • 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: 
    12""" 
    23JP-specific Form helpers 
    34""" 
    class JPPrefectureSelect(Select): 
    3536    def __init__(self, attrs=None): 
    3637        from jp_prefectures import JP_PREFECTURES 
    3738        super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES) 
     39 
     40class 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 
     52HIRAGANA_RE = ur'^[ぁ-ゞー〜~&  、・]*$' 
     53class 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 
     63KATAKANA_RE = ur'^[ァ-ヴー〜~&  、・]*$' 
     64class 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 
     74FULL_WIDTH_RE = ur'^[一-龠]*|[ぁ-ゞ]*|[ァ-ヴ]*|[0-9]*$' 
     75class 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 
     85HALF_WIDTH_KATAKANA_RE = ur'^[ァ-゚-~& ,]*$' 
     86class 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  
    11from django.utils.translation import ugettext_lazy 
    22 
     3""" 
     4Prefectures ordered to conform with the Japanese entry 
     5of ISO-3166 as this ordering is widely used in Japan. 
     6 
     7See: 
     8http://en.wikipedia.org/wiki/ISO_3166-2:JP 
     9""" 
     10 
    311JP_PREFECTURES = ( 
    412    ('hokkaido', ugettext_lazy('Hokkaido'),), 
    513    ('aomori', ugettext_lazy('Aomori'),), 
    JP_PREFECTURES = ( 
    1523    ('chiba', ugettext_lazy('Chiba'),), 
    1624    ('tokyo', ugettext_lazy('Tokyo'),), 
    1725    ('kanagawa', ugettext_lazy('Kanagawa'),), 
    18     ('yamanashi', ugettext_lazy('Yamanashi'),), 
    19     ('nagano', ugettext_lazy('Nagano'),), 
    2026    ('niigata', ugettext_lazy('Niigata'),), 
    2127    ('toyama', ugettext_lazy('Toyama'),), 
    2228    ('ishikawa', ugettext_lazy('Ishikawa'),), 
    2329    ('fukui', ugettext_lazy('Fukui'),), 
     30    ('yamanashi', ugettext_lazy('Yamanashi'),), 
     31    ('nagano', ugettext_lazy('Nagano'),), 
    2432    ('gifu', ugettext_lazy('Gifu'),), 
    2533    ('shizuoka', ugettext_lazy('Shizuoka'),), 
    2634    ('aichi', ugettext_lazy('Aichi'),), 
    JP_PREFECTURES = ( 
    4957    ('kagoshima', ugettext_lazy('Kagoshima'),), 
    5058    ('okinawa', ugettext_lazy('Okinawa'),), 
    5159) 
     60 
     61JP_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  
    11# -*- coding: utf-8 -*- 
    22# Tests for the contrib/localflavor/ JP form fields. 
     3from django.test import TestCase 
    34 
    45tests = r""" 
    56# JPPostalCodeField ############################################################### 
    A Select widget that uses a list of Japanese prefectures as its choices. 
    6970<option value="chiba">Chiba</option> 
    7071<option value="tokyo">Tokyo</option> 
    7172<option value="kanagawa" selected="selected">Kanagawa</option> 
    72 <option value="yamanashi">Yamanashi</option> 
    73 <option value="nagano">Nagano</option> 
    7473<option value="niigata">Niigata</option> 
    7574<option value="toyama">Toyama</option> 
    7675<option value="ishikawa">Ishikawa</option> 
    7776<option value="fukui">Fukui</option> 
     77<option value="yamanashi">Yamanashi</option> 
     78<option value="nagano">Nagano</option> 
    7879<option value="gifu">Gifu</option> 
    7980<option value="shizuoka">Shizuoka</option> 
    8081<option value="aichi">Aichi</option> 
    A Select widget that uses a list of Japanese prefectures as its choices. 
    103104<option value="kagoshima">Kagoshima</option> 
    104105<option value="okinawa">Okinawa</option> 
    105106</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> 
    106160""" 
     161 
     162class 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 
     172class 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 
     178class 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 
     184class 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         
     190class 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", "ナガスギルテスト"]