Implemented E-Mail converter.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 2 Feb 2016 21:08:59 +0000 (21:08 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 2 Feb 2016 21:08:59 +0000 (21:08 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2429 7aebc617-e5e2-0310-91dc-80fb5f6d2477

tests/test_wrvalidators.py
wrpylib/wrvalidators.py

index 8cc33e636f46d92fd2c352cd460fbb5da34b1721..b12962b4fe22cbef6c570d7b674393b2825f4599 100644 (file)
@@ -377,6 +377,25 @@ class TestTelefonauskunft(unittest.TestCase):
         self.assertEqual(telefonauskunft_to_str([('+43-512-1234', 'untertags'), ('+43-664-123456', 'Alm')]), '+43-512-1234 (untertags); +43-664-123456 (Alm)')
 
 
+class TestEmail(unittest.TestCase):
+    def setUp(self):
+        self.good_addresses = ['office@example.com', 'winter+rodeln@localhost', 'joe.doe@exämple.com']
+        self.bad_addresses = ['öffice@example.com', 'winter rodeln@localhost', 'www.winterrodeln.org', 'mailto:info@example.com', 'info@example.com.']
+
+    def test_from_str(self):
+        for value in self.good_addresses:
+            self.assertEqual(value, email_from_str(value))
+        for value in self.bad_addresses:
+            with self.assertRaises(ValueError):
+                email_from_str(value)
+                print(value)
+
+    def test_to_str(self):
+        for value in self.good_addresses:
+            self.assertEqual(value, email_from_str(value))
+
+
+
 class TestBox(unittest.TestCase):
     def test_from_str(self):
         value = '{{MyTemplate|apple=2|banana=5}}'
index 518234556e7af52f8ade0c2b167fdc1a1548a681..f8511de22ea9980d99ddea700e4a2961acf5cd35 100644 (file)
@@ -10,6 +10,7 @@ can assume that the value it has to convert to a string is valid. If it gets an
 undefined.
 """
 import datetime
+import email.headerregistry
 import urllib.parse
 import re
 import xml.dom.minidom as minidom
@@ -1041,6 +1042,13 @@ def telefonauskunft_to_str(value):
 telefonauskunft_converter = FromToConverter(telefonauskunft_from_str, telefonauskunft_to_str)
 
 
+def email_from_str(value):
+    """Takes an email address like 'office@example.com', checks it for correctness and returns it again as string."""
+    try:
+        email.headerregistry.Address(addr_spec=value)
+    except email.errors.HeaderParseError as e:
+        raise ValueError('Invalid email address: {}'.format(value), e)
+    return value
 class PhoneNumber(formencode.FancyValidator):
     """Telefonnumber in international format, e.g. u'+43-699-1234567'"""
     def __init__(self, default_cc=43):
@@ -1053,6 +1061,10 @@ class PhoneNumber(formencode.FancyValidator):
         return self.validator.from_python(value, state)
 
 
+def email_to_str(value):
+    return str(email)
+
+
 class PhoneCommentListNeinLoopNone(NoneValidator):
     """List with semicolon-separated phone numbers in international format with optional comment or 'Nein' as string:
     u''                                                       <=> None