|
1 """ |
|
2 Slovak-specific form helpers |
|
3 """ |
|
4 |
|
5 from django.newforms.fields import Select, RegexField |
|
6 from django.utils.translation import ugettext |
|
7 |
|
8 class 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 |
|
16 class 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 |
|
24 class 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': ugettext(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(' ', '') |