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

senaite.core.schema.emailfield   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 16
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A EmailField._validate() 0 6 3
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