|
0
|
1 |
""" |
|
|
2 |
JP-specific Form helpers |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
from django.forms import ValidationError |
|
|
6 |
from django.utils.translation import ugettext_lazy as _ |
|
|
7 |
from django.forms.fields import RegexField, Select |
|
|
8 |
|
|
|
9 |
class JPPostalCodeField(RegexField): |
|
|
10 |
""" |
|
|
11 |
A form field that validates its input is a Japanese postcode. |
|
|
12 |
|
|
|
13 |
Accepts 7 digits, with or without a hyphen. |
|
|
14 |
""" |
|
|
15 |
default_error_messages = { |
|
|
16 |
'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'), |
|
|
17 |
} |
|
|
18 |
|
|
|
19 |
def __init__(self, *args, **kwargs): |
|
|
20 |
super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$', |
|
|
21 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
22 |
|
|
|
23 |
def clean(self, value): |
|
|
24 |
""" |
|
|
25 |
Validates the input and returns a string that contains only numbers. |
|
|
26 |
Returns an empty string for empty values. |
|
|
27 |
""" |
|
|
28 |
v = super(JPPostalCodeField, self).clean(value) |
|
|
29 |
return v.replace('-', '') |
|
|
30 |
|
|
|
31 |
class JPPrefectureSelect(Select): |
|
|
32 |
""" |
|
|
33 |
A Select widget that uses a list of Japanese prefectures as its choices. |
|
|
34 |
""" |
|
|
35 |
def __init__(self, attrs=None): |
|
|
36 |
from jp_prefectures import JP_PREFECTURES |
|
|
37 |
super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES) |