Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7686 -- Added an Austrian localflavor. Thanks, bernd.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8087 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a26ba33111a41d87eaea23b4ba2ae48be4e08b18 1 parent 319a314
Malcolm Tredinnick authored July 26, 2008
14  django/contrib/localflavor/at/at_states.py
... ...
@@ -0,0 +1,14 @@
  1
+# -*- coding: utf-8 -*
  2
+from django.utils.translation import ugettext_lazy as _
  3
+
  4
+STATE_CHOICES = (
  5
+    ('BL', _('Burgenland')),
  6
+    ('KA', _('Carinthia')),
  7
+    ('NO', _('Lower Austria')),
  8
+    ('OO', _('Upper Austria')),
  9
+    ('SA', _('Salzburg')),
  10
+    ('ST', _('Styria')),
  11
+    ('TI', _('Tyrol')),
  12
+    ('VO', _('Vorarlberg')),
  13
+    ('WI', _('Vienna')),
  14
+)
29  django/contrib/localflavor/at/forms.py
... ...
@@ -0,0 +1,29 @@
  1
+"""
  2
+AT-specific Form helpers
  3
+"""
  4
+
  5
+import re
  6
+
  7
+from django.utils.translation import ugettext_lazy as _
  8
+from django.forms.fields import Field, RegexField, Select
  9
+
  10
+class ATZipCodeField(RegexField):
  11
+    """
  12
+    A form field that validates its input is an Austrian postcode.
  13
+    
  14
+    Accepts 4 digits.
  15
+    """
  16
+    default_error_messages = {
  17
+        'invalid': _('Enter a zip code in the format XXXX.'),
  18
+    }
  19
+    def __init__(self, *args, **kwargs):
  20
+        super(ATZipCodeField, self).__init__(r'^\d{4}$',
  21
+                max_length=None, min_length=None, *args, **kwargs)
  22
+
  23
+class ATStateSelect(Select):
  24
+    """
  25
+    A Select widget that uses a list of AT states as its choices.
  26
+    """
  27
+    def __init__(self, attrs=None):
  28
+        from at_states import STATE_CHOICES
  29
+        super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
15  docs/localflavor.txt
@@ -32,6 +32,7 @@ Countries currently supported by ``localflavor`` are:
32 32
 
33 33
     * Argentina_
34 34
     * Australia_
  35
+    * Austria_
35 36
     * Brazil_
36 37
     * Canada_
37 38
     * Chile_
@@ -70,6 +71,7 @@ them::
70 71
 .. _ISO 3166 country codes: http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm
71 72
 .. _Argentina: `Argentina (django.contrib.localflavor.ar)`_
72 73
 .. _Australia: `Australia (django.contrib.localflavor.au)`_
  74
+.. _Austria: `Austria (django.contrib.localflavor.at)`_
73 75
 .. _Brazil: `Brazil (django.contrib.localflavor.br)`_
74 76
 .. _Canada: `Canada (django.contrib.localflavor.ca)`_
75 77
 .. _Chile: `Chile (django.contrib.localflavor.cl)`_
@@ -153,6 +155,19 @@ AUStateSelect
153 155
 A ``Select`` widget that uses a list of Australian states/territories as its
154 156
 choices.
155 157
 
  158
+Austria (``django.contrib.localflavor.at``)
  159
+=============================================
  160
+
  161
+ATZipCodeField
  162
+---------------
  163
+
  164
+A form field that validates its input is an Austrian postcode.
  165
+
  166
+ATStateSelect
  167
+-------------
  168
+
  169
+A ``Select`` widget that uses a list of Austrian states as its choices. 
  170
+
156 171
 Brazil (``django.contrib.localflavor.br``)
157 172
 ==========================================
158 173
 
0  localflavor/at/__init__.py b/django/contrib/localflavor/at/__init__.py
No changes.
66  tests/regressiontests/forms/localflavor/at.py
... ...
@@ -0,0 +1,66 @@
  1
+# -*- coding: utf-8 -*-
  2
+# Tests for the contrib/localflavor/ AT form fields.
  3
+
  4
+tests = r"""
  5
+# ATZipCodeField ###########################################################
  6
+
  7
+>>> from django.contrib.localflavor.at.forms import ATZipCodeField 
  8
+>>> f = ATZipCodeField()
  9
+>>> f.clean('1150')
  10
+u'1150'
  11
+>>> f.clean('4020')
  12
+u'4020'
  13
+>>> f.clean('8020')
  14
+u'8020'
  15
+>>> f.clean('111222')
  16
+Traceback (most recent call last):
  17
+...
  18
+ValidationError: [u'Enter a zip code in the format XXXX.']
  19
+>>> f.clean('eeffee')
  20
+Traceback (most recent call last):
  21
+...
  22
+ValidationError: [u'Enter a zip code in the format XXXX.']
  23
+>>> f.clean(u'')
  24
+Traceback (most recent call last):
  25
+...
  26
+ValidationError: [u'This field is required.']
  27
+>>> f.clean(None)
  28
+Traceback (most recent call last):
  29
+...
  30
+ValidationError: [u'This field is required.']
  31
+>>> f.clean('')
  32
+Traceback (most recent call last):
  33
+...
  34
+ValidationError: [u'This field is required.']
  35
+
  36
+
  37
+>>> f = ATZipCodeField(required=False)
  38
+>>> f.clean('1150')
  39
+u'1150'
  40
+>>> f.clean('4020')
  41
+u'4020'
  42
+>>> f.clean('8020')
  43
+u'8020'
  44
+>>> f.clean('111222')
  45
+Traceback (most recent call last):
  46
+...
  47
+ValidationError: [u'Enter a zip code in the format XXXX.']
  48
+>>> f.clean('eeffee')
  49
+Traceback (most recent call last):
  50
+...
  51
+ValidationError: [u'Enter a zip code in the format XXXX.']
  52
+>>> f.clean(None)
  53
+u''
  54
+>>> f.clean('')
  55
+u''
  56
+>>> f.clean(u'')
  57
+u''
  58
+
  59
+# ATStateSelect ##################################################################
  60
+
  61
+>>> from django.contrib.localflavor.at.forms import ATStateSelect
  62
+>>> f = ATStateSelect()
  63
+>>> f.render('bundesland', 'WI')
  64
+u'<select name="bundesland">\n<option value="BL">Burgenland</option>\n<option value="KA">Carinthia</option>\n<option value="NO">Lower Austria</option>\n<option value="OO">Upper Austria</option>\n<option value="SA">Salzburg</option>\n<option value="ST">Styria</option>\n<option value="TI">Tyrol</option>\n<option value="VO">Vorarlberg</option>\n<option value="WI" selected="selected">Vienna</option>\n</select>'
  65
+
  66
+"""
2  tests/regressiontests/forms/tests.py
@@ -4,6 +4,7 @@
4 4
 from forms import tests as form_tests
5 5
 from error_messages import tests as custom_error_message_tests
6 6
 from localflavor.ar import tests as localflavor_ar_tests
  7
+from localflavor.at import tests as localflavor_at_tests
7 8
 from localflavor.au import tests as localflavor_au_tests
8 9
 from localflavor.br import tests as localflavor_br_tests
9 10
 from localflavor.ca import tests as localflavor_ca_tests
@@ -36,6 +37,7 @@
36 37
     'form_tests': form_tests,
37 38
     'custom_error_message_tests': custom_error_message_tests,
38 39
     'localflavor_ar_tests': localflavor_ar_tests,
  40
+    'localflavor_at_tests': localflavor_at_tests,
39 41
     'localflavor_au_tests': localflavor_au_tests,
40 42
     'localflavor_br_tests': localflavor_br_tests,
41 43
     'localflavor_ca_tests': localflavor_ca_tests,

0 notes on commit a26ba33

Please sign in to comment.
Something went wrong with that request. Please try again.