|
0
|
1 |
""" |
|
|
2 |
IT-specific Form helpers |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
from django.forms import ValidationError |
|
|
6 |
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES |
|
|
7 |
from django.utils.translation import ugettext_lazy as _ |
|
|
8 |
from django.utils.encoding import smart_unicode |
|
|
9 |
from django.contrib.localflavor.it.util import ssn_check_digit, vat_number_check_digit |
|
|
10 |
import re |
|
|
11 |
|
|
|
12 |
class ITZipCodeField(RegexField): |
|
|
13 |
default_error_messages = { |
|
|
14 |
'invalid': _('Enter a valid zip code.'), |
|
|
15 |
} |
|
|
16 |
def __init__(self, *args, **kwargs): |
|
|
17 |
super(ITZipCodeField, self).__init__(r'^\d{5}$', |
|
|
18 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
19 |
|
|
|
20 |
class ITRegionSelect(Select): |
|
|
21 |
""" |
|
|
22 |
A Select widget that uses a list of IT regions as its choices. |
|
|
23 |
""" |
|
|
24 |
def __init__(self, attrs=None): |
|
|
25 |
from it_region import REGION_CHOICES |
|
|
26 |
super(ITRegionSelect, self).__init__(attrs, choices=REGION_CHOICES) |
|
|
27 |
|
|
|
28 |
class ITProvinceSelect(Select): |
|
|
29 |
""" |
|
|
30 |
A Select widget that uses a list of IT provinces as its choices. |
|
|
31 |
""" |
|
|
32 |
def __init__(self, attrs=None): |
|
|
33 |
from it_province import PROVINCE_CHOICES |
|
|
34 |
super(ITProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES) |
|
|
35 |
|
|
|
36 |
class ITSocialSecurityNumberField(RegexField): |
|
|
37 |
""" |
|
|
38 |
A form field that validates Italian Social Security numbers (codice fiscale). |
|
|
39 |
For reference see http://www.agenziaentrate.it/ and search for |
|
|
40 |
'Informazioni sulla codificazione delle persone fisiche'. |
|
|
41 |
""" |
|
|
42 |
default_error_messages = { |
|
|
43 |
'invalid': _(u'Enter a valid Social Security number.'), |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
def __init__(self, *args, **kwargs): |
|
|
47 |
super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$', |
|
|
48 |
max_length=None, min_length=None, *args, **kwargs) |
|
|
49 |
|
|
|
50 |
def clean(self, value): |
|
|
51 |
value = super(ITSocialSecurityNumberField, self).clean(value) |
|
|
52 |
if value == u'': |
|
|
53 |
return value |
|
|
54 |
value = re.sub('\s', u'', value).upper() |
|
|
55 |
try: |
|
|
56 |
check_digit = ssn_check_digit(value) |
|
|
57 |
except ValueError: |
|
|
58 |
raise ValidationError(self.error_messages['invalid']) |
|
|
59 |
if not value[15] == check_digit: |
|
|
60 |
raise ValidationError(self.error_messages['invalid']) |
|
|
61 |
return value |
|
|
62 |
|
|
|
63 |
class ITVatNumberField(Field): |
|
|
64 |
""" |
|
|
65 |
A form field that validates Italian VAT numbers (partita IVA). |
|
|
66 |
""" |
|
|
67 |
default_error_messages = { |
|
|
68 |
'invalid': _(u'Enter a valid VAT number.'), |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
def clean(self, value): |
|
|
72 |
value = super(ITVatNumberField, self).clean(value) |
|
|
73 |
if value == u'': |
|
|
74 |
return value |
|
|
75 |
try: |
|
|
76 |
vat_number = int(value) |
|
|
77 |
except ValueError: |
|
|
78 |
raise ValidationError(self.error_messages['invalid']) |
|
|
79 |
vat_number = str(vat_number).zfill(11) |
|
|
80 |
check_digit = vat_number_check_digit(vat_number[0:10]) |
|
|
81 |
if not vat_number[10] == check_digit: |
|
|
82 |
raise ValidationError(self.error_messages['invalid']) |
|
|
83 |
return smart_unicode(vat_number) |