PageRenderTime 439ms CodeModel.GetById 433ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/localflavor/sk/forms.py

https://code.google.com/p/mango-py/
Python | 43 lines | 20 code | 6 blank | 17 comment | 0 complexity | f7fc81a23d0f4b27ee481d9dd52087cb MD5 | raw file
 1"""
 2Slovak-specific form helpers
 3"""
 4
 5from django.forms.fields import Select, RegexField
 6from django.utils.translation import ugettext_lazy as _
 7
 8class SKRegionSelect(Select):
 9    """
10    A select widget widget with list of Slovak regions as choices.
11    """
12    def __init__(self, attrs=None):
13        from sk_regions import REGION_CHOICES
14        super(SKRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
15
16class SKDistrictSelect(Select):
17    """
18    A select widget with list of Slovak districts as choices.
19    """
20    def __init__(self, attrs=None):
21        from sk_districts import DISTRICT_CHOICES
22        super(SKDistrictSelect, self).__init__(attrs, choices=DISTRICT_CHOICES)
23
24class SKPostalCodeField(RegexField):
25    """
26    A form field that validates its input as Slovak postal code.
27    Valid form is XXXXX or XXX XX, where X represents integer.
28    """
29    default_error_messages = {
30        'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
31    }
32
33    def __init__(self, *args, **kwargs):
34        super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
35            max_length=None, min_length=None, *args, **kwargs)
36
37    def clean(self, value):
38        """
39        Validates the input and returns a string that contains only numbers.
40        Returns an empty string for empty values.
41        """
42        v = super(SKPostalCodeField, self).clean(value)
43        return v.replace(' ', '')