Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.7 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
38
from django.utils.encoding import force_unicode as force_text
39
from django import forms
40

    
41

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

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

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

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

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

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

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

    
91

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