Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 5f2c513f9cfb7c4e20f3c970c602649e64de541d 1 parent 33a3220
Malcolm Tredinnick authored April 04, 2007
64  django/contrib/localflavor/de/forms.py
@@ -7,6 +7,8 @@
7 7
 from django.utils.translation import gettext
8 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})$")
  11
+
10 12
 class DEZipCodeField(RegexField):
11 13
     def __init__(self, *args, **kwargs):
12 14
         super(DEZipCodeField, self).__init__(r'^\d{5}$',
@@ -21,3 +23,65 @@ class DEStateSelect(Select):
21 23
     def __init__(self, attrs=None):
22 24
         from de_states import STATE_CHOICES # relative import
23 25
         super(DEStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  26
+
  27
+class DEIdentityCardNumberField(Field):
  28
+    """
  29
+    A German identity card number.
  30
+
  31
+    Checks the following rules to determine whether the number is valid:
  32
+
  33
+        * Conforms to the XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.
  34
+        * No group consists entirely of zeroes.
  35
+        * Included checksums match calculated checksums
  36
+
  37
+    Algorithm is documented at http://de.wikipedia.org/wiki/Personalausweis
  38
+    """
  39
+    def has_valid_checksum(self, number):
  40
+        given_number, given_checksum = number[:-1], number[-1]
  41
+        calculated_checksum = 0
  42
+        fragment = ""
  43
+        parameter = 7
  44
+
  45
+        for i in range(len(given_number)):
  46
+          fragment = str(int(given_number[i])*parameter)
  47
+          if fragment.isalnum():
  48
+            calculated_checksum += int(fragment[-1])
  49
+
  50
+          if parameter == 1:
  51
+            parameter = 7
  52
+          elif parameter == 3:
  53
+            parameter = 1
  54
+          elif parameter ==7:
  55
+            parameter = 3
  56
+
  57
+        if str(calculated_checksum)[-1] == given_checksum:
  58
+            return True
  59
+        return False
  60
+
  61
+    def clean(self, value):
  62
+        super(DEIdentityCardNumberField, self).clean(value)
  63
+        error_msg = gettext(u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format')
  64
+        if value in EMPTY_VALUES:
  65
+            return u''
  66
+        match = re.match(id_re, value)
  67
+        if not match:
  68
+            raise ValidationError(error_msg)
  69
+
  70
+        residence, origin, birthday, validity, checksum = \
  71
+        match.groupdict()['residence'], match.groupdict()['origin'], \
  72
+        match.groupdict()['birthday'], match.groupdict()['validity'], \
  73
+        match.groupdict()['checksum']
  74
+
  75
+        if residence == '0000000000' or \
  76
+           birthday == '0000000' or \
  77
+           validity == '0000000':
  78
+            raise ValidationError(error_msg)
  79
+
  80
+        all_digits = "%s%s%s%s" % (residence, birthday, validity, checksum)
  81
+        if not self.has_valid_checksum(residence) or \
  82
+            not self.has_valid_checksum(birthday) or \
  83
+            not self.has_valid_checksum(validity) or \
  84
+            not self.has_valid_checksum(all_digits):
  85
+                raise ValidationError(error_msg)
  86
+
  87
+        return u'%s%s-%s-%s-%s' % (residence, origin, birthday, validity, checksum)
13  tests/regressiontests/forms/localflavor.py
@@ -869,4 +869,17 @@
869 869
 >>> w = DEStateSelect()
870 870
 >>> w.render('states', 'TH')
871 871
 u'<select name="states">\n<option value="BW">Baden-W\xfcrttemberg</option>\n<option value="BY">Bavaria</option>\n<option value="BE">Berlin</option>\n<option value="BB">Brandenburg</option>\n<option value="HB">Bremen</option>\n<option value="HH">Hamburg</option>\n<option value="HE">Hessen</option>\n<option value="MV">Mecklenburg-Western Pomerania</option>\n<option value="NI">Lower Saxony</option>\n<option value="NW">North Rhine-Westphalia</option>\n<option value="RP">Rhineland-Palatinate</option>\n<option value="SL">Saarland</option>\n<option value="SN">Saxony</option>\n<option value="ST">Saxony-Anhalt</option>\n<option value="SH">Schleswig-Holstein</option>\n<option value="TH" selected="selected">Thuringia</option>\n</select>'
  872
+
  873
+# DEIdentityCardNumberField #################################################
  874
+
  875
+>>> from django.contrib.localflavor.de.forms import DEIdentityCardNumberField
  876
+>>> f = DEIdentityCardNumberField()
  877
+>>> f.clean('7549313035D-6004103-0903042-0')
  878
+u'7549313035D-6004103-0903042-0'
  879
+>>> f.clean('9786324830D 6104243 0910271 2')
  880
+u'9786324830D-6104243-0910271-2'
  881
+>>> f.clean('0434657485D-6407276-0508137-9')
  882
+Traceback (most recent call last):
  883
+...
  884
+ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format']
872 885
 """

0 notes on commit 5f2c513

Please sign in to comment.
Something went wrong with that request. Please try again.