2007-03-30 13:45:17 +08:00
|
|
|
"""
|
|
|
|
IT-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
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
|
|
|
|
from django.contrib.localflavor.it.util import ssn_check_digit, vat_number_check_digit
|
2007-03-30 13:45:17 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2007-03-30 13:45:17 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ITZipCodeField, self).__init__(r'^\d{5}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *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):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from it_region import REGION_CHOICES
|
2007-03-30 13:45:17 +08:00
|
|
|
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):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from it_province import PROVINCE_CHOICES
|
2007-03-30 13:45:17 +08:00
|
|
|
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 = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _(u'Enter a valid Social Security number.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-04-17 21:37:45 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-04-17 21:37:45 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(ITSocialSecurityNumberField, self).clean(value)
|
|
|
|
if value == u'':
|
|
|
|
return value
|
|
|
|
value = re.sub('\s', u'', value).upper()
|
|
|
|
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 = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _(u'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)
|
|
|
|
if value == u'':
|
|
|
|
return value
|
|
|
|
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)
|