/django/contrib/localflavor/at/forms.py

https://code.google.com/p/mango-py/ · Python · 69 lines · 48 code · 8 blank · 13 comment · 1 complexity · f07c9d82b2753e487ef4e396180183a8 MD5 · raw file

  1. """
  2. AT-specific Form helpers
  3. """
  4. import re
  5. from django.core.validators import EMPTY_VALUES
  6. from django.forms import ValidationError
  7. from django.forms.fields import Field, RegexField, Select
  8. from django.utils.translation import ugettext_lazy as _
  9. re_ssn = re.compile(r'^\d{4} \d{6}')
  10. class ATZipCodeField(RegexField):
  11. """
  12. A form field that validates its input is an Austrian postcode.
  13. Accepts 4 digits.
  14. """
  15. default_error_messages = {
  16. 'invalid': _('Enter a zip code in the format XXXX.'),
  17. }
  18. def __init__(self, *args, **kwargs):
  19. super(ATZipCodeField, self).__init__(r'^\d{4}$',
  20. max_length=None, min_length=None, *args, **kwargs)
  21. class ATStateSelect(Select):
  22. """
  23. A Select widget that uses a list of AT states as its choices.
  24. """
  25. def __init__(self, attrs=None):
  26. from django.contrib.localflavor.at.at_states import STATE_CHOICES
  27. super(ATStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
  28. class ATSocialSecurityNumberField(Field):
  29. """
  30. Austrian Social Security numbers are composed of a 4 digits and 6 digits
  31. field. The latter represents in most cases the person's birthdate while
  32. the first 4 digits represent a 3-digits counter and a one-digit checksum.
  33. The 6-digits field can also differ from the person's birthdate if the
  34. 3-digits counter suffered an overflow.
  35. This code is based on information available on
  36. http://de.wikipedia.org/wiki/Sozialversicherungsnummer#.C3.96sterreich
  37. """
  38. default_error_messages = {
  39. 'invalid': _(u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'),
  40. }
  41. def clean(self, value):
  42. value = super(ATSocialSecurityNumberField, self).clean(value)
  43. if value in EMPTY_VALUES:
  44. return u""
  45. if not re_ssn.search(value):
  46. raise ValidationError(self.error_messages['invalid'])
  47. sqnr, date = value.split(" ")
  48. sqnr, check = (sqnr[:3], (sqnr[3]))
  49. if int(sqnr) < 100:
  50. raise ValidationError(self.error_messages['invalid'])
  51. res = int(sqnr[0])*3 + int(sqnr[1])*7 + int(sqnr[2])*9 \
  52. + int(date[0])*5 + int(date[1])*8 + int(date[2])*4 \
  53. + int(date[3])*2 + int(date[4])*1 + int(date[5])*6
  54. res = res % 11
  55. if res != int(check):
  56. raise ValidationError(self.error_messages['invalid'])
  57. return u'%s%s %s'%(sqnr, check, date,)