diff --git a/django/contrib/localflavor/at/__init__.py b/django/contrib/localflavor/at/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/localflavor/at/at_states.py b/django/contrib/localflavor/at/at_states.py new file mode 100644 index 0000000000..d4b4d8aa53 --- /dev/null +++ b/django/contrib/localflavor/at/at_states.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -* +from django.utils.translation import ugettext_lazy as _ + +STATE_CHOICES = ( + ('BL', _('Burgenland')), + ('KA', _('Carinthia')), + ('NO', _('Lower Austria')), + ('OO', _('Upper Austria')), + ('SA', _('Salzburg')), + ('ST', _('Styria')), + ('TI', _('Tyrol')), + ('VO', _('Vorarlberg')), + ('WI', _('Vienna')), +) \ No newline at end of file diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py new file mode 100644 index 0000000000..37fe3616d9 --- /dev/null +++ b/django/contrib/localflavor/at/forms.py @@ -0,0 +1,29 @@ +""" +AT-specific Form helpers +""" + +import re + +from django.utils.translation import ugettext_lazy as _ +from django.forms.fields import Field, RegexField, Select + +class ATZipCodeField(RegexField): + """ + A form field that validates its input is an Austrian postcode. + + Accepts 4 digits. + """ + default_error_messages = { + 'invalid': _('Enter a zip code in the format XXXX.'), + } + def __init__(self, *args, **kwargs): + super(ATZipCodeField, self).__init__(r'^\d{4}$', + max_length=None, min_length=None, *args, **kwargs) + +class ATStateSelect(Select): + """ + A Select widget that uses a list of AT states as its choices. + """ + def __init__(self, attrs=None): + from at_states import STATE_CHOICES + super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/docs/localflavor.txt b/docs/localflavor.txt index 74224ab413..25ac4fbb40 100644 --- a/docs/localflavor.txt +++ b/docs/localflavor.txt @@ -32,6 +32,7 @@ Countries currently supported by ``localflavor`` are: * Argentina_ * Australia_ + * Austria_ * Brazil_ * Canada_ * Chile_ @@ -70,6 +71,7 @@ them:: .. _ISO 3166 country codes: http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm .. _Argentina: `Argentina (django.contrib.localflavor.ar)`_ .. _Australia: `Australia (django.contrib.localflavor.au)`_ +.. _Austria: `Austria (django.contrib.localflavor.at)`_ .. _Brazil: `Brazil (django.contrib.localflavor.br)`_ .. _Canada: `Canada (django.contrib.localflavor.ca)`_ .. _Chile: `Chile (django.contrib.localflavor.cl)`_ @@ -153,6 +155,19 @@ AUStateSelect A ``Select`` widget that uses a list of Australian states/territories as its choices. +Austria (``django.contrib.localflavor.at``) +============================================= + +ATZipCodeField +--------------- + +A form field that validates its input is an Austrian postcode. + +ATStateSelect +------------- + +A ``Select`` widget that uses a list of Austrian states as its choices. + Brazil (``django.contrib.localflavor.br``) ========================================== diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py new file mode 100644 index 0000000000..e7229012f0 --- /dev/null +++ b/tests/regressiontests/forms/localflavor/at.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Tests for the contrib/localflavor/ AT form fields. + +tests = r""" +# ATZipCodeField ########################################################### + +>>> from django.contrib.localflavor.at.forms import ATZipCodeField +>>> f = ATZipCodeField() +>>> f.clean('1150') +u'1150' +>>> f.clean('4020') +u'4020' +>>> f.clean('8020') +u'8020' +>>> f.clean('111222') +Traceback (most recent call last): +... +ValidationError: [u'Enter a zip code in the format XXXX.'] +>>> f.clean('eeffee') +Traceback (most recent call last): +... +ValidationError: [u'Enter a zip code in the format XXXX.'] +>>> f.clean(u'') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] +>>> f.clean(None) +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] +>>> f.clean('') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] + + +>>> f = ATZipCodeField(required=False) +>>> f.clean('1150') +u'1150' +>>> f.clean('4020') +u'4020' +>>> f.clean('8020') +u'8020' +>>> f.clean('111222') +Traceback (most recent call last): +... +ValidationError: [u'Enter a zip code in the format XXXX.'] +>>> f.clean('eeffee') +Traceback (most recent call last): +... +ValidationError: [u'Enter a zip code in the format XXXX.'] +>>> f.clean(None) +u'' +>>> f.clean('') +u'' +>>> f.clean(u'') +u'' + +# ATStateSelect ################################################################## + +>>> from django.contrib.localflavor.at.forms import ATStateSelect +>>> f = ATStateSelect() +>>> f.render('bundesland', 'WI') +u'' + +""" diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index f5ab34507d..6a8b017f44 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -4,6 +4,7 @@ from fields import tests as fields_tests from forms import tests as form_tests from error_messages import tests as custom_error_message_tests from localflavor.ar import tests as localflavor_ar_tests +from localflavor.at import tests as localflavor_at_tests from localflavor.au import tests as localflavor_au_tests from localflavor.br import tests as localflavor_br_tests from localflavor.ca import tests as localflavor_ca_tests @@ -36,6 +37,7 @@ __test__ = { 'form_tests': form_tests, 'custom_error_message_tests': custom_error_message_tests, 'localflavor_ar_tests': localflavor_ar_tests, + 'localflavor_at_tests': localflavor_at_tests, 'localflavor_au_tests': localflavor_au_tests, 'localflavor_br_tests': localflavor_br_tests, 'localflavor_ca_tests': localflavor_ca_tests,