diff --git a/AUTHORS b/AUTHORS index f5f47c909a8..673976231a4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -128,6 +128,7 @@ answer newbie questions, and generally made Django that much better: Nicola Larosa Eugene Lazutkin Jeong-Min Lee + Jannis Leidel Christopher Lenz lerouxb@gmail.com Waylan Limberg diff --git a/django/contrib/localflavor/de/__init__.py b/django/contrib/localflavor/de/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/django/contrib/localflavor/de/de_states.py b/django/contrib/localflavor/de/de_states.py new file mode 100644 index 00000000000..a3833788547 --- /dev/null +++ b/django/contrib/localflavor/de/de_states.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -* +from django.utils.translation import gettext_lazy as _ + +STATE_CHOICES = ( + ('BW', _(u'Baden-Württemberg')), + ('BY', _('Bavaria')), + ('BE', _('Berlin')), + ('BB', _('Brandenburg')), + ('HB', _('Bremen')), + ('HH', _('Hamburg')), + ('HE', _('Hessen')), + ('MV', _('Mecklenburg-Western Pomerania')), + ('NI', _('Lower Saxony')), + ('NW', _('North Rhine-Westphalia')), + ('RP', _('Rhineland-Palatinate')), + ('SL', _('Saarland')), + ('SN', _('Saxony')), + ('ST', _('Saxony-Anhalt')), + ('SH', _('Schleswig-Holstein')), + ('TH', _('Thuringia')), +) diff --git a/django/contrib/localflavor/de/forms.py b/django/contrib/localflavor/de/forms.py new file mode 100644 index 00000000000..7e1bee5b4ec --- /dev/null +++ b/django/contrib/localflavor/de/forms.py @@ -0,0 +1,23 @@ +""" +DE-specific Form helpers +""" + +from django.newforms import ValidationError +from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES +from django.utils.translation import gettext +import re + +class DEZipCodeField(RegexField): + def __init__(self, *args, **kwargs): + super(DEZipCodeField, self).__init__(r'^\d{5}$', + max_length=None, min_length=None, + error_message=gettext(u'Enter a zip code in the format XXXXX.'), + *args, **kwargs) + +class DEStateSelect(Select): + """ + A Select widget that uses a list of DE states as its choices. + """ + def __init__(self, attrs=None): + from de_states import STATE_CHOICES # relative import + super(DEStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/tests/regressiontests/forms/localflavor.py b/tests/regressiontests/forms/localflavor.py index a9d2126e383..e30b52e3664 100644 --- a/tests/regressiontests/forms/localflavor.py +++ b/tests/regressiontests/forms/localflavor.py @@ -851,4 +851,22 @@ u'41-3562-3464' >>> w = BRStateSelect() >>> w.render('states', 'PR') u'' + +# DEZipCodeField ############################################################## + +>>> from django.contrib.localflavor.de.forms import DEZipCodeField +>>> f = DEZipCodeField() +>>> f.clean('99423') +u'99423' +>>> f.clean(' 99423') +Traceback (most recent call last): +... +ValidationError: [u'Enter a zip code in the format XXXXX.'] + +# DEStateSelect ############################################################# + +>>> from django.contrib.localflavor.de.forms import DEStateSelect +>>> w = DEStateSelect() +>>> w.render('states', 'TH') +u'' """