Ticket #6427: localflavor.at-4.diff

File localflavor.at-4.diff, 4.1 KB (added by Horst Gutmann <zerok@…>, 16 years ago)

Updated patch for the post-r8087 world

  • 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  
    66
    77from django.utils.translation import ugettext_lazy as _
    88from django.forms.fields import Field, RegexField, Select
     9from django.forms import ValidationError
     10
     11re_ssn = re.compile(r'^\d{4} \d{6}')
    912
    1013class ATZipCodeField(RegexField):
    1114    """
    class ATStateSelect(Select):  
    2528    A Select widget that uses a list of AT states as its choices.
    2629    """
    2730    def __init__(self, attrs=None):
    28         from at_states import STATE_CHOICES
     31        from django.contrib.localflavor.at.at_states import STATE_CHOICES
    2932        super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
     33
     34class 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``)  
    161161ATZipCodeField
    162162---------------
    163163
    164 A form field that validates its input is an Austrian postcode.
     164A form field that validates its input as an Austrian zip code.
    165165
    166166ATStateSelect
    167167-------------
    168168
    169169A ``Select`` widget that uses a list of Austrian states as its choices.
    170170
     171ATSocialSecurityNumberField
     172---------------------------
     173
     174A form field that validates its input as an Austrian social security number.
     175
    171176Brazil (``django.contrib.localflavor.br``)
    172177==========================================
    173178
  • 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''  
    6363>>> f.render('bundesland', 'WI')
    6464u'<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>'
    6565
     66# ATSocialSecurityNumberField ################################################
     67
     68>>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField
     69>>> f = ATSocialSecurityNumberField()
     70>>> f.clean('1237 010180')
     71u'1237 010180'
     72>>> f.clean('1237 010181')
     73Traceback (most recent call last):
     74...
     75ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
     76>>> f.clean('12370 010180')
     77Traceback (most recent call last):
     78...
     79ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
    6680"""
Back to Top