diff --git a/django/contrib/localflavor/sk/__init__.py b/django/contrib/localflavor/sk/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/django/contrib/localflavor/sk/forms.py b/django/contrib/localflavor/sk/forms.py new file mode 100644 index 00000000000..0aa000a0a56 --- /dev/null +++ b/django/contrib/localflavor/sk/forms.py @@ -0,0 +1,41 @@ +""" +Slovak-specific form helpers +""" + +from django.newforms.fields import Select, RegexField +from django.utils.translation import ugettext + +class SKRegionSelect(Select): + """ + A select widget widget with list of Slovak regions as choices. + """ + def __init__(self, attrs=None): + from sk_regions import REGION_CHOICES + 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): + from sk_districts import DISTRICT_CHOICES + 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. + """ + def __init__(self, *args, **kwargs): + super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$', + max_length=None, min_length=None, + error_message=ugettext(u'Enter a postal code in the format XXXXX or XXX XX.'), + *args, **kwargs) + + 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(' ', '') diff --git a/django/contrib/localflavor/sk/sk_districts.py b/django/contrib/localflavor/sk/sk_districts.py new file mode 100644 index 00000000000..95e87967b9e --- /dev/null +++ b/django/contrib/localflavor/sk/sk_districts.py @@ -0,0 +1,87 @@ +""" +Slovak districts according to http://sk.wikipedia.org/wiki/Administrat%C3%ADvne_%C4%8Dlenenie_Slovenska +""" + +from django.utils.translation import ugettext_lazy as _ + +DISTRICT_CHOICES = ( + ('BB', _('Banska Bystrica')), + ('BS', _('Banska Stiavnica')), + ('BJ', _('Bardejov')), + ('BN', _('Banovce nad Bebravou')), + ('BR', _('Brezno')), + ('BA1', _('Bratislava I')), + ('BA2', _('Bratislava II')), + ('BA3', _('Bratislava III')), + ('BA4', _('Bratislava IV')), + ('BA5', _('Bratislava V')), + ('BY', _('Bytca')), + ('CA', _('Cadca')), + ('DT', _('Detva')), + ('DK', _('Dolny Kubin')), + ('DS', _('Dunajska Streda')), + ('GA', _('Galanta')), + ('GL', _('Gelnica')), + ('HC', _('Hlohovec')), + ('HE', _('Humenne')), + ('IL', _('Ilava')), + ('KK', _('Kezmarok')), + ('KN', _('Komarno')), + ('KE1', _('Kosice I')), + ('KE2', _('Kosice II')), + ('KE3', _('Kosice III')), + ('KE4', _('Kosice IV')), + ('KEO', _('Kosice - okolie')), + ('KA', _('Krupina')), + ('KM', _('Kysucke Nove Mesto')), + ('LV', _('Levice')), + ('LE', _('Levoca')), + ('LM', _('Liptovsky Mikulas')), + ('LC', _('Lucenec')), + ('MA', _('Malacky')), + ('MT', _('Martin')), + ('ML', _('Medzilaborce')), + ('MI', _('Michalovce')), + ('MY', _('Myjava')), + ('NO', _('Namestovo')), + ('NR', _('Nitra')), + ('NM', _('Nove Mesto nad Vahom')), + ('NZ', _('Nove Zamky')), + ('PE', _('Partizanske')), + ('PK', _('Pezinok')), + ('PN', _('Piestany')), + ('PT', _('Poltar')), + ('PP', _('Poprad')), + ('PB', _('Povazska Bystrica')), + ('PO', _('Presov')), + ('PD', _('Prievidza')), + ('PU', _('Puchov')), + ('RA', _('Revuca')), + ('RS', _('Rimavska Sobota')), + ('RV', _('Roznava')), + ('RK', _('Ruzomberok')), + ('SB', _('Sabinov')), + ('SC', _('Senec')), + ('SE', _('Senica')), + ('SI', _('Skalica')), + ('SV', _('Snina')), + ('SO', _('Sobrance')), + ('SN', _('Spisska Nova Ves')), + ('SL', _('Stara Lubovna')), + ('SP', _('Stropkov')), + ('SK', _('Svidnik')), + ('SA', _('Sala')), + ('TO', _('Topolcany')), + ('TV', _('Trebisov')), + ('TN', _('Trencin')), + ('TT', _('Trnava')), + ('TR', _('Turcianske Teplice')), + ('TS', _('Tvrdosin')), + ('VK', _('Velky Krtis')), + ('VT', _('Vranov nad Toplou')), + ('ZM', _('Zlate Moravce')), + ('ZV', _('Zvolen')), + ('ZC', _('Zarnovica')), + ('ZH', _('Ziar nad Hronom')), + ('ZA', _('Zilina')), +) diff --git a/django/contrib/localflavor/sk/sk_regions.py b/django/contrib/localflavor/sk/sk_regions.py new file mode 100644 index 00000000000..66de814e018 --- /dev/null +++ b/django/contrib/localflavor/sk/sk_regions.py @@ -0,0 +1,16 @@ +""" +Slovak regions according to http://sk.wikipedia.org/wiki/Administrat%C3%ADvne_%C4%8Dlenenie_Slovenska +""" + +from django.utils.translation import ugettext_lazy as _ + +REGION_CHOICES = ( + ('BB', _('Banska Bystrica region')), + ('BA', _('Bratislava region')), + ('KE', _('Kosice region')), + ('NR', _('Nitra region')), + ('PO', _('Presov region')), + ('TN', _('Trencin region')), + ('TT', _('Trnava region')), + ('ZA', _('Zilina region')), +) diff --git a/tests/regressiontests/forms/localflavor.py b/tests/regressiontests/forms/localflavor.py index 8cb096bd201..132a61c30d0 100644 --- a/tests/regressiontests/forms/localflavor.py +++ b/tests/regressiontests/forms/localflavor.py @@ -1363,5 +1363,32 @@ ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'] >>> f.render('foo', 'bar') u'' +# SKPostalCodeField ######################################################### + +>>> from django.contrib.localflavor.sk.forms import SKPostalCodeField +>>> f = SKPostalCodeField() +>>> f.clean('84545x') +Traceback (most recent call last): +... +ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.'] +>>> f.clean('91909') +u'91909' +>>> f.clean('917 01') +u'91701' + +# SKRegionSelect ############################################################ + +>>> from django.contrib.localflavor.sk.forms import SKRegionSelect +>>> w = SKRegionSelect() +>>> w.render('regions', 'TT') +u'' + +# SKDistrictSelect ########################################################## + +>>> from django.contrib.localflavor.sk.forms import SKDistrictSelect +>>> w = SKDistrictSelect() +>>> w.render('Districts', 'RK') +u'' + """