PageRenderTime 162ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/localflavor/sk/forms.py

https://code.google.com/p/mango-py/
Python | 43 lines | 20 code | 6 blank | 17 comment | 0 complexity | f7fc81a23d0f4b27ee481d9dd52087cb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Slovak-specific form helpers
  3. """
  4. from django.forms.fields import Select, RegexField
  5. from django.utils.translation import ugettext_lazy as _
  6. class SKRegionSelect(Select):
  7. """
  8. A select widget widget with list of Slovak regions as choices.
  9. """
  10. def __init__(self, attrs=None):
  11. from sk_regions import REGION_CHOICES
  12. super(SKRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
  13. class SKDistrictSelect(Select):
  14. """
  15. A select widget with list of Slovak districts as choices.
  16. """
  17. def __init__(self, attrs=None):
  18. from sk_districts import DISTRICT_CHOICES
  19. super(SKDistrictSelect, self).__init__(attrs, choices=DISTRICT_CHOICES)
  20. class SKPostalCodeField(RegexField):
  21. """
  22. A form field that validates its input as Slovak postal code.
  23. Valid form is XXXXX or XXX XX, where X represents integer.
  24. """
  25. default_error_messages = {
  26. 'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
  27. }
  28. def __init__(self, *args, **kwargs):
  29. super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
  30. max_length=None, min_length=None, *args, **kwargs)
  31. def clean(self, value):
  32. """
  33. Validates the input and returns a string that contains only numbers.
  34. Returns an empty string for empty values.
  35. """
  36. v = super(SKPostalCodeField, self).clean(value)
  37. return v.replace(' ', '')