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

/django/contrib/localflavor/is_/forms.py

https://code.google.com/p/mango-py/
Python | 83 lines | 71 code | 5 blank | 7 comment | 1 complexity | 825880eb97ee8d264d9317040c97fff5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Iceland specific form helpers.
  3. """
  4. from django.core.validators import EMPTY_VALUES
  5. from django.forms import ValidationError
  6. from django.forms.fields import RegexField
  7. from django.forms.widgets import Select
  8. from django.utils.translation import ugettext_lazy as _
  9. from django.utils.encoding import smart_unicode
  10. class ISIdNumberField(RegexField):
  11. """
  12. Icelandic identification number (kennitala). This is a number every citizen
  13. of Iceland has.
  14. """
  15. default_error_messages = {
  16. 'invalid': _('Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.'),
  17. 'checksum': _(u'The Icelandic identification number is not valid.'),
  18. }
  19. def __init__(self, *args, **kwargs):
  20. kwargs['min_length'],kwargs['max_length'] = 10,11
  21. super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$', *args, **kwargs)
  22. def clean(self, value):
  23. value = super(ISIdNumberField, self).clean(value)
  24. if value in EMPTY_VALUES:
  25. return u''
  26. value = self._canonify(value)
  27. if self._validate(value):
  28. return self._format(value)
  29. else:
  30. raise ValidationError(self.error_messages['checksum'])
  31. def _canonify(self, value):
  32. """
  33. Returns the value as only digits.
  34. """
  35. return value.replace('-', '').replace(' ', '')
  36. def _validate(self, value):
  37. """
  38. Takes in the value in canonical form and checks the verifier digit. The
  39. method is modulo 11.
  40. """
  41. check = [3, 2, 7, 6, 5, 4, 3, 2, 1, 0]
  42. return sum([int(value[i]) * check[i] for i in range(10)]) % 11 == 0
  43. def _format(self, value):
  44. """
  45. Takes in the value in canonical form and returns it in the common
  46. display format.
  47. """
  48. return smart_unicode(value[:6]+'-'+value[6:])
  49. class ISPhoneNumberField(RegexField):
  50. """
  51. Icelandic phone number. Seven digits with an optional hyphen or space after
  52. the first three digits.
  53. """
  54. def __init__(self, *args, **kwargs):
  55. kwargs['min_length'], kwargs['max_length'] = 7,8
  56. super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$', *args, **kwargs)
  57. def clean(self, value):
  58. value = super(ISPhoneNumberField, self).clean(value)
  59. if value in EMPTY_VALUES:
  60. return u''
  61. return value.replace('-', '').replace(' ', '')
  62. class ISPostalCodeSelect(Select):
  63. """
  64. A Select widget that uses a list of Icelandic postal codes as its choices.
  65. """
  66. def __init__(self, attrs=None):
  67. from is_postalcodes import IS_POSTALCODES
  68. super(ISPostalCodeSelect, self).__init__(attrs, choices=IS_POSTALCODES)