PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 38ms app.codeStats 0ms

/django/contrib/localflavor/jp/forms.py

https://code.google.com/p/mango-py/
Python | 37 lines | 21 code | 4 blank | 12 comment | 0 complexity | 9106a6f1453795cc0466560d4aac33e4 MD5 | raw file
 1"""
 2JP-specific Form helpers
 3"""
 4
 5from django.forms import ValidationError
 6from django.utils.translation import ugettext_lazy as _
 7from django.forms.fields import RegexField, Select
 8
 9class 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
31class 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)