adding allowString and allowList to vEmail to make sure, if a list is allowd or a string. devel
authorSandro Knauß <knauss@netzguerilla.net>
Mon, 27 Feb 2012 22:21:22 +0100
branchdevel
changeset 193 e5ec4bfa4929
parent 192 6c708c334f37
child 194 0dad11389eec
adding allowString and allowList to vEmail to make sure, if a list is allowd or a string.
iro/model/message.py
iro/validate.py
tests/email_validate.py
--- a/iro/model/message.py	Mon Feb 27 22:20:19 2012 +0100
+++ b/iro/model/message.py	Mon Feb 27 22:21:22 2012 +0100
@@ -46,8 +46,8 @@
         if len(self.attachments) != len(other.attachments):
             return False
 
-        for i in range(len(self.attachments)):
-            if self.attachments[i] != other.attachments[i]:
+        for a in self.attachments:
+            if a not in other.attachments:
                 return False
         return True
 
--- a/iro/validate.py	Mon Feb 27 22:20:19 2012 +0100
+++ b/iro/validate.py	Mon Feb 27 22:21:22 2012 +0100
@@ -63,7 +63,7 @@
             raise e
     return ret
 
-def vEmail(value, field):
+def vEmail(value, field, allowString=True, allowList=True):
     '''validator for emailadresses (see wikipeda for strange mailadresses and RFC3696)
     valid:
     "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
@@ -79,8 +79,12 @@
     ret = []
     str_=False
     if type(value) is types.StringType:
+        if not allowString:
+            raise ValidateException('%s must be a list of email addresses.'%field)
         str_=True
         value=[value]
+    elif not allowList:
+        raise ValidateException('%s must be a email address - No list of email addresses.'%field)
     for v in value:
         parts= re.match(r'^(.*)@(.+?)$',v)
         if not parts:
--- a/tests/email_validate.py	Mon Feb 27 22:20:19 2012 +0100
+++ b/tests/email_validate.py	Mon Feb 27 22:21:22 2012 +0100
@@ -1,7 +1,7 @@
 import unittest
 
 from iro.validate import vEmail
-from iro.error import InvalidMail
+from iro.error import InvalidMail, ValidateException
 
 
 class testEmail(unittest.TestCase):
@@ -57,3 +57,9 @@
 
     def testString(self):
         self.assertEqual(vEmail('x@test.de', None),"x@test.de")
+
+    def testAllowString(self):
+        self.assertRaises(ValidateException,vEmail, 'x@test.de', None, allowString=False)
+
+    def testAllowList(self):
+        self.assertRaises(ValidateException,vEmail, ['x@test.de'], None, allowList=False)