Ticket #3715: jplocalflavor_refine.diff

File jplocalflavor_refine.diff, 7.9 KB (added by makoto tsuyuki <mtsuyuki@…>, 18 years ago)

patch refined

  • django/contrib/localflavor/jp/jp_prefectures.py

     
     1from django.utils.translation import gettext_lazy as gettext_lazy
     2
     3JP_PREFECTURES = (
     4    ('', gettext_lazy('---'),),
     5    ('hokkaido', gettext_lazy('Hokkaido'),),
     6    ('aomori', gettext_lazy('Aomori'),),
     7    ('iwate', gettext_lazy('Iwate'),),
     8    ('miyagi', gettext_lazy('Miyagi'),),
     9    ('akita', gettext_lazy('Akita'),),
     10    ('yamagata', gettext_lazy('Yamagata'),),
     11    ('fukushima', gettext_lazy('Fukushima'),),
     12    ('ibaraki', gettext_lazy('Ibaraki'),),
     13    ('tochigi', gettext_lazy('Tochigi'),),
     14    ('gunma', gettext_lazy('Gunma'),),
     15    ('saitama', gettext_lazy('Saitama'),),
     16    ('chiba', gettext_lazy('Chiba'),),
     17    ('tokyo', gettext_lazy('Tokyo'),),
     18    ('kanagawa', gettext_lazy('Kanagawa'),),
     19    ('yamanashi', gettext_lazy('Yamanashi'),),
     20    ('nagano', gettext_lazy('Nagano'),),
     21    ('niigata', gettext_lazy('Niigata'),),
     22    ('toyama', gettext_lazy('Toyama'),),
     23    ('ishikawa', gettext_lazy('Ishikawa'),),
     24    ('fukui', gettext_lazy('Fukui'),),
     25    ('gifu', gettext_lazy('Gifu'),),
     26    ('shizuoka', gettext_lazy('Shizuoka'),),
     27    ('aichi', gettext_lazy('Aichi'),),
     28    ('mie', gettext_lazy('Mie'),),
     29    ('shiga', gettext_lazy('Shiga'),),
     30    ('kyoto', gettext_lazy('Kyoto'),),
     31    ('osaka', gettext_lazy('Osaka'),),
     32    ('hyogo', gettext_lazy('Hyogo'),),
     33    ('nara', gettext_lazy('Nara'),),
     34    ('wakayama', gettext_lazy('Wakayama'),),
     35    ('tottori', gettext_lazy('Tottori'),),
     36    ('shimane', gettext_lazy('Shimane'),),
     37    ('okayama', gettext_lazy('Okayama'),),
     38    ('hiroshima', gettext_lazy('Hiroshima'),),
     39    ('yamaguchi', gettext_lazy('Yamaguchi'),),
     40    ('tokushima', gettext_lazy('Tokushima'),),
     41    ('kagawa', gettext_lazy('Kagawa'),),
     42    ('ehime', gettext_lazy('Ehime'),),
     43    ('kochi', gettext_lazy('Kochi'),),
     44    ('fukuoka', gettext_lazy('Fukuoka'),),
     45    ('saga', gettext_lazy('Saga'),),
     46    ('nagasaki', gettext_lazy('Nagasaki'),),
     47    ('kumamoto', gettext_lazy('Kumamoto'),),
     48    ('oita', gettext_lazy('Oita'),),
     49    ('miyazaki', gettext_lazy('Miyazaki'),),
     50    ('kagoshima', gettext_lazy('Kagoshima'),),
     51    ('okinawa', gettext_lazy('Okinawa'),),
     52)
  • django/contrib/localflavor/jp/forms.py

     
     1"""
     2JP-specific Form helpers
     3"""
     4
     5from django.core import validators
     6from django.newforms import ValidationError
     7from django.utils.translation import gettext
     8from django.newforms.fields import RegexField, Select
     9
     10import re
     11
     12class JPPostalCodeField(RegexField):
     13    """
     14    A form field that validates its input is a Japanese postcode.
     15
     16    Accepts 7 digits(with/out 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.
     27        Returns it converted to a string that has only numbers.
     28        Returns None for empty values.
     29        """
     30        v = super(JPPostalCodeField, self).clean(value)
     31        return v.replace('-', '')
     32
     33class JPPrefectureSelect(Select):
     34    """
     35    A Select widget that uses a list of Japanese prefectures as its choices.
     36    """
     37    def __init__(self, attrs=None):
     38        from jp_prefectures import JP_PREFECTURES
     39        super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)
  • tests/regressiontests/forms/tests.py

     
    35563556>>> f.clean('')
    35573557u''
    35583558
     3559# JPPostalCodeField ###############################################################
     3560
     3561A form field that validates its input is a Japanese postcode.
     3562
     3563Accepts 7 digits(with/out hyphen).
     3564>>> from django.contrib.localflavor.jp.forms import JPPostalCodeField
     3565>>> f = JPPostalCodeField()
     3566>>> f.clean('251-0032')
     3567u'2510032'
     3568>>> f.clean('2510032')
     3569u'2510032'
     3570>>> f.clean('2510-032')
     3571Traceback (most recent call last):
     3572...
     3573ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
     3574>>> f.clean('251a0032')
     3575Traceback (most recent call last):
     3576...
     3577ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
     3578>>> f.clean('a51-0032')
     3579Traceback (most recent call last):
     3580...
     3581ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
     3582>>> f.clean('25100321')
     3583Traceback (most recent call last):
     3584...
     3585ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
     3586>>> f.clean('')
     3587Traceback (most recent call last):
     3588...
     3589ValidationError: [u'This field is required.']
     3590>>> f = JPPostalCodeField(required=False)
     3591>>> f.clean('251-0032')
     3592u'2510032'
     3593>>> f.clean('2510032')
     3594u'2510032'
     3595>>> f.clean('2510-032')
     3596Traceback (most recent call last):
     3597...
     3598ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
     3599>>> f.clean('')
     3600u''
     3601
     3602# JPPrefectureSelect ###############################################################
     3603
     3604A Select widget that uses a list of Japanese prefectures as its choices.
     3605>>> from django.contrib.localflavor.jp.forms import JPPrefectureSelect
     3606>>> w = JPPrefectureSelect()
     3607>>> print w.render('prefecture', 'kanagawa')
     3608<select name="prefecture">
     3609<option value="">---</option>
     3610<option value="hokkaido">Hokkaido</option>
     3611<option value="aomori">Aomori</option>
     3612<option value="iwate">Iwate</option>
     3613<option value="miyagi">Miyagi</option>
     3614<option value="akita">Akita</option>
     3615<option value="yamagata">Yamagata</option>
     3616<option value="fukushima">Fukushima</option>
     3617<option value="ibaraki">Ibaraki</option>
     3618<option value="tochigi">Tochigi</option>
     3619<option value="gunma">Gunma</option>
     3620<option value="saitama">Saitama</option>
     3621<option value="chiba">Chiba</option>
     3622<option value="tokyo">Tokyo</option>
     3623<option value="kanagawa" selected="selected">Kanagawa</option>
     3624<option value="yamanashi">Yamanashi</option>
     3625<option value="nagano">Nagano</option>
     3626<option value="niigata">Niigata</option>
     3627<option value="toyama">Toyama</option>
     3628<option value="ishikawa">Ishikawa</option>
     3629<option value="fukui">Fukui</option>
     3630<option value="gifu">Gifu</option>
     3631<option value="shizuoka">Shizuoka</option>
     3632<option value="aichi">Aichi</option>
     3633<option value="mie">Mie</option>
     3634<option value="shiga">Shiga</option>
     3635<option value="kyoto">Kyoto</option>
     3636<option value="osaka">Osaka</option>
     3637<option value="hyogo">Hyogo</option>
     3638<option value="nara">Nara</option>
     3639<option value="wakayama">Wakayama</option>
     3640<option value="tottori">Tottori</option>
     3641<option value="shimane">Shimane</option>
     3642<option value="okayama">Okayama</option>
     3643<option value="hiroshima">Hiroshima</option>
     3644<option value="yamaguchi">Yamaguchi</option>
     3645<option value="tokushima">Tokushima</option>
     3646<option value="kagawa">Kagawa</option>
     3647<option value="ehime">Ehime</option>
     3648<option value="kochi">Kochi</option>
     3649<option value="fukuoka">Fukuoka</option>
     3650<option value="saga">Saga</option>
     3651<option value="nagasaki">Nagasaki</option>
     3652<option value="kumamoto">Kumamoto</option>
     3653<option value="oita">Oita</option>
     3654<option value="miyazaki">Miyazaki</option>
     3655<option value="kagoshima">Kagoshima</option>
     3656<option value="okinawa">Okinawa</option>
     3657</select>
     3658
     3659
    35593660#################################
    35603661# Tests of underlying functions #
    35613662#################################
Back to Top