Code

Ticket #3866: more_it_fields.diff

File more_it_fields.diff, 7.1 KB (added by Massimiliano Ravelli <massimiliano.ravelli@…>, 7 years ago)

Patch for ITFiscalCodeField and ITVatNumberField with regression tests

Line 
1Index: django/contrib/localflavor/it/util.py
2===================================================================
3--- django/contrib/localflavor/it/util.py       (revision 0)
4+++ django/contrib/localflavor/it/util.py       (revision 0)
5@@ -0,0 +1,43 @@
6+from django.utils.translation import gettext
7+
8+FISCAL_CODE_EVEN_CHARS = {
9+    '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
10+    'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9,
11+    'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18,
12+    'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25
13+}
14+FISCAL_CODE_ODD_CHARS = {
15+    '0': 1, '1': 0, '2': 5, '3': 7, '4': 9, '5': 13, '6': 15, '7': 17, '8': 19, '9': 21,
16+    'A': 1, 'B': 0, 'C': 5, 'D': 7, 'E': 9, 'F': 13, 'G': 15, 'H': 17, 'I': 19, 'J': 21,
17+    'K': 2, 'L': 4, 'M': 18, 'N': 20, 'O': 11, 'P': 3, 'Q': 6, 'R': 8, 'S': 12,
18+    'T': 14, 'U': 16, 'V': 10, 'W': 22, 'X': 25, 'Y': 24, 'Z': 23
19+}
20+# Chars from 'A' to 'Z'
21+FISCAL_CODE_CHECK_DIGITS = [chr(x) for x in range(65, 91)]
22+
23+
24+def fiscal_code_check_digit(fiscal_code):
25+    """ Calculate italian fiscal code check digit. """
26+    code = fiscal_code.upper()
27+    total = 0
28+    for i in range(0,15):
29+        try:
30+            if i % 2 == 0:
31+                total += FISCAL_CODE_ODD_CHARS[code[i]]
32+            else:
33+                total += FISCAL_CODE_EVEN_CHARS[code[i]]
34+        except KeyError:
35+            msg = gettext(u" Character '%(char)s' is not allowed.") % {'char': code[i]}
36+            raise ValueError(msg)
37+    return FISCAL_CODE_CHECK_DIGITS[total % 26]
38+
39+def vat_number_check_digit(vat_number):
40+    """ Calculate italian vat number check digit. """
41+    normalized_vat_number = str(vat_number).zfill(10)
42+    total = 0
43+    for i in range(0, 10, 2):
44+        total += int(normalized_vat_number[i])
45+    for i in range(1, 11, 2):
46+        quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10)
47+        total += quotient + remainder
48+    return str((10 - total % 10) % 10)
49Index: django/contrib/localflavor/it/forms.py
50===================================================================
51--- django/contrib/localflavor/it/forms.py      (revision 4992)
52+++ django/contrib/localflavor/it/forms.py      (working copy)
53@@ -5,6 +5,8 @@
54 from django.newforms import ValidationError
55 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
56 from django.utils.translation import gettext
57+from django.utils.encoding import smart_unicode
58+from django.contrib.localflavor.it.util import fiscal_code_check_digit, vat_number_check_digit
59 import re
60 
61 class ITZipCodeField(RegexField):
62@@ -29,3 +31,44 @@
63     def __init__(self, attrs=None):
64         from it_province import PROVINCE_CHOICES # relative import
65         super(ITProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
66+
67+class ITFiscalCodeField(Field):
68+    """
69+    A form field  that validates it's input in Italian fiscal code.
70+    """
71+    def clean(self, value):
72+        super(ITFiscalCodeField, self).clean(value)
73+        if value in EMPTY_VALUES:
74+            return u''
75+        err_msg = gettext(u'Enter a valid fiscal code.')
76+        fiscal_code = value.upper()
77+        fiscal_code = fiscal_code.replace(' ', '')
78+        fiscal_code = fiscal_code.replace('-', '')
79+        if not len(fiscal_code) == 16:
80+            raise ValidationError(err_msg)
81+        try:
82+            check_digit = fiscal_code_check_digit(fiscal_code)
83+        except ValueError:
84+            raise ValidationError(err_msg)
85+        if not fiscal_code[15] == check_digit:
86+            raise ValidationError(err_msg)
87+        return smart_unicode(fiscal_code)
88+
89+class ITVatNumberField(Field):
90+    """
91+    A form field  that validates it's input in Italian VAT number.
92+    """
93+    def clean(self, value):
94+        super(ITVatNumberField, self).clean(value)
95+        if value in EMPTY_VALUES:
96+            return u''
97+        err_msg = gettext(u'Enter a valid VAT number.')
98+        try:
99+            vat_number = int(value)
100+        except ValueError:
101+            raise ValidationError(err_msg)
102+        vat_number = str(vat_number).zfill(11)
103+        check_digit = vat_number_check_digit(vat_number[0:10])
104+        if not vat_number[10] == check_digit:
105+            raise ValidationError(err_msg)
106+        return smart_unicode(vat_number)
107Index: tests/regressiontests/forms/localflavor.py
108===================================================================
109--- tests/regressiontests/forms/localflavor.py  (revision 4992)
110+++ tests/regressiontests/forms/localflavor.py  (working copy)
111@@ -642,6 +642,46 @@
112 >>> w.render('regions', 'PMN')
113 u'<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>'
114 
115+# ITFiscalCodeField ##########################################################
116+
117+>>> from django.contrib.localflavor.it.forms import ITFiscalCodeField
118+>>> f = ITFiscalCodeField()
119+>>> f.clean('RVLMSM71L12E897Q')
120+u'RVLMSM71L12E897Q'
121+>>> f.clean('rvlmsm71l12e897q')
122+u'RVLMSM71L12E897Q'
123+>>> f.clean('RVL MSM 71L12 E897Q')
124+u'RVLMSM71L12E897Q'
125+>>> f.clean('RVL-MSM-71L12-E897Q')
126+u'RVLMSM71L12E897Q'
127+>>> f.clean('RVLMSM71L12E897A')
128+Traceback (most recent call last):
129+...
130+ValidationError: [u'Enter a valid fiscal code.']
131+>>> f.clean('%VLMSM71L12E897Q')
132+Traceback (most recent call last):
133+...
134+ValidationError: [u'Enter a valid fiscal code.']
135+
136+# ITVatNumberField ###########################################################
137+
138+>>> from django.contrib.localflavor.it.forms import ITVatNumberField
139+>>> f = ITVatNumberField()
140+>>> f.clean('07973780013')
141+u'07973780013'
142+>>> f.clean('7973780013')
143+u'07973780013'
144+>>> f.clean(7973780013)
145+u'07973780013'
146+>>> f.clean('07973780014')
147+Traceback (most recent call last):
148+...
149+ValidationError: [u'Enter a valid VAT number.']
150+>>> f.clean('A7973780013')
151+Traceback (most recent call last):
152+...
153+ValidationError: [u'Enter a valid VAT number.']
154+
155 # FIZipCodeField #############################################################
156 
157 FIZipCodeField validates that the data is a valid FI zipcode.
158Index: AUTHORS
159===================================================================
160--- AUTHORS     (revision 4992)
161+++ AUTHORS     (working copy)
162@@ -174,6 +174,7 @@
163     J. Rademaker
164     Michael Radziej <mir@noris.de>
165     ramiro
166+    Massimiliano Ravelli <massimiliano.ravelli@gmail.com>
167     Brian Ray <http://brianray.chipy.org/>
168     remco@diji.biz
169     rhettg@gmail.com