2007-03-30 13:45:17 +08:00
|
|
|
"""
|
|
|
|
IT-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from django.contrib.localflavor.it.it_province import PROVINCE_CHOICES
|
|
|
|
from django.contrib.localflavor.it.it_region import REGION_CHOICES
|
|
|
|
from django.contrib.localflavor.it.util import ssn_check_digit, vat_number_check_digit
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.forms.fields import Field, RegexField, Select
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-04-17 21:37:45 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-03-30 13:45:17 +08:00
|
|
|
|
|
|
|
class ITZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a valid zip code.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-03-30 13:45:17 +08:00
|
|
|
super(ITZipCodeField, self).__init__(r'^\d{5}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-03-30 13:45:17 +08:00
|
|
|
|
|
|
|
class ITRegionSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of IT regions as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(ITRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
|
|
|
|
|
|
|
|
class ITProvinceSelect(Select):
|
|
|
|
"""
|
2008-06-16 20:38:58 +08:00
|
|
|
A Select widget that uses a list of IT provinces as its choices.
|
2007-03-30 13:45:17 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(ITProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
|
2007-04-17 21:37:45 +08:00
|
|
|
|
|
|
|
class ITSocialSecurityNumberField(RegexField):
|
|
|
|
"""
|
|
|
|
A form field that validates Italian Social Security numbers (codice fiscale).
|
|
|
|
For reference see http://www.agenziaentrate.it/ and search for
|
|
|
|
'Informazioni sulla codificazione delle persone fisiche'.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Enter a valid Social Security number.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-04-17 21:37:45 +08:00
|
|
|
super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-04-17 21:37:45 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(ITSocialSecurityNumberField, self).clean(value)
|
2010-12-19 04:31:49 +08:00
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
|
|
|
value = re.sub('\s', '', value).upper()
|
2007-04-17 21:37:45 +08:00
|
|
|
try:
|
|
|
|
check_digit = ssn_check_digit(value)
|
|
|
|
except ValueError:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-17 21:37:45 +08:00
|
|
|
if not value[15] == check_digit:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-17 21:37:45 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
class ITVatNumberField(Field):
|
|
|
|
"""
|
|
|
|
A form field that validates Italian VAT numbers (partita IVA).
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Enter a valid VAT number.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-04-17 21:37:45 +08:00
|
|
|
def clean(self, value):
|
|
|
|
value = super(ITVatNumberField, self).clean(value)
|
2010-12-19 04:31:49 +08:00
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-04-17 21:37:45 +08:00
|
|
|
try:
|
|
|
|
vat_number = int(value)
|
|
|
|
except ValueError:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-17 21:37:45 +08:00
|
|
|
vat_number = str(vat_number).zfill(11)
|
|
|
|
check_digit = vat_number_check_digit(vat_number[0:10])
|
|
|
|
if not vat_number[10] == check_digit:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-17 21:37:45 +08:00
|
|
|
return smart_unicode(vat_number)
|