summaryrefslogtreecommitdiff
path: root/django/contrib/localflavor/pt/forms.py
blob: 1f51679c4a851cdaa63d79b00a4f0a72fc56f2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
PT-specific Form helpers
"""

from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy as _
import re

phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$')


class PTZipCodeField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a zip code in the format XXXX-XXX.'),
    }

    def __init__(self, *args, **kwargs):
        super(PTZipCodeField, self).__init__(r'^(\d{4}-\d{3}|\d{7})$',
            max_length=None, min_length=None, *args, **kwargs)

    def clean(self,value):
        cleaned = super(PTZipCodeField, self).clean(value)
        if len(cleaned) == 7:
           return u'%s-%s' % (cleaned[:4],cleaned[4:])
        else:
           return cleaned
        
class PTPhoneNumberField(Field):
    """
    Validate local Portuguese phone number (including international ones)
    It should have 9 digits (may include spaces) or start by 00 or + (international)
    """
    default_error_messages = {
        'invalid': _('Phone numbers must have 9 digits, or start by + or 00.'),
    }

    def clean(self, value):
        super(PTPhoneNumberField, self).clean(value)
        if value in EMPTY_VALUES:
            return u''
        value = re.sub('(\.|\s)', '', smart_unicode(value))
        m = phone_digits_re.search(value)
        if m:
            return u'%s' % value
        raise ValidationError(self.error_messages['invalid'])