Passed
Push — 2.x ( ab284b...3a853c )
by Ramon
08:16
created

EmailField._validate()   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
# -*- coding: utf-8 -*-
2
3
from bika.lims.api.mail import is_valid_email_address
4
from plone.schema import _
5
from senaite.core.schema.interfaces import IEmailField
6
from senaite.core.schema.textlinefield import TextLineField
7
from zope.interface import implementer
8
from zope.schema.interfaces import ValidationError
9
10
11
class InvalidEmail(ValidationError):
12
    __doc__ = _("""The specified email is not valid.""")
13
14
15
@implementer(IEmailField)
16
class EmailField(TextLineField):
17
    """Email schema field
18
19
    NOTE: This is an "almost" copy of plone.schema.email.Email, but inherits
20
     from TextLineField instead of NativeStringLine. Thereby, accepts (and
21
     stores) unicode
22
    """
23
24
    def _validate(self, value):
25
        super(EmailField, self)._validate(value)
26
        if not value or is_valid_email_address(value):
27
            return
28
29
        raise InvalidEmail(value)
30