From 7a0441b712609999b1d45884d8113c8f98c956ab Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 4 Apr 2007 06:45:29 +0000 Subject: [PATCH] Fixed #3897 -- Added German localflavor. Thanks, Jannis Leidel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4920 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/contrib/localflavor/de/__init__.py | 0 django/contrib/localflavor/de/de_states.py | 21 ++++++++++++++++++++ django/contrib/localflavor/de/forms.py | 23 ++++++++++++++++++++++ tests/regressiontests/forms/localflavor.py | 18 +++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 django/contrib/localflavor/de/__init__.py create mode 100644 django/contrib/localflavor/de/de_states.py create mode 100644 django/contrib/localflavor/de/forms.py diff --git a/AUTHORS b/AUTHORS index f5f47c909a..673976231a 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 0000000000..e69de29bb2 diff --git a/django/contrib/localflavor/de/de_states.py b/django/contrib/localflavor/de/de_states.py new file mode 100644 index 0000000000..a383378854 --- /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 0000000000..7e1bee5b4e --- /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 a9d2126e38..e30b52e366 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'' """