1 """ |
1 """ |
2 DE-specific Form helpers |
2 DE-specific Form helpers |
3 """ |
3 """ |
4 |
4 |
5 from django.newforms import ValidationError |
5 from django.forms import ValidationError |
6 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES |
6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES |
7 from django.utils.translation import ugettext |
7 from django.utils.translation import ugettext_lazy as _ |
8 import re |
8 import re |
9 |
9 |
10 id_re = re.compile(r"^(?P<residence>\d{10})(?P<origin>\w{1,3})[-\ ]?(?P<birthday>\d{7})[-\ ]?(?P<validity>\d{7})[-\ ]?(?P<checksum>\d{1})$") |
10 id_re = re.compile(r"^(?P<residence>\d{10})(?P<origin>\w{1,3})[-\ ]?(?P<birthday>\d{7})[-\ ]?(?P<validity>\d{7})[-\ ]?(?P<checksum>\d{1})$") |
11 |
11 |
12 class DEZipCodeField(RegexField): |
12 class DEZipCodeField(RegexField): |
13 default_error_messages = { |
13 default_error_messages = { |
14 'invalid': ugettext('Enter a zip code in the format XXXXX.'), |
14 'invalid': _('Enter a zip code in the format XXXXX.'), |
15 } |
15 } |
16 def __init__(self, *args, **kwargs): |
16 def __init__(self, *args, **kwargs): |
17 super(DEZipCodeField, self).__init__(r'^\d{5}$', |
17 super(DEZipCodeField, self).__init__(r'^\d{5}$', |
18 max_length=None, min_length=None, *args, **kwargs) |
18 max_length=None, min_length=None, *args, **kwargs) |
19 |
19 |
36 * Included checksums match calculated checksums |
36 * Included checksums match calculated checksums |
37 |
37 |
38 Algorithm is documented at http://de.wikipedia.org/wiki/Personalausweis |
38 Algorithm is documented at http://de.wikipedia.org/wiki/Personalausweis |
39 """ |
39 """ |
40 default_error_messages = { |
40 default_error_messages = { |
41 'invalid': ugettext('Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.'), |
41 'invalid': _('Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.'), |
42 } |
42 } |
43 |
43 |
44 def has_valid_checksum(self, number): |
44 def has_valid_checksum(self, number): |
45 given_number, given_checksum = number[:-1], number[-1] |
45 given_number, given_checksum = number[:-1], number[-1] |
46 calculated_checksum = 0 |
46 calculated_checksum = 0 |