1 | Index: django/contrib/localflavor/fi/__init__.py
|
---|
2 | ===================================================================
|
---|
3 | Index: django/contrib/localflavor/fi/forms.py
|
---|
4 | ===================================================================
|
---|
5 | --- django/contrib/localflavor/fi/forms.py (revision 0)
|
---|
6 |
|
---|
7 | +++ django/contrib/localflavor/fi/forms.py (revision 0)
|
---|
8 |
|
---|
9 | @@ -0,0 +1,31 @@
|
---|
10 |
|
---|
11 | +"""
|
---|
12 | +FI-specific Form helpers
|
---|
13 | +"""
|
---|
14 | +
|
---|
15 | +from django.newforms import ValidationError
|
---|
16 | +from django.newforms.fields import Field, EMPTY_VALUES
|
---|
17 | +from django.utils.translation import gettext
|
---|
18 | +import re
|
---|
19 | +
|
---|
20 | +class FISocialSecurityNumber(Field):
|
---|
21 | + def clean(self, value):
|
---|
22 | + super(FISocialSecurityNumber, self).clean(value)
|
---|
23 | + if value in EMPTY_VALUES:
|
---|
24 | + return u''
|
---|
25 | +
|
---|
26 | + checkmarks = "0123456789ABCDEFHJKLMNPRSTUVWXY"
|
---|
27 | + result = re.match(r"""^
|
---|
28 | + (?P<date>([0-2]\d|3[01])
|
---|
29 | + (0\d|1[012])
|
---|
30 | + (\d{2}))
|
---|
31 | + [A+-]
|
---|
32 | + (?P<serial>(\d{3}))
|
---|
33 | + (?P<chechsum>[%s])$""" % checkmarks, value, re.VERBOSE | re.IGNORECASE)
|
---|
34 | + if not result:
|
---|
35 | + raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
|
---|
36 | + checksum = int(result.groupdict()['date'] + result.groupdict()['serial'])
|
---|
37 | +
|
---|
38 | + if checkmarks[checksum % len(checkmarks)] == result.groupdict()['chechsum'].upper():
|
---|
39 | + return u'%s' % value.upper()
|
---|
40 | +
|
---|
41 | + raise ValidationError(gettext(u'Enter a valid Finnish social security number.'))
|
---|
42 | Index: tests/regressiontests/forms/tests.py
|
---|
43 |
|
---|
44 | ===================================================================
|
---|
45 |
|
---|
46 | --- tests/regressiontests/forms/tests.py (revision 4866)
|
---|
47 |
|
---|
48 | +++ tests/regressiontests/forms/tests.py (working copy)
|
---|
49 |
|
---|
50 | @@ -3878,7 +3878,49 @@
|
---|
51 |
|
---|
52 | <option value="okinawa">Okinawa</option>
|
---|
53 | </select>
|
---|
54 |
|
---|
55 | +# FISocialSecurityNumber ###############################################################
|
---|
56 |
|
---|
57 | +>>> from django.contrib.localflavor.fi.forms import FISocialSecurityNumber
|
---|
58 | +>>> f = FISocialSecurityNumber()
|
---|
59 | +>>> f.clean('010101-0101')
|
---|
60 | +u'010101-0101'
|
---|
61 | +>>> f.clean('010101+0101')
|
---|
62 | +u'010101+0101'
|
---|
63 | +>>> f.clean('010101A0101')
|
---|
64 | +u'010101A0101'
|
---|
65 | +>>> f.clean('101010-0102')
|
---|
66 | +Traceback (most recent call last):
|
---|
67 | +...
|
---|
68 | +ValidationError: [u'Enter a valid Finnish social security number.']
|
---|
69 | +>>> f.clean('10a010-0101')
|
---|
70 | +Traceback (most recent call last):
|
---|
71 | +...
|
---|
72 | +ValidationError: [u'Enter a valid Finnish social security number.']
|
---|
73 | +>>> f.clean('101010-0\xe401')
|
---|
74 | +Traceback (most recent call last):
|
---|
75 | +...
|
---|
76 | +ValidationError: [u'Enter a valid Finnish social security number.']
|
---|
77 | +>>> f.clean('101010b0101')
|
---|
78 | +Traceback (most recent call last):
|
---|
79 | +...
|
---|
80 | +ValidationError: [u'Enter a valid Finnish social security number.']
|
---|
81 | +>>> f.clean('')
|
---|
82 | +Traceback (most recent call last):
|
---|
83 | +...
|
---|
84 | +ValidationError: [u'This field is required.']
|
---|
85 | +
|
---|
86 | +>>> f.clean(None)
|
---|
87 | +Traceback (most recent call last):
|
---|
88 | +...
|
---|
89 | +ValidationError: [u'This field is required.']
|
---|
90 | +>>> f = FISocialSecurityNumber(required=False)
|
---|
91 | +>>> f.clean('010101-0101')
|
---|
92 | +u'010101-0101'
|
---|
93 | +>>> f.clean(None)
|
---|
94 | +u''
|
---|
95 | +>>> f.clean('')
|
---|
96 | +u''
|
---|
97 | +
|
---|
98 | #################################
|
---|
99 | # Tests of underlying functions #
|
---|
100 | #################################
|
---|