From 82a85fdbaec57955c264ef016e6958456d8fdf4e Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 27 Jul 2008 18:54:56 +0000 Subject: [PATCH] Fixed #6427 -- Added some more features to the Austrian localflavor. Thanks, Horst Gutmann. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8113 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/contrib/localflavor/at/forms.py | 40 ++++++++++++++++++- docs/localflavor.txt | 7 +++- tests/regressiontests/forms/localflavor/at.py | 14 +++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index c134c7f2b8..26b2e0540f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -175,6 +175,7 @@ answer newbie questions, and generally made Django that much better: Owen Griffiths Espen Grindhaug Thomas Güttler + Horst Gutmann dAniel hAhler hambaloney Brian Harring diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py index 37fe3616d9..e428fdaa17 100644 --- a/django/contrib/localflavor/at/forms.py +++ b/django/contrib/localflavor/at/forms.py @@ -6,11 +6,14 @@ import re from django.utils.translation import ugettext_lazy as _ from django.forms.fields import Field, RegexField, Select +from django.forms import ValidationError + +re_ssn = re.compile(r'^\d{4} \d{6}') class ATZipCodeField(RegexField): """ A form field that validates its input is an Austrian postcode. - + Accepts 4 digits. """ default_error_messages = { @@ -25,5 +28,38 @@ 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 + from django.contrib.localflavor.at.at_states import STATE_CHOICES super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES) + +class ATSocialSecurityNumberField(Field): + """ + Austrian Social Security numbers are composed of a 4 digits and 6 digits + field. The latter represents in most cases the person's birthdate while + the first 4 digits represent a 3-digits counter and a one-digit checksum. + + The 6-digits field can also differ from the person's birthdate if the + 3-digits counter suffered an overflow. + + This code is based on information available on + http://de.wikipedia.org/wiki/Sozialversicherungsnummer#.C3.96sterreich + """ + + default_error_messages = { + 'invalid': _(u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'), + } + + def clean(self, value): + if not re_ssn.search(value): + raise ValidationError(self.error_messages['invalid']) + sqnr, date = value.split(" ") + sqnr, check = (sqnr[:3], (sqnr[3])) + if int(sqnr) < 100: + raise ValidationError(self.error_messages['invalid']) + res = int(sqnr[0])*3 + int(sqnr[1])*7 + int(sqnr[2])*9 \ + + int(date[0])*5 + int(date[1])*8 + int(date[2])*4 \ + + int(date[3])*2 + int(date[4])*1 + int(date[5])*6 + res = res % 11 + if res != int(check): + raise ValidationError(self.error_messages['invalid']) + return u'%s%s %s'%(sqnr, check, date,) + diff --git a/docs/localflavor.txt b/docs/localflavor.txt index 25ac4fbb40..71e353c5d8 100644 --- a/docs/localflavor.txt +++ b/docs/localflavor.txt @@ -161,13 +161,18 @@ Austria (``django.contrib.localflavor.at``) ATZipCodeField --------------- -A form field that validates its input is an Austrian postcode. +A form field that validates its input as an Austrian zip code. ATStateSelect ------------- A ``Select`` widget that uses a list of Austrian states as its choices. +ATSocialSecurityNumberField +--------------------------- + +A form field that validates its input as an Austrian social security number. + Brazil (``django.contrib.localflavor.br``) ========================================== diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py index e7229012f0..54ca46898e 100644 --- a/tests/regressiontests/forms/localflavor/at.py +++ b/tests/regressiontests/forms/localflavor/at.py @@ -63,4 +63,18 @@ u'' >>> f.render('bundesland', 'WI') u'' +# ATSocialSecurityNumberField ################################################ + +>>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField +>>> f = ATSocialSecurityNumberField() +>>> f.clean('1237 010180') +u'1237 010180' +>>> f.clean('1237 010181') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] +>>> f.clean('12370 010180') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] """