PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/localflavor/de/forms.py

https://code.google.com/p/mango-py/
Python | 84 lines | 61 code | 6 blank | 17 comment | 0 complexity | ec187b2cb38dabab4b8a85de31e89f86 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. DE-specific Form helpers
  3. """
  4. from django.core.validators import EMPTY_VALUES
  5. from django.forms import ValidationError
  6. from django.forms.fields import Field, RegexField, Select
  7. from django.utils.translation import ugettext_lazy as _
  8. import re
  9. 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. class DEZipCodeField(RegexField):
  11. default_error_messages = {
  12. 'invalid': _('Enter a zip code in the format XXXXX.'),
  13. }
  14. def __init__(self, *args, **kwargs):
  15. super(DEZipCodeField, self).__init__(r'^\d{5}$',
  16. max_length=None, min_length=None, *args, **kwargs)
  17. class DEStateSelect(Select):
  18. """
  19. A Select widget that uses a list of DE states as its choices.
  20. """
  21. def __init__(self, attrs=None):
  22. from de_states import STATE_CHOICES
  23. super(DEStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  24. class DEIdentityCardNumberField(Field):
  25. """
  26. A German identity card number.
  27. Checks the following rules to determine whether the number is valid:
  28. * Conforms to the XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.
  29. * No group consists entirely of zeroes.
  30. * Included checksums match calculated checksums
  31. Algorithm is documented at http://de.wikipedia.org/wiki/Personalausweis
  32. """
  33. default_error_messages = {
  34. 'invalid': _('Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.'),
  35. }
  36. def has_valid_checksum(self, number):
  37. given_number, given_checksum = number[:-1], number[-1]
  38. calculated_checksum = 0
  39. fragment = ""
  40. parameter = 7
  41. for i in range(len(given_number)):
  42. fragment = str(int(given_number[i]) * parameter)
  43. if fragment.isalnum():
  44. calculated_checksum += int(fragment[-1])
  45. if parameter == 1:
  46. parameter = 7
  47. elif parameter == 3:
  48. parameter = 1
  49. elif parameter ==7:
  50. parameter = 3
  51. return str(calculated_checksum)[-1] == given_checksum
  52. def clean(self, value):
  53. super(DEIdentityCardNumberField, self).clean(value)
  54. if value in EMPTY_VALUES:
  55. return u''
  56. match = re.match(id_re, value)
  57. if not match:
  58. raise ValidationError(self.error_messages['invalid'])
  59. gd = match.groupdict()
  60. residence, origin = gd['residence'], gd['origin']
  61. birthday, validity, checksum = gd['birthday'], gd['validity'], gd['checksum']
  62. if residence == '0000000000' or birthday == '0000000' or validity == '0000000':
  63. raise ValidationError(self.error_messages['invalid'])
  64. all_digits = u"%s%s%s%s" % (residence, birthday, validity, checksum)
  65. if not self.has_valid_checksum(residence) or not self.has_valid_checksum(birthday) or \
  66. not self.has_valid_checksum(validity) or not self.has_valid_checksum(all_digits):
  67. raise ValidationError(self.error_messages['invalid'])
  68. return u'%s%s-%s-%s-%s' % (residence, origin, birthday, validity, checksum)