Ticket #3866: more_it_fields_2.diff

File more_it_fields_2.diff, 7.3 KB (added by Massimiliano Ravelli <massimiliano.ravelli@…>, 17 years ago)

Little improvement to ITFiscalCodeField

  • django/contrib/localflavor/it/util.py

     
     1from django.utils.translation import gettext
     2
     3FISCAL_CODE_EVEN_CHARS = {
     4    '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
     5    'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9,
     6    'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18,
     7    'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25
     8}
     9FISCAL_CODE_ODD_CHARS = {
     10    '0': 1, '1': 0, '2': 5, '3': 7, '4': 9, '5': 13, '6': 15, '7': 17, '8': 19, '9': 21,
     11    'A': 1, 'B': 0, 'C': 5, 'D': 7, 'E': 9, 'F': 13, 'G': 15, 'H': 17, 'I': 19, 'J': 21,
     12    'K': 2, 'L': 4, 'M': 18, 'N': 20, 'O': 11, 'P': 3, 'Q': 6, 'R': 8, 'S': 12,
     13    'T': 14, 'U': 16, 'V': 10, 'W': 22, 'X': 25, 'Y': 24, 'Z': 23
     14}
     15# Chars from 'A' to 'Z'
     16FISCAL_CODE_CHECK_DIGITS = [chr(x) for x in range(65, 91)]
     17
     18
     19def fiscal_code_check_digit(fiscal_code):
     20    """ Calculate italian fiscal code check digit. """
     21    code = fiscal_code.upper()
     22    total = 0
     23    for i in range(0,15):
     24        try:
     25            if i % 2 == 0:
     26                total += FISCAL_CODE_ODD_CHARS[code[i]]
     27            else:
     28                total += FISCAL_CODE_EVEN_CHARS[code[i]]
     29        except KeyError:
     30            msg = gettext(u" Character '%(char)s' is not allowed.") % {'char': code[i]}
     31            raise ValueError(msg)
     32    return FISCAL_CODE_CHECK_DIGITS[total % 26]
     33
     34def vat_number_check_digit(vat_number):
     35    """ Calculate italian vat number check digit. """
     36    normalized_vat_number = str(vat_number).zfill(10)
     37    total = 0
     38    for i in range(0, 10, 2):
     39        total += int(normalized_vat_number[i])
     40    for i in range(1, 11, 2):
     41        quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10)
     42        total += quotient + remainder
     43    return str((10 - total % 10) % 10)
  • django/contrib/localflavor/it/forms.py

     
    55from django.newforms import ValidationError
    66from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
    77from django.utils.translation import gettext
     8from django.utils.encoding import smart_unicode
     9from django.contrib.localflavor.it.util import fiscal_code_check_digit, vat_number_check_digit
    810import re
    911
     12fc_re = re.compile(r"^(?P<area>\d{3})[-\ ]?(?P<group>\d{2})[-\ ]?(?P<serial>\d{4})$")
    1013class ITZipCodeField(RegexField):
    1114    def __init__(self, *args, **kwargs):
    1215        super(ITZipCodeField, self).__init__(r'^\d{5}$',
     
    2932    def __init__(self, attrs=None):
    3033        from it_province import PROVINCE_CHOICES # relative import
    3134        super(ITProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
     35
     36class ITFiscalCodeField(RegexField):
     37    """
     38    A form field  that validates it's input in Italian fiscal code.
     39    For reference see http://www.agenziaentrate.it/ .
     40    """
     41    err_msg = gettext(u'Enter a valid fiscal code.')
     42    def __init__(self, *args, **kwargs):
     43        super(ITFiscalCodeField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$',
     44        max_length=None, min_length=None, error_message=self.err_msg,
     45        *args, **kwargs)
     46
     47    def clean(self, value):
     48        value = super(ITFiscalCodeField, self).clean(value)
     49        if value == u'':
     50            return value
     51        value = re.sub('\s', u'', value).upper()
     52        try:
     53            check_digit = fiscal_code_check_digit(value)
     54        except ValueError:
     55            raise ValidationError(self.err_msg)
     56        if not value[15] == check_digit:
     57            raise ValidationError(self.err_msg)
     58        return value
     59
     60class ITVatNumberField(Field):
     61    """
     62    A form field  that validates it's input in Italian VAT number.
     63    """
     64    def clean(self, value):
     65        value = super(ITVatNumberField, self).clean(value)
     66        if value == u'':
     67            return value
     68        err_msg = gettext(u'Enter a valid VAT number.')
     69        try:
     70            vat_number = int(value)
     71        except ValueError:
     72            raise ValidationError(err_msg)
     73        vat_number = str(vat_number).zfill(11)
     74        check_digit = vat_number_check_digit(vat_number[0:10])
     75        if not vat_number[10] == check_digit:
     76            raise ValidationError(err_msg)
     77        return smart_unicode(vat_number)
  • tests/regressiontests/forms/localflavor.py

     
    642642>>> w.render('regions', 'PMN')
    643643u'<select name="regions">\n<option value="ABR">Abruzzo</option>\n<option value="BAS">Basilicata</option>\n<option value="CAL">Calabria</option>\n<option value="CAM">Campania</option>\n<option value="EMR">Emilia-Romagna</option>\n<option value="FVG">Friuli-Venezia Giulia</option>\n<option value="LAZ">Lazio</option>\n<option value="LIG">Liguria</option>\n<option value="LOM">Lombardia</option>\n<option value="MAR">Marche</option>\n<option value="MOL">Molise</option>\n<option value="PMN" selected="selected">Piemonte</option>\n<option value="PUG">Puglia</option>\n<option value="SAR">Sardegna</option>\n<option value="SIC">Sicilia</option>\n<option value="TOS">Toscana</option>\n<option value="TAA">Trentino-Alto Adige</option>\n<option value="UMB">Umbria</option>\n<option value="VAO">Valle d\u2019Aosta</option>\n<option value="VEN">Veneto</option>\n</select>'
    644644
     645# ITFiscalCodeField ##########################################################
     646
     647>>> from django.contrib.localflavor.it.forms import ITFiscalCodeField
     648>>> f = ITFiscalCodeField()
     649>>> f.clean('RVLMSM71L12E897Q')
     650u'RVLMSM71L12E897Q'
     651>>> f.clean('rvlmsm71l12e897q')
     652u'RVLMSM71L12E897Q'
     653>>> f.clean('RVL MSM 71L12 E897Q')
     654u'RVLMSM71L12E897Q'
     655>>> f.clean('RVLMSM71L12E897A')
     656Traceback (most recent call last):
     657...
     658ValidationError: [u'Enter a valid fiscal code.']
     659>>> f.clean('%VLMSM71L12E897Q')
     660Traceback (most recent call last):
     661...
     662ValidationError: [u'Enter a valid fiscal code.']
     663
     664# ITVatNumberField ###########################################################
     665
     666>>> from django.contrib.localflavor.it.forms import ITVatNumberField
     667>>> f = ITVatNumberField()
     668>>> f.clean('07973780013')
     669u'07973780013'
     670>>> f.clean('7973780013')
     671u'07973780013'
     672>>> f.clean(7973780013)
     673u'07973780013'
     674>>> f.clean('07973780014')
     675Traceback (most recent call last):
     676...
     677ValidationError: [u'Enter a valid VAT number.']
     678>>> f.clean('A7973780013')
     679Traceback (most recent call last):
     680...
     681ValidationError: [u'Enter a valid VAT number.']
     682
    645683# FIZipCodeField #############################################################
    646684
    647685FIZipCodeField validates that the data is a valid FI zipcode.
  • AUTHORS

     
    174174    J. Rademaker
    175175    Michael Radziej <mir@noris.de>
    176176    ramiro
     177    Massimiliano Ravelli <massimiliano.ravelli@gmail.com>
    177178    Brian Ray <http://brianray.chipy.org/>
    178179    remco@diji.biz
    179180    rhettg@gmail.com
Back to Top