2007-03-30 18:04:22 +08:00
|
|
|
"""
|
|
|
|
FI-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2007-03-30 18:18:15 +08:00
|
|
|
import re
|
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-03-30 18:04:22 +08:00
|
|
|
|
|
|
|
class FIZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a zip code in the format XXXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
2007-03-30 18:04:22 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(FIZipCodeField, self).__init__(r'^\d{5}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-03-30 18:04:22 +08:00
|
|
|
|
|
|
|
class FIMunicipalitySelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Finnish municipalities 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 fi_municipalities import MUNICIPALITY_CHOICES
|
2007-03-30 18:04:22 +08:00
|
|
|
super(FIMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
|
2007-03-30 18:18:15 +08:00
|
|
|
|
|
|
|
class FISocialSecurityNumber(Field):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a valid Finnish social security number.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-03-30 18:18:15 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(FISocialSecurityNumber, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2007-04-09 10:38:07 +08:00
|
|
|
return u''
|
|
|
|
|
2007-03-30 18:18:15 +08:00
|
|
|
checkmarks = "0123456789ABCDEFHJKLMNPRSTUVWXY"
|
|
|
|
result = re.match(r"""^
|
|
|
|
(?P<date>([0-2]\d|3[01])
|
|
|
|
(0\d|1[012])
|
|
|
|
(\d{2}))
|
|
|
|
[A+-]
|
|
|
|
(?P<serial>(\d{3}))
|
2007-04-09 10:38:07 +08:00
|
|
|
(?P<checksum>[%s])$""" % checkmarks, value, re.VERBOSE | re.IGNORECASE)
|
2007-03-30 18:18:15 +08:00
|
|
|
if not result:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-09 14:16:44 +08:00
|
|
|
gd = result.groupdict()
|
2007-04-09 10:38:07 +08:00
|
|
|
checksum = int(gd['date'] + gd['serial'])
|
|
|
|
if checkmarks[checksum % len(checkmarks)] == gd['checksum'].upper():
|
2007-03-30 18:18:15 +08:00
|
|
|
return u'%s' % value.upper()
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|