2007-03-26 05:26:44 +08:00
|
|
|
"""
|
|
|
|
JP-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.contrib.localflavor.jp.jp_prefectures import JP_PREFECTURES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.fields import RegexField, Select
|
2011-10-18 08:47:49 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2007-03-26 05:26:44 +08:00
|
|
|
|
|
|
|
class JPPostalCodeField(RegexField):
|
|
|
|
"""
|
|
|
|
A form field that validates its input is a Japanese postcode.
|
|
|
|
|
|
|
|
Accepts 7 digits, with or without a hyphen.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-03-26 05:26:44 +08:00
|
|
|
super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-03-26 05:26:44 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates the input and returns a string that contains only numbers.
|
|
|
|
Returns an empty string for empty values.
|
|
|
|
"""
|
|
|
|
v = super(JPPostalCodeField, self).clean(value)
|
|
|
|
return v.replace('-', '')
|
|
|
|
|
|
|
|
class JPPrefectureSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Japanese prefectures as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)
|