PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 35ms app.codeStats 0ms

/django/contrib/localflavor/fr/forms.py

https://code.google.com/p/mango-py/
Python | 51 lines | 41 code | 5 blank | 5 comment | 1 complexity | c55a2828ad7ce925289a54cbb6f28a29 MD5 | raw file
 1"""
 2FR-specific Form helpers
 3"""
 4
 5from django.core.validators import EMPTY_VALUES
 6from django.forms import ValidationError
 7from django.forms.fields import Field, RegexField, Select
 8from django.utils.encoding import smart_unicode
 9from django.utils.translation import ugettext_lazy as _
10import re
11
12phone_digits_re = re.compile(r'^0\d(\s|\.)?(\d{2}(\s|\.)?){3}\d{2}$')
13
14class FRZipCodeField(RegexField):
15    default_error_messages = {
16        'invalid': _('Enter a zip code in the format XXXXX.'),
17    }
18
19    def __init__(self, *args, **kwargs):
20        super(FRZipCodeField, self).__init__(r'^\d{5}$',
21            max_length=None, min_length=None, *args, **kwargs)
22
23class FRPhoneNumberField(Field):
24    """
25    Validate local French phone number (not international ones)
26    The correct format is '0X XX XX XX XX'.
27    '0X.XX.XX.XX.XX' and '0XXXXXXXXX' validate but are corrected to
28    '0X XX XX XX XX'.
29    """
30    default_error_messages = {
31        'invalid': _('Phone numbers must be in 0X XX XX XX XX format.'),
32    }
33
34    def clean(self, value):
35        super(FRPhoneNumberField, self).clean(value)
36        if value in EMPTY_VALUES:
37            return u''
38        value = re.sub('(\.|\s)', '', smart_unicode(value))
39        m = phone_digits_re.search(value)
40        if m:
41            return u'%s %s %s %s %s' % (value[0:2], value[2:4], value[4:6], value[6:8], value[8:10])
42        raise ValidationError(self.error_messages['invalid'])
43
44class FRDepartmentSelect(Select):
45    """
46    A Select widget that uses a list of FR departments as its choices.
47    """
48    def __init__(self, attrs=None):
49        from fr_department import DEPARTMENT_ASCII_CHOICES
50        super(FRDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_ASCII_CHOICES)
51