Fixed #3847 -- Added validation support for Finnish social security numbers.
Thanks, karsu. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4868 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
51c7dadc4c
commit
63a629bb8d
|
@ -2,8 +2,9 @@
|
||||||
FI-specific Form helpers
|
FI-specific Form helpers
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
from django.newforms import ValidationError
|
from django.newforms import ValidationError
|
||||||
from django.newforms.fields import RegexField, Select
|
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
|
||||||
from django.utils.translation import gettext
|
from django.utils.translation import gettext
|
||||||
|
|
||||||
class FIZipCodeField(RegexField):
|
class FIZipCodeField(RegexField):
|
||||||
|
@ -20,3 +21,27 @@ class FIMunicipalitySelect(Select):
|
||||||
def __init__(self, attrs=None):
|
def __init__(self, attrs=None):
|
||||||
from fi_municipalities import MUNICIPALITY_CHOICES # relative import
|
from fi_municipalities import MUNICIPALITY_CHOICES # relative import
|
||||||
super(FIMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
|
super(FIMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
|
||||||
|
|
||||||
|
class FISocialSecurityNumber(Field):
|
||||||
|
def clean(self, value):
|
||||||
|
super(FISocialSecurityNumber, self).clean(value)
|
||||||
|
if value in EMPTY_VALUES:
|
||||||
|
return u''
|
||||||
|
|
||||||
|
checkmarks = "0123456789ABCDEFHJKLMNPRSTUVWXY"
|
||||||
|
result = re.match(r"""^
|
||||||
|
(?P<date>([0-2]\d|3[01])
|
||||||
|
(0\d|1[012])
|
||||||
|
(\d{2}))
|
||||||
|
[A+-]
|
||||||
|
(?P<serial>(\d{3}))
|
||||||
|
(?P<chechsum>[%s])$""" % checkmarks, value, re.VERBOSE | re.IGNORECASE)
|
||||||
|
if not result:
|
||||||
|
raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
|
||||||
|
checksum = int(result.groupdict()['date'] + result.groupdict()['serial'])
|
||||||
|
|
||||||
|
if checkmarks[checksum % len(checkmarks)] == result.groupdict()['chechsum'].upper():
|
||||||
|
return u'%s' % value.upper()
|
||||||
|
|
||||||
|
raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue