Ticket #6427: localflavor.at-2.diff

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

Some final changes to the documentation to clarify the origin of the whole SSN checker

  • new file django/contrib/localflavor/at/at_provinces.py

    diff --git a/django/contrib/localflavor/at/__init__.py b/django/contrib/localflavor/at/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/localflavor/at/at_provinces.py b/django/contrib/localflavor/at/at_provinces.py
    new file mode 100644
    index 0000000..da51b0a
    - +  
     1# -*- coding: utf-8 -*-
     2"""
     3A list of all nine provinces in Austria, see also:
     4http://en.wikipedia.org/wiki/ISO_3166-2:AT . Vienna is moved to the top,
     5because it's the capital of Austria
     6"""
     7
     8from django.utils.translation import ugettext_lazy as _
     9
     10# ISO_3166-2
     11PROVINCE_CHOICES = (
     12        ('9', _(u'Vienna')),
     13        ('1', _(u'Burgenland')),
     14        ('2', _(u'Carinthia')),
     15        ('3', _(u'Lower Austria')),
     16        ('4', _(u'Upper Austria')),
     17        ('5', _(u'Salzburg')),
     18        ('6', _(u'Styria')),
     19        ('7', _(u'Tyrol')),
     20        ('8', _(u'Vorarlberg')),
     21)
     22 No newline at end of file
  • new file django/contrib/localflavor/at/forms.py

    diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py
    new file mode 100644
    index 0000000..9a2f5f5
    - +  
     1"""
     2AT-specific Form helpers
     3"""
     4
     5from django.newforms.fields import RegexField, Select, Field
     6from django.newforms import ValidationError
     7from django.utils.translation import ugettext
     8import re
     9
     10re_ssn = re.compile(r'^\d{4} \d{6}')
     11
     12class ATZipCodeField(RegexField):
     13    """
     14    A simple input field that validates 4-digit zip codes as used in Austria.
     15    """
     16   
     17    default_error_messages = {
     18        'invalid': ugettext('Enter a zip code in the format XXXX.'),
     19    }
     20
     21    def __init__(self, *args, **kwargs):
     22        super(ATZipCodeField, self).__init__(r'^[1-9]\d{3}$',
     23            max_length=None, min_length=None, *args, **kwargs)
     24
     25class ATProvinceSelect(Select):
     26    """
     27    A simple select box for all nine provinces of Austria.
     28    """
     29   
     30    def __init__(self, attrs=None):
     31        from django.contrib.localflavor.at.at_provinces import PROVINCE_CHOICES
     32        super(ATProvinceSelect, self).__init__(attrs, choices=PROVINCE_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': ugettext(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       
  • new file tests/regressiontests/forms/localflavor/at.py

    diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py
    new file mode 100644
    index 0000000..b230329
    - +  
     1# -*- coding: utf-8 -*-
     2# Tests for contrib/localflavor/at form fields
     3
     4tests = r"""
     5# ATZipCodeField ############################################################
     6
     7>>> from django.contrib.localflavor.at.forms import ATZipCodeField
     8>>> f = ATZipCodeField()
     9>>> f.clean('9073')
     10u'9073'
     11>>> f.clean('90730')
     12Traceback (most recent call last):
     13...
     14ValidationError: [u'Enter a zip code in the format XXXX.']
     15>>> f.clean('0073')
     16Traceback (most recent call last):
     17...
     18ValidationError: [u'Enter a zip code in the format XXXX.']
     19
     20# ATProvinceSelect ##########################################################
     21
     22>>> from django.contrib.localflavor.at.forms import ATProvinceSelect
     23>>> w = ATProvinceSelect()
     24>>> w.render('provinces', '2')
     25u'<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>'
     26
     27# ATSocialSecurityNumberField ###############################################
     28
     29>>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField
     30>>> f = ATSocialSecurityNumberField()
     31>>> f.clean('1237 010180')
     32u'1237 010180'
     33>>> f.clean('1237 010181')
     34Traceback (most recent call last):
     35...
     36ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
     37"""
  • tests/regressiontests/forms/tests.py

    diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
    index bb0e30b..0891c1b 100644
    a b from fields import tests as fields_tests 
    44from forms import tests as form_tests
    55from error_messages import tests as custom_error_message_tests
    66from localflavor.ar import tests as localflavor_ar_tests
     7from localflavor.at import tests as localflavor_at_tests
    78from localflavor.au import tests as localflavor_au_tests
    89from localflavor.br import tests as localflavor_br_tests
    910from localflavor.ca import tests as localflavor_ca_tests
    __test__ = { 
    3334    'form_tests': form_tests,
    3435    'custom_error_message_tests': custom_error_message_tests,
    3536    'localflavor_ar_tests': localflavor_ar_tests,
     37    'localflavor_at_tests': localflavor_at_tests,
    3638    'localflavor_au_tests': localflavor_au_tests,
    3739    'localflavor_br_tests': localflavor_br_tests,
    3840    'localflavor_ca_tests': localflavor_ca_tests,
Back to Top