Added mail address schema field
authorThierry Florac <thierry.florac@onf.fr>
Tue, 19 Sep 2017 11:20:41 +0200
changeset 110 b1281938f057
parent 109 ebe5c7c45816
child 111 8d80cb67b023
Added mail address schema field
src/pyams_utils/schema.py
--- a/src/pyams_utils/schema.py	Mon Sep 11 14:59:37 2017 +0200
+++ b/src/pyams_utils/schema.py	Tue Sep 19 11:20:41 2017 +0200
@@ -14,6 +14,7 @@
 
 
 # import standard library
+import re
 import string
 
 # import interfaces
@@ -163,3 +164,23 @@
 
     def __init__(self, value_type=None, unique=False, **kw):
         super(TextLineListField, self).__init__(value_type=TextLine(), unique=True, **kw)
+
+
+#
+# Mail address field
+#
+
+class IMailAddressField(ITextLine):
+    """Marker interface for mail address field"""
+
+
+EMAIL_REGEX = re.compile("^[^ @]+@[^ @]+\.[^ @]+$")
+
+
+@implementer(IMailAddressField)
+class MailAddressField(TextLine):
+    """Mail address field"""
+
+    def __init__(self, *args, **kwargs):
+        kwargs['constraint'] = EMAIL_REGEX.match
+        super(MailAddressField, self).__init__(*args, **kwargs)