Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / fields.py @ bd2c6bc5

History | View | Annotate | Download (3.6 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import re
35

    
36
from django.utils.translation import ugettext as _
37
from django.utils.encoding import smart_str, force_text
38
from django import forms
39

    
40

    
41
class EmailValidator(object):
42
    """
43
    Email validator. Backported from django 1.6
44
    """
45
    message = _('Enter a valid email address.')
46
    code = 'invalid'
47
    user_regex = re.compile(
48
        r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"  # dot-atom
49
        r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"$)', # quoted-string
50
        re.IGNORECASE)
51
    domain_regex = re.compile(
52
        r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,})$'  # domain
53
        # literal form, ipv4 address (SMTP 4.1.3)
54
        r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
55
        re.IGNORECASE)
56
    domain_whitelist = ['localhost']
57

    
58
    def __init__(self, message=None, code=None, whitelist=None):
59
        if message is not None:
60
            self.message = message
61
        if code is not None:
62
            self.code = code
63
        if whitelist is not None:
64
            self.domain_whitelist = whitelist
65

    
66
    def __call__(self, value):
67
        value = force_text(value)
68

    
69
        if not value or '@' not in value:
70
            raise forms.ValidationError(self.message, code=self.code)
71

    
72
        user_part, domain_part = value.rsplit('@', 1)
73

    
74
        if not self.user_regex.match(user_part):
75
            raise forms.ValidationError(self.message, code=self.code)
76

    
77
        if (not domain_part in self.domain_whitelist and
78
                not self.domain_regex.match(domain_part)):
79
            # Try for possible IDN domain-part
80
            try:
81
                domain_part = domain_part.encode('idna').decode('ascii')
82
                if not self.domain_regex.match(domain_part):
83
                    raise forms.ValidationError(self.message, code=self.code)
84
                else:
85
                    return
86
            except UnicodeError:
87
                pass
88
            raise forms.ValidationError(self.message, code=self.code)
89

    
90

    
91
class EmailField(forms.EmailField):
92
    default_validators = [EmailValidator()]