Ticket #6427: localflavor.at-4.diff
File localflavor.at-4.diff, 4.1 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/at/forms.py
diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py index 37fe361..61e07be 100644
a b import re 6 6 7 7 from django.utils.translation import ugettext_lazy as _ 8 8 from django.forms.fields import Field, RegexField, Select 9 from django.forms import ValidationError 10 11 re_ssn = re.compile(r'^\d{4} \d{6}') 9 12 10 13 class ATZipCodeField(RegexField): 11 14 """ … … class ATStateSelect(Select): 25 28 A Select widget that uses a list of AT states as its choices. 26 29 """ 27 30 def __init__(self, attrs=None): 28 from at_states import STATE_CHOICES31 from django.contrib.localflavor.at.at_states import STATE_CHOICES 29 32 super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES) 33 34 class ATSocialSecurityNumberField(Field): 35 """ 36 Austrian Social Security numbers are composed of a 4 digits and 6 digits 37 field. The latter represents in most cases the person's birthdate while 38 the first 4 digits represent a 3-digits counter and a one-digit checksum. 39 40 The 6-digits field can also differ from the person's birthdate if the 41 3-digits counter suffered an overflow. 42 43 This code is based on information available on 44 http://de.wikipedia.org/wiki/Sozialversicherungsnummer#.C3.96sterreich 45 """ 46 47 default_error_messages = { 48 'invalid': _(u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'), 49 } 50 51 def clean(self, value): 52 if not re_ssn.search(value): 53 raise ValidationError(self.error_messages['invalid']) 54 sqnr, date = value.split(" ") 55 sqnr, check = (sqnr[:3], (sqnr[3])) 56 if int(sqnr) < 100: 57 raise ValidationError(self.error_messages['invalid']) 58 res = int(sqnr[0])*3 + int(sqnr[1])*7 + int(sqnr[2])*9 \ 59 + int(date[0])*5 + int(date[1])*8 + int(date[2])*4 \ 60 + int(date[3])*2 + int(date[4])*1 + int(date[5])*6 61 res = res % 11 62 if res != int(check): 63 raise ValidationError(self.error_messages['invalid']) 64 return u'%s%s %s'%(sqnr, check, date,) 65 -
docs/localflavor.txt
diff --git a/docs/localflavor.txt b/docs/localflavor.txt index 25ac4fb..71e353c 100644
a b Austria (``django.contrib.localflavor.at``) 161 161 ATZipCodeField 162 162 --------------- 163 163 164 A form field that validates its input is an Austrian postcode.164 A form field that validates its input as an Austrian zip code. 165 165 166 166 ATStateSelect 167 167 ------------- 168 168 169 169 A ``Select`` widget that uses a list of Austrian states as its choices. 170 170 171 ATSocialSecurityNumberField 172 --------------------------- 173 174 A form field that validates its input as an Austrian social security number. 175 171 176 Brazil (``django.contrib.localflavor.br``) 172 177 ========================================== 173 178 -
tests/regressiontests/forms/localflavor/at.py
diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py index e722901..54ca468 100644
a b u'' 63 63 >>> f.render('bundesland', 'WI') 64 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 65 66 # ATSocialSecurityNumberField ################################################ 67 68 >>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField 69 >>> f = ATSocialSecurityNumberField() 70 >>> f.clean('1237 010180') 71 u'1237 010180' 72 >>> f.clean('1237 010181') 73 Traceback (most recent call last): 74 ... 75 ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] 76 >>> f.clean('12370 010180') 77 Traceback (most recent call last): 78 ... 79 ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] 66 80 """