Ticket #3715: jplocalflavor.diff
File jplocalflavor.diff, 8.2 KB (added by , 18 years ago) |
---|
-
django/contrib/localflavor/jp/jp_prefectures.py
1 from django.utils.translation import gettext_lazy as gettext_lazy 2 3 JP_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
Property changes on: django/contrib/localflavor/jp/jp_prefectures.py ___________________________________________________________________ Name: svn:executable + *
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 from validators import jp_postal_code_re 13 14 class JPPostalCodeField(RegexField): 15 """ 16 A form field that validates its input is a Japanese postcode. 17 18 Accepts 7 digits(with/out hyphen). 19 """ 20 def __init__(self, *args, **kwargs): 21 super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$', 22 max_length=None, min_length=None, 23 error_message=gettext(u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'), 24 *args, **kwargs) 25 26 def clean(self, value): 27 """ 28 Validates the input. 29 Returns it converted to a string that has only numbers. 30 Returns None for empty values. 31 """ 32 v = super(JPPostalCodeField, self).clean(value) 33 return v.replace('-', '') 34 35 class JPPrefectureSelect(Select): 36 """ 37 A Select widget that uses a list of Japanese prefectures as its choices. 38 """ 39 def __init__(self, attrs=None): 40 from jp_prefectures import JP_PREFECTURES 41 super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES) -
tests/regressiontests/forms/tests.py
Property changes on: django/contrib/localflavor/jp/forms.py ___________________________________________________________________ Name: svn:executable + *
3556 3556 >>> f.clean('') 3557 3557 u'' 3558 3558 3559 # JPPostalCodeField ############################################################### 3560 3561 A form field that validates its input is a Japanese postcode. 3562 3563 Accepts 7 digits(with/out hyphen). 3564 >>> from django.contrib.localflavor.jp.forms import JPPostalCodeField 3565 >>> f = JPPostalCodeField() 3566 >>> f.clean('251-0032') 3567 u'2510032' 3568 >>> f.clean('2510032') 3569 u'2510032' 3570 >>> f.clean('2510-032') 3571 Traceback (most recent call last): 3572 ... 3573 ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'] 3574 >>> f.clean('251a0032') 3575 Traceback (most recent call last): 3576 ... 3577 ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'] 3578 >>> f.clean('a51-0032') 3579 Traceback (most recent call last): 3580 ... 3581 ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'] 3582 >>> f.clean('25100321') 3583 Traceback (most recent call last): 3584 ... 3585 ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'] 3586 >>> f.clean('') 3587 Traceback (most recent call last): 3588 ... 3589 ValidationError: [u'This field is required.'] 3590 >>> f = JPPostalCodeField(required=False) 3591 >>> f.clean('251-0032') 3592 u'2510032' 3593 >>> f.clean('2510032') 3594 u'2510032' 3595 >>> f.clean('2510-032') 3596 Traceback (most recent call last): 3597 ... 3598 ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'] 3599 >>> f.clean('') 3600 u'' 3601 3602 # JPPrefectureSelect ############################################################### 3603 3604 A 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 3559 3659 ################################# 3560 3660 # Tests of underlying functions # 3561 3661 #################################