PageRenderTime 17ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/localflavor/jp/forms.py

https://code.google.com/p/mango-py/
Python | 37 lines | 21 code | 4 blank | 12 comment | 0 complexity | 9106a6f1453795cc0466560d4aac33e4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. JP-specific Form helpers
  3. """
  4. from django.forms import ValidationError
  5. from django.utils.translation import ugettext_lazy as _
  6. from django.forms.fields import RegexField, Select
  7. class JPPostalCodeField(RegexField):
  8. """
  9. A form field that validates its input is a Japanese postcode.
  10. Accepts 7 digits, with or without a hyphen.
  11. """
  12. default_error_messages = {
  13. 'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
  14. }
  15. def __init__(self, *args, **kwargs):
  16. super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
  17. max_length=None, min_length=None, *args, **kwargs)
  18. def clean(self, value):
  19. """
  20. Validates the input and returns a string that contains only numbers.
  21. Returns an empty string for empty values.
  22. """
  23. v = super(JPPostalCodeField, self).clean(value)
  24. return v.replace('-', '')
  25. class JPPrefectureSelect(Select):
  26. """
  27. A Select widget that uses a list of Japanese prefectures as its choices.
  28. """
  29. def __init__(self, attrs=None):
  30. from jp_prefectures import JP_PREFECTURES
  31. super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)