From 5f2c513f9cfb7c4e20f3c970c602649e64de541d Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 4 Apr 2007 11:40:08 +0000 Subject: [PATCH] Fixed #3919 -- Added German identity card number validation to German localflavor. Thanks, Jannis Leidel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4922 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/de/forms.py | 64 ++++++++++++++++++++++ tests/regressiontests/forms/localflavor.py | 13 +++++ 2 files changed, 77 insertions(+) diff --git a/django/contrib/localflavor/de/forms.py b/django/contrib/localflavor/de/forms.py index 7e1bee5b4e..c82d2d5b8d 100644 --- a/django/contrib/localflavor/de/forms.py +++ b/django/contrib/localflavor/de/forms.py @@ -7,6 +7,8 @@ from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES from django.utils.translation import gettext import re +id_re = re.compile(r"^(?P\d{10})(?P\w{1,3})[-\ ]?(?P\d{7})[-\ ]?(?P\d{7})[-\ ]?(?P\d{1})$") + class DEZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(DEZipCodeField, self).__init__(r'^\d{5}$', @@ -21,3 +23,65 @@ class DEStateSelect(Select): def __init__(self, attrs=None): from de_states import STATE_CHOICES # relative import super(DEStateSelect, self).__init__(attrs, choices=STATE_CHOICES) + +class DEIdentityCardNumberField(Field): + """ + A German identity card number. + + Checks the following rules to determine whether the number is valid: + + * Conforms to the XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format. + * No group consists entirely of zeroes. + * Included checksums match calculated checksums + + Algorithm is documented at http://de.wikipedia.org/wiki/Personalausweis + """ + def has_valid_checksum(self, number): + given_number, given_checksum = number[:-1], number[-1] + calculated_checksum = 0 + fragment = "" + parameter = 7 + + for i in range(len(given_number)): + fragment = str(int(given_number[i])*parameter) + if fragment.isalnum(): + calculated_checksum += int(fragment[-1]) + + if parameter == 1: + parameter = 7 + elif parameter == 3: + parameter = 1 + elif parameter ==7: + parameter = 3 + + if str(calculated_checksum)[-1] == given_checksum: + return True + return False + + def clean(self, value): + super(DEIdentityCardNumberField, self).clean(value) + error_msg = gettext(u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format') + if value in EMPTY_VALUES: + return u'' + match = re.match(id_re, value) + if not match: + raise ValidationError(error_msg) + + residence, origin, birthday, validity, checksum = \ + match.groupdict()['residence'], match.groupdict()['origin'], \ + match.groupdict()['birthday'], match.groupdict()['validity'], \ + match.groupdict()['checksum'] + + if residence == '0000000000' or \ + birthday == '0000000' or \ + validity == '0000000': + raise ValidationError(error_msg) + + all_digits = "%s%s%s%s" % (residence, birthday, validity, checksum) + if not self.has_valid_checksum(residence) or \ + not self.has_valid_checksum(birthday) or \ + not self.has_valid_checksum(validity) or \ + not self.has_valid_checksum(all_digits): + raise ValidationError(error_msg) + + return u'%s%s-%s-%s-%s' % (residence, origin, birthday, validity, checksum) diff --git a/tests/regressiontests/forms/localflavor.py b/tests/regressiontests/forms/localflavor.py index e30b52e366..419b597155 100644 --- a/tests/regressiontests/forms/localflavor.py +++ b/tests/regressiontests/forms/localflavor.py @@ -869,4 +869,17 @@ ValidationError: [u'Enter a zip code in the format XXXXX.'] >>> w = DEStateSelect() >>> w.render('states', 'TH') u'' + +# DEIdentityCardNumberField ################################################# + +>>> from django.contrib.localflavor.de.forms import DEIdentityCardNumberField +>>> f = DEIdentityCardNumberField() +>>> f.clean('7549313035D-6004103-0903042-0') +u'7549313035D-6004103-0903042-0' +>>> f.clean('9786324830D 6104243 0910271 2') +u'9786324830D-6104243-0910271-2' +>>> f.clean('0434657485D-6407276-0508137-9') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format'] """