|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of SENAITE.CORE. |
|
4
|
|
|
# |
|
5
|
|
|
# SENAITE.CORE is free software: you can redistribute it and/or modify it under |
|
6
|
|
|
# the terms of the GNU General Public License as published by the Free Software |
|
7
|
|
|
# Foundation, version 2. |
|
8
|
|
|
# |
|
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
12
|
|
|
# details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
|
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
|
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17
|
|
|
# |
|
18
|
|
|
# Copyright 2018-2020 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
import re |
|
22
|
|
|
|
|
23
|
|
|
from Products.Archetypes.Field import StringField |
|
24
|
|
|
from Products.Archetypes.Registry import registerField |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class EmailsField(StringField): |
|
28
|
|
|
"""Field for string representation of a list of emails |
|
29
|
|
|
""" |
|
30
|
|
|
_properties = StringField._properties.copy() |
|
31
|
|
|
_properties.update({ |
|
32
|
|
|
"type": "emails_field", |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
def set(self, instance, value, **kwargs): |
|
36
|
|
|
if value: |
|
37
|
|
|
# Standardize to comma-separated and remove duplicates |
|
38
|
|
|
validator = instance.plone_utils.validateSingleEmailAddress |
|
39
|
|
|
value = ", ".join(self.to_list(value, validator=validator)) |
|
40
|
|
|
super(EmailsField, self).set(instance, value, **kwargs) |
|
41
|
|
|
|
|
42
|
|
|
def get(self, instance, **kwargs): |
|
43
|
|
|
if kwargs.get("as_list", False): |
|
44
|
|
|
# Return as a list |
|
45
|
|
|
validator = instance.plone_utils.validateSingleEmailAddress |
|
46
|
|
|
return self.to_list(self.get(instance), validator) |
|
47
|
|
|
return super(EmailsField, self).get(instance, **kwargs) |
|
48
|
|
|
|
|
49
|
|
|
def to_list(self, value, validator=None): |
|
50
|
|
|
"""Transforms the value to a list of values |
|
51
|
|
|
""" |
|
52
|
|
|
if not value: |
|
53
|
|
|
return [] |
|
54
|
|
|
emails = map(lambda x: x.strip(), re.split("[;,]", value)) |
|
55
|
|
|
emails = filter(None, emails) |
|
56
|
|
|
if validator: |
|
57
|
|
|
emails = filter(lambda email: validator(email), emails) |
|
58
|
|
|
return emails |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
registerField(EmailsField, |
|
62
|
|
|
title="Emails", |
|
63
|
|
|
description="Used for storing e-mail addresses in string format") |
|
64
|
|
|
|