Django

Code

Ticket #6427: localflavor.at-3.diff

File localflavor.at-3.diff, 6.2 kB (added by Horst Gutmann <zerok@zerokspot.com>, 6 months ago)

Updated contrib/localflavor addition to the post-r7971 world

  • /dev/null

    old new  
     1""" 
     2A list of all nine provinces in Austria, see also:  
     3http://en.wikipedia.org/wiki/ISO_3166-2:AT . Vienna is moved to the top,  
     4because it's the capital of Austria 
     5""" 
     6 
     7from django.utils.translation import ugettext_lazy as _ 
     8 
     9# ISO_3166-2 
     10PROVINCE_CHOICES = ( 
     11        ('9', _(u'Vienna')), 
     12        ('1', _(u'Burgenland')), 
     13        ('2', _(u'Carinthia')), 
     14        ('3', _(u'Lower Austria')), 
     15        ('4', _(u'Upper Austria')), 
     16        ('5', _(u'Salzburg')), 
     17        ('6', _(u'Styria')), 
     18        ('7', _(u'Tyrol')), 
     19        ('8', _(u'Vorarlberg')), 
     20) 
  • /dev/null

    old new  
     1""" 
     2AT-specific Form helpers 
     3""" 
     4 
     5from django.forms.fields import RegexField, Select, Field 
     6from django.forms 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         
  • /dev/null

    old new  
     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""" 
  • a/tests/regressiontests/forms/tests.py

    old new  
    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 
     
    3536    'form_tests': form_tests, 
    3637    'custom_error_message_tests': custom_error_message_tests, 
    3738    'localflavor_ar_tests': localflavor_ar_tests, 
     39    'localflavor_at_tests': localflavor_at_tests, 
    3840    'localflavor_au_tests': localflavor_au_tests, 
    3941    'localflavor_br_tests': localflavor_br_tests, 
    4042    'localflavor_ca_tests': localflavor_ca_tests,