PageRenderTime 107ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/localflavor/be/forms.py

https://code.google.com/p/mango-py/
Python | 71 lines | 32 code | 7 blank | 32 comment | 0 complexity | ab3535d08bd5eae41321f0cfceff450f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Belgium-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 RegexField, Select
  8. from django.utils.translation import ugettext_lazy as _
  9. class BEPostalCodeField(RegexField):
  10. """
  11. A form field that validates its input as a belgium postal code.
  12. Belgium postal code is a 4 digits string. The first digit indicates
  13. the province (except for the 3ddd numbers that are shared by the
  14. eastern part of Flemish Brabant and Limburg and the and 1ddd that
  15. are shared by the Brussels Capital Region, the western part of
  16. Flemish Brabant and Walloon Brabant)
  17. """
  18. default_error_messages = {
  19. 'invalid': _(
  20. 'Enter a valid postal code in the range and format 1XXX - 9XXX.'),
  21. }
  22. def __init__(self, *args, **kwargs):
  23. super(BEPostalCodeField, self).__init__(r'^[1-9]\d{3}$',
  24. max_length=None, min_length=None, *args, **kwargs)
  25. class BEPhoneNumberField(RegexField):
  26. """
  27. A form field that validates its input as a belgium phone number.
  28. Landlines have a seven-digit subscriber number and a one-digit area code,
  29. while smaller cities have a six-digit subscriber number and a two-digit
  30. area code. Cell phones have a six-digit subscriber number and a two-digit
  31. area code preceeded by the number 4.
  32. 0d ddd dd dd, 0d/ddd.dd.dd, 0d.ddd.dd.dd,
  33. 0dddddddd - dialling a bigger city
  34. 0dd dd dd dd, 0dd/dd.dd.dd, 0dd.dd.dd.dd,
  35. 0dddddddd - dialling a smaller city
  36. 04dd ddd dd dd, 04dd/ddd.dd.dd,
  37. 04dd.ddd.dd.dd, 04ddddddddd - dialling a mobile number
  38. """
  39. default_error_messages = {
  40. 'invalid': _('Enter a valid phone number in one of the formats '
  41. '0x xxx xx xx, 0xx xx xx xx, 04xx xx xx xx, '
  42. '0x/xxx.xx.xx, 0xx/xx.xx.xx, 04xx/xx.xx.xx, '
  43. '0x.xxx.xx.xx, 0xx.xx.xx.xx, 04xx.xx.xx.xx, '
  44. '0xxxxxxxx or 04xxxxxxxx.'),
  45. }
  46. def __init__(self, *args, **kwargs):
  47. super(BEPhoneNumberField, self).__init__(r'^[0]\d{1}[/. ]?\d{3}[. ]\d{2}[. ]?\d{2}$|^[0]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$|^[0][4]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$',
  48. max_length=None, min_length=None, *args, **kwargs)
  49. class BERegionSelect(Select):
  50. """
  51. A Select widget that uses a list of belgium regions as its choices.
  52. """
  53. def __init__(self, attrs=None):
  54. from be_regions import REGION_CHOICES
  55. super(BERegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
  56. class BEProvinceSelect(Select):
  57. """
  58. A Select widget that uses a list of belgium provinces as its choices.
  59. """
  60. def __init__(self, attrs=None):
  61. from be_provinces import PROVINCE_CHOICES
  62. super(BEProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES)