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

/django/contrib/localflavor/nl/forms.py

https://code.google.com/p/mango-py/
Python | 101 lines | 90 code | 5 blank | 6 comment | 1 complexity | 9f64183f50a217fa751b9107f790160f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. NL-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, Select
  8. from django.utils.translation import ugettext_lazy as _
  9. from django.utils.encoding import smart_unicode
  10. pc_re = re.compile('^\d{4}[A-Z]{2}$')
  11. sofi_re = re.compile('^\d{9}$')
  12. numeric_re = re.compile('^\d+$')
  13. class NLZipCodeField(Field):
  14. """
  15. A Dutch postal code field.
  16. """
  17. default_error_messages = {
  18. 'invalid': _('Enter a valid postal code'),
  19. }
  20. def clean(self, value):
  21. super(NLZipCodeField, self).clean(value)
  22. if value in EMPTY_VALUES:
  23. return u''
  24. value = value.strip().upper().replace(' ', '')
  25. if not pc_re.search(value):
  26. raise ValidationError(self.error_messages['invalid'])
  27. if int(value[:4]) < 1000:
  28. raise ValidationError(self.error_messages['invalid'])
  29. return u'%s %s' % (value[:4], value[4:])
  30. class NLProvinceSelect(Select):
  31. """
  32. A Select widget that uses a list of provinces of the Netherlands as its
  33. choices.
  34. """
  35. def __init__(self, attrs=None):
  36. from nl_provinces import PROVINCE_CHOICES
  37. super(NLProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)
  38. class NLPhoneNumberField(Field):
  39. """
  40. A Dutch telephone number field.
  41. """
  42. default_error_messages = {
  43. 'invalid': _('Enter a valid phone number'),
  44. }
  45. def clean(self, value):
  46. super(NLPhoneNumberField, self).clean(value)
  47. if value in EMPTY_VALUES:
  48. return u''
  49. phone_nr = re.sub('[\-\s\(\)]', '', smart_unicode(value))
  50. if len(phone_nr) == 10 and numeric_re.search(phone_nr):
  51. return value
  52. if phone_nr[:3] == '+31' and len(phone_nr) == 12 and \
  53. numeric_re.search(phone_nr[3:]):
  54. return value
  55. raise ValidationError(self.error_messages['invalid'])
  56. class NLSoFiNumberField(Field):
  57. """
  58. A Dutch social security number (SoFi/BSN) field.
  59. http://nl.wikipedia.org/wiki/Sofinummer
  60. """
  61. default_error_messages = {
  62. 'invalid': _('Enter a valid SoFi number'),
  63. }
  64. def clean(self, value):
  65. super(NLSoFiNumberField, self).clean(value)
  66. if value in EMPTY_VALUES:
  67. return u''
  68. if not sofi_re.search(value):
  69. raise ValidationError(self.error_messages['invalid'])
  70. if int(value) == 0:
  71. raise ValidationError(self.error_messages['invalid'])
  72. checksum = 0
  73. for i in range(9, 1, -1):
  74. checksum += int(value[9-i]) * i
  75. checksum -= int(value[-1])
  76. if checksum % 11 != 0:
  77. raise ValidationError(self.error_messages['invalid'])
  78. return value