2007-09-14 06:16:59 +08:00
|
|
|
"""
|
|
|
|
NL-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
import re
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from django.contrib.localflavor.nl.nl_provinces import PROVINCE_CHOICES
|
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, Select
|
2007-09-14 06:16:59 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
2011-10-18 08:47:49 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
|
|
|
|
pc_re = re.compile('^\d{4}[A-Z]{2}$')
|
|
|
|
sofi_re = re.compile('^\d{9}$')
|
|
|
|
numeric_re = re.compile('^\d+$')
|
|
|
|
|
|
|
|
class NLZipCodeField(Field):
|
|
|
|
"""
|
|
|
|
A Dutch postal code field.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Enter a valid postal code'),
|
|
|
|
}
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(NLZipCodeField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
value = value.strip().upper().replace(' ', '')
|
|
|
|
if not pc_re.search(value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if int(value[:4]) < 1000:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
return u'%s %s' % (value[:4], value[4:])
|
|
|
|
|
|
|
|
class NLProvinceSelect(Select):
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
A Select widget that uses a list of provinces of the Netherlands as its
|
2007-09-14 06:16:59 +08:00
|
|
|
choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(NLProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
|
|
|
|
|
|
|
|
class NLPhoneNumberField(Field):
|
|
|
|
"""
|
|
|
|
A Dutch telephone number field.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Enter a valid phone number'),
|
|
|
|
}
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(NLPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
phone_nr = re.sub('[\-\s\(\)]', '', smart_unicode(value))
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if len(phone_nr) == 10 and numeric_re.search(phone_nr):
|
|
|
|
return value
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if phone_nr[:3] == '+31' and len(phone_nr) == 12 and \
|
|
|
|
numeric_re.search(phone_nr[3:]):
|
|
|
|
return value
|
2007-12-17 16:05:27 +08:00
|
|
|
|
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-09-14 06:16:59 +08:00
|
|
|
|
|
|
|
class NLSoFiNumberField(Field):
|
|
|
|
"""
|
|
|
|
A Dutch social security number (SoFi/BSN) field.
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
http://nl.wikipedia.org/wiki/Sofinummer
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Enter a valid SoFi number'),
|
|
|
|
}
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(NLSoFiNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return u''
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if not sofi_re.search(value):
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if int(value) == 0:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
checksum = 0
|
|
|
|
for i in range(9, 1, -1):
|
|
|
|
checksum += int(value[9-i]) * i
|
|
|
|
checksum -= int(value[-1])
|
2007-12-17 16:05:27 +08:00
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
if checksum % 11 != 0:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
|
|
|
|
2007-09-14 06:16:59 +08:00
|
|
|
return value
|