2007-07-19 17:24:36 +08:00
|
|
|
"""
|
|
|
|
Slovak-specific form helpers
|
|
|
|
"""
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.contrib.localflavor.sk.sk_districts import DISTRICT_CHOICES
|
|
|
|
from django.contrib.localflavor.sk.sk_regions import REGION_CHOICES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.fields import Select, RegexField
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-07-19 17:24:36 +08:00
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-07-19 17:24:36 +08:00
|
|
|
class SKRegionSelect(Select):
|
|
|
|
"""
|
|
|
|
A select widget widget with list of Slovak regions as choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(SKRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
|
|
|
|
|
|
|
|
class SKDistrictSelect(Select):
|
|
|
|
"""
|
|
|
|
A select widget with list of Slovak districts as choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(SKDistrictSelect, self).__init__(attrs, choices=DISTRICT_CHOICES)
|
|
|
|
|
|
|
|
class SKPostalCodeField(RegexField):
|
|
|
|
"""
|
|
|
|
A form field that validates its input as Slovak postal code.
|
|
|
|
Valid form is XXXXX or XXX XX, where X represents integer.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
|
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-07-19 17:24:36 +08:00
|
|
|
super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-07-19 17:24:36 +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(SKPostalCodeField, self).clean(value)
|
|
|
|
return v.replace(' ', '')
|