Code

Ticket #6427: localflavor.at.diff

File localflavor.at.diff, 6.2 KB (added by Horst Gutmann <zerok@…>, 6 years ago)

New localflavor.at

Line 
1diff --git a/django/contrib/localflavor/at/__init__.py b/django/contrib/localflavor/at/__init__.py
2new file mode 100644
3index 0000000..e69de29
4diff --git a/django/contrib/localflavor/at/at_provinces.py b/django/contrib/localflavor/at/at_provinces.py
5new file mode 100644
6index 0000000..da51b0a
7--- /dev/null
8+++ b/django/contrib/localflavor/at/at_provinces.py
9@@ -0,0 +1,21 @@
10+# -*- coding: utf-8 -*-
11+"""
12+A list of all nine provinces in Austria, see also:
13+http://en.wikipedia.org/wiki/ISO_3166-2:AT . Vienna is moved to the top,
14+because it's the capital of Austria
15+"""
16+
17+from django.utils.translation import ugettext_lazy as _
18+
19+# ISO_3166-2
20+PROVINCE_CHOICES = (
21+       ('9', _(u'Vienna')),
22+       ('1', _(u'Burgenland')),
23+       ('2', _(u'Carinthia')),
24+       ('3', _(u'Lower Austria')),
25+       ('4', _(u'Upper Austria')),
26+       ('5', _(u'Salzburg')),
27+       ('6', _(u'Styria')),
28+       ('7', _(u'Tyrol')),
29+       ('8', _(u'Vorarlberg')),
30+)
31\ No newline at end of file
32diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py
33new file mode 100644
34index 0000000..911ccfc
35--- /dev/null
36+++ b/django/contrib/localflavor/at/forms.py
37@@ -0,0 +1,64 @@
38+"""
39+AT-specific Form helpers
40+"""
41+
42+from django.newforms.fields import RegexField, Select, Field
43+from django.newforms import ValidationError
44+from django.utils.translation import ugettext
45+import re
46+
47+re_ssn = re.compile(r'^\d{4} \d{6}')
48+
49+class ATZipCodeField(RegexField):
50+    """
51+    A simple input field that validates 4-digit zip codes as used in Austria.
52+    """
53+   
54+    default_error_messages = {
55+        'invalid': ugettext('Enter a zip code in the format XXXX.'),
56+    }
57+
58+    def __init__(self, *args, **kwargs):
59+        super(ATZipCodeField, self).__init__(r'^[1-9]\d{3}$',
60+            max_length=None, min_length=None, *args, **kwargs)
61+
62+class ATProvinceSelect(Select):
63+    """
64+    A simple select box for all nine provinces of Austria.
65+    """
66+   
67+    def __init__(self, attrs=None):
68+        from django.contrib.localflavor.at.at_provinces import PROVINCE_CHOICES
69+        super(ATProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
70+
71+class ATSocialSecurityNumberField(Field):
72+    """
73+    Austrian Social Security numbers are composed of a 4 digits and 6 digits
74+    field. The latter represents in most cases the person's birthdate while
75+    the first 4 digits represent a 3-digits-counter and a one-digit checksum.
76+   
77+    The 6-digits-field can also differ from the person's birthdate if the
78+    3-digits counter suffered an overflow.
79+   
80+    For more details visit http://de.wikipedia.org/wiki/Sozialversicherungsnummer#.C3.96sterreich
81+    """
82+   
83+    default_error_messages = {
84+        'invalid': ugettext(u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'),
85+    }
86+   
87+    def clean(self, value):
88+        if not re_ssn.search(value):
89+            raise ValidationError(self.error_messages['invalid'])
90+        sqnr, date = value.split(" ")
91+        sqnr, check = (sqnr[:3], (sqnr[3]))
92+        if int(sqnr) < 100:
93+           raise ValidationError(self.error_messages['invalid'])
94+        res = int(sqnr[0])*3 + int(sqnr[1])*7 + int(sqnr[2])*9 \
95+           + int(date[0])*5 + int(date[1])*8 + int(date[2])*4 \
96+           + int(date[3])*2 + int(date[4])*1 + int(date[5])*6
97+        res = res % 11
98+        if res != int(check):
99+           raise ValidationError(self.error_messages['invalid'])
100+        return u'%s%s %s'%(sqnr, check, date,)
101+       
102diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py
103new file mode 100644
104index 0000000..b230329
105--- /dev/null
106+++ b/tests/regressiontests/forms/localflavor/at.py
107@@ -0,0 +1,37 @@
108+# -*- coding: utf-8 -*-
109+# Tests for contrib/localflavor/at form fields
110+
111+tests = r"""
112+# ATZipCodeField ############################################################
113+
114+>>> from django.contrib.localflavor.at.forms import ATZipCodeField
115+>>> f = ATZipCodeField()
116+>>> f.clean('9073')
117+u'9073'
118+>>> f.clean('90730')
119+Traceback (most recent call last):
120+...
121+ValidationError: [u'Enter a zip code in the format XXXX.']
122+>>> f.clean('0073')
123+Traceback (most recent call last):
124+...
125+ValidationError: [u'Enter a zip code in the format XXXX.']
126+
127+# ATProvinceSelect ##########################################################
128+
129+>>> from django.contrib.localflavor.at.forms import ATProvinceSelect
130+>>> w = ATProvinceSelect()
131+>>> w.render('provinces', '2')
132+u'<select name="provinces">\n<option value="9">Vienna</option>\n<option value="1">Burgenland</option>\n<option value="2" selected="selected">Carinthia</option>\n<option value="3">Lower Austria</option>\n<option value="4">Upper Austria</option>\n<option value="5">Salzburg</option>\n<option value="6">Styria</option>\n<option value="7">Tyrol</option>\n<option value="8">Vorarlberg</option>\n</select>'
133+
134+# ATSocialSecurityNumberField ###############################################
135+
136+>>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField
137+>>> f = ATSocialSecurityNumberField()
138+>>> f.clean('1237 010180')
139+u'1237 010180'
140+>>> f.clean('1237 010181')
141+Traceback (most recent call last):
142+...
143+ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
144+"""
145diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
146index bb0e30b..0891c1b 100644
147--- a/tests/regressiontests/forms/tests.py
148+++ b/tests/regressiontests/forms/tests.py
149@@ -4,6 +4,7 @@ from fields import tests as fields_tests
150 from forms import tests as form_tests
151 from error_messages import tests as custom_error_message_tests
152 from localflavor.ar import tests as localflavor_ar_tests
153+from localflavor.at import tests as localflavor_at_tests
154 from localflavor.au import tests as localflavor_au_tests
155 from localflavor.br import tests as localflavor_br_tests
156 from localflavor.ca import tests as localflavor_ca_tests
157@@ -33,6 +34,7 @@ __test__ = {
158     'form_tests': form_tests,
159     'custom_error_message_tests': custom_error_message_tests,
160     'localflavor_ar_tests': localflavor_ar_tests,
161+    'localflavor_at_tests': localflavor_at_tests,
162     'localflavor_au_tests': localflavor_au_tests,
163     'localflavor_br_tests': localflavor_br_tests,
164     'localflavor_ca_tests': localflavor_ca_tests,