[Checkins] SVN: z3c.schema/trunk/src/z3c/schema/email/ Added RFC 822 conform email field to z3c.schema

Roger Ineichen roger at projekt01.ch
Thu Nov 23 22:30:11 EST 2006


Log message for revision 71296:
  Added RFC 822 conform email field to z3c.schema
  Added unit tests

Changed:
  A   z3c.schema/trunk/src/z3c/schema/email/
  A   z3c.schema/trunk/src/z3c/schema/email/README.txt
  A   z3c.schema/trunk/src/z3c/schema/email/__init__.py
  A   z3c.schema/trunk/src/z3c/schema/email/field.py
  A   z3c.schema/trunk/src/z3c/schema/email/interfaces.py
  A   z3c.schema/trunk/src/z3c/schema/email/tests.py

-=-
Added: z3c.schema/trunk/src/z3c/schema/email/README.txt
===================================================================
--- z3c.schema/trunk/src/z3c/schema/email/README.txt	2006-11-24 03:27:58 UTC (rev 71295)
+++ z3c.schema/trunk/src/z3c/schema/email/README.txt	2006-11-24 03:30:10 UTC (rev 71296)
@@ -0,0 +1,82 @@
+==============
+Special Fields
+==============
+
+1. E-Mail Field
+---------------
+
+Let's first generate an E-mail field:
+
+  >>> from z3c.schema.email import RFC822MailAddress
+  >>> email = RFC822MailAddress()
+
+Check that the constraints of the value are fulfilled:
+
+  >>> email.constraint('foo\n')
+  False
+  >>> email.constraint('foo\r')
+  False
+  >>> email.constraint('foo')
+  True
+
+Now make sure the E-mail addresses validate:
+
+  >>> email.validate('foo at bar.com')
+  Traceback (most recent call last):
+  ...
+  WrongType: ('foo at bar.com', <type 'unicode'>)
+
+  >>> email.validate(u'foo at bar.')
+  Traceback (most recent call last):
+  ...
+  NotValidRFC822MailAdress: foo at bar.
+
+  >>> email.validate(u'foo at bar.com')
+
+Since the field uses a simple function to validate its E-mail fields, it is
+easier to use it for the tests:
+
+  >>> from z3c.schema.email import isValidMailAddress
+  >>> isValidMailAddress(u'foo at bar.com')
+  True
+  >>> isValidMailAddress(u'foo.blah at bar.com')
+  True
+
+  # Name failures
+
+  >>> isValidMailAddress(u'foo\r at bar.com')
+  False
+  >>> isValidMailAddress(u'foo<@bar.com')
+  False
+  >>> isValidMailAddress(u'foo:@bar.com')
+  False
+
+  # Overall failures
+
+  >>> isValidMailAddress(u'')
+  False
+  >>> isValidMailAddress(u'foo.')
+  False
+  >>> isValidMailAddress(u'foo. at bar.com')
+  False
+  >>> isValidMailAddress(u'.foo at bar.com')
+  False
+  >>> isValidMailAddress(u'foo at bar.com.')
+  False
+
+  # Domain failures
+
+  >>> isValidMailAddress(u'foo@')
+  False
+  >>> isValidMailAddress(u'foo at bar.')
+  False
+  >>> isValidMailAddress(u'foo at bar')
+  False
+  >>> isValidMailAddress(u'foo at bar..com')
+  False
+  >>> isValidMailAddress(u'foo at bar\r.com')
+  False
+  >>> isValidMailAddress(u'foo at bar<.com')
+  False
+  >>> isValidMailAddress(u'foo at bar:.com')
+  False


Property changes on: z3c.schema/trunk/src/z3c/schema/email/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.schema/trunk/src/z3c/schema/email/__init__.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/email/__init__.py	2006-11-24 03:27:58 UTC (rev 71295)
+++ z3c.schema/trunk/src/z3c/schema/email/__init__.py	2006-11-24 03:30:10 UTC (rev 71296)
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from z3c.schema.email.field import isValidMailAddress
+from z3c.schema.email.field import RFC822MailAddress


Property changes on: z3c.schema/trunk/src/z3c/schema/email/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.schema/trunk/src/z3c/schema/email/field.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/email/field.py	2006-11-24 03:27:58 UTC (rev 71295)
+++ z3c.schema/trunk/src/z3c/schema/email/field.py	2006-11-24 03:30:10 UTC (rev 71296)
@@ -0,0 +1,88 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.interface
+import zope.schema
+
+from z3c.schema.email import interfaces
+
+rfc822_specials = '()<>@,;:\\"[]'
+
+
+def isValidMailAddress(addr):
+    """Returns True if the email address is valid and False if not."""
+
+    # First we validate the name portion (name at domain)
+    c = 0
+    while c < len(addr):
+        if addr[c] == '@':
+            break
+        # Make sure there are only ASCII characters
+        if ord(addr[c]) <= 32 or ord(addr[c]) >= 127:
+            return False
+        # A RFC-822 address cannot contain certain ASCII characters
+        if addr[c] in rfc822_specials:
+            return False
+        c = c + 1
+
+    # check whether we have any input and that the name did not end with a dot
+    if not c or addr[c - 1] == '.':
+        return False
+
+    # check also starting and ending dots in (name at domain)
+    if addr.startswith('.') or addr.endswith('.'):
+        return False
+
+    # Next we validate the domain portion (name at domain)
+    domain = c = c + 1
+    # Ensure that the domain is not empty (name@)
+    if domain >= len(addr):
+        return False
+    count = 0
+    while c < len(addr):
+        # Make sure that domain does not end with a dot or has two dots in a row
+        if addr[c] == '.':
+            if c == domain or addr[c - 1] == '.':
+                return False
+            count = count + 1
+        # Make sure there are only ASCII characters
+        if ord(addr[c]) <= 32 or ord(addr[c]) >= 127:
+            return False
+        # A RFC-822 address cannot contain certain ASCII characters
+        if addr[c] in rfc822_specials:
+            return False
+        c = c + 1
+    if count >= 1:
+        return True
+    else:
+        return False
+
+
+class RFC822MailAddress(zope.schema.TextLine):
+    """A valid email address."""
+    __doc__ = interfaces.IRFC822MailAddress.__doc__
+
+    zope.interface.implements(interfaces.IRFC822MailAddress)
+
+    def constraint(self, value):
+        return '\n' not in value and '\r' not in value
+
+    def _validate(self, value):
+        super(RFC822MailAddress, self)._validate(value)
+        if not isValidMailAddress(value):
+            raise interfaces.NotValidRFC822MailAdress(value)


Property changes on: z3c.schema/trunk/src/z3c/schema/email/field.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.schema/trunk/src/z3c/schema/email/interfaces.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/email/interfaces.py	2006-11-24 03:27:58 UTC (rev 71295)
+++ z3c.schema/trunk/src/z3c/schema/email/interfaces.py	2006-11-24 03:30:10 UTC (rev 71296)
@@ -0,0 +1,29 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.schema
+import zope.schema.interfaces
+from z3c.i18n import MessageFactory as _
+
+
+class IRFC822MailAddress(zope.schema.interfaces.ITextLine):
+    """A valid RFC822 email address field."""
+
+
+class NotValidRFC822MailAdress(zope.schema.ValidationError):
+    __doc__ = _("""Not a valid RFC822 email address""")


Property changes on: z3c.schema/trunk/src/z3c/schema/email/interfaces.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.schema/trunk/src/z3c/schema/email/tests.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/email/tests.py	2006-11-24 03:27:58 UTC (rev 71295)
+++ z3c.schema/trunk/src/z3c/schema/email/tests.py	2006-11-24 03:30:10 UTC (rev 71296)
@@ -0,0 +1,32 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+        ))
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.schema/trunk/src/z3c/schema/email/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list