PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/localflavor/no/forms.py

https://code.google.com/p/mango-py/
Python | 82 lines | 66 code | 6 blank | 10 comment | 1 complexity | 810d8704fe4242749abd5bfd9c9d1d9a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Norwegian-specific Form helpers
  3. """
  4. import re, datetime
  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. class NOZipCodeField(RegexField):
  10. default_error_messages = {
  11. 'invalid': _('Enter a zip code in the format XXXX.'),
  12. }
  13. def __init__(self, *args, **kwargs):
  14. super(NOZipCodeField, self).__init__(r'^\d{4}$',
  15. max_length=None, min_length=None, *args, **kwargs)
  16. class NOMunicipalitySelect(Select):
  17. """
  18. A Select widget that uses a list of Norwegian municipalities (fylker)
  19. as its choices.
  20. """
  21. def __init__(self, attrs=None):
  22. from no_municipalities import MUNICIPALITY_CHOICES
  23. super(NOMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES)
  24. class NOSocialSecurityNumber(Field):
  25. """
  26. Algorithm is documented at http://no.wikipedia.org/wiki/Personnummer
  27. """
  28. default_error_messages = {
  29. 'invalid': _(u'Enter a valid Norwegian social security number.'),
  30. }
  31. def clean(self, value):
  32. super(NOSocialSecurityNumber, self).clean(value)
  33. if value in EMPTY_VALUES:
  34. return u''
  35. if not re.match(r'^\d{11}$', value):
  36. raise ValidationError(self.error_messages['invalid'])
  37. day = int(value[:2])
  38. month = int(value[2:4])
  39. year2 = int(value[4:6])
  40. inum = int(value[6:9])
  41. self.birthday = None
  42. try:
  43. if 000 <= inum < 500:
  44. self.birthday = datetime.date(1900+year2, month, day)
  45. if 500 <= inum < 750 and year2 > 54:
  46. self.birthday = datetime.date(1800+year2, month, day)
  47. if 500 <= inum < 1000 and year2 < 40:
  48. self.birthday = datetime.date(2000+year2, month, day)
  49. if 900 <= inum < 1000 and year2 > 39:
  50. self.birthday = datetime.date(1900+year2, month, day)
  51. except ValueError:
  52. raise ValidationError(self.error_messages['invalid'])
  53. sexnum = int(value[8])
  54. if sexnum % 2 == 0:
  55. self.gender = 'F'
  56. else:
  57. self.gender = 'M'
  58. digits = map(int, list(value))
  59. weight_1 = [3, 7, 6, 1, 8, 9, 4, 5, 2, 1, 0]
  60. weight_2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]
  61. def multiply_reduce(aval, bval):
  62. return sum([(a * b) for (a, b) in zip(aval, bval)])
  63. if multiply_reduce(digits, weight_1) % 11 != 0:
  64. raise ValidationError(self.error_messages['invalid'])
  65. if multiply_reduce(digits, weight_2) % 11 != 0:
  66. raise ValidationError(self.error_messages['invalid'])
  67. return value