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