[Checkins] SVN: z3c.schema/trunk/src/z3c/schema/baseurl/ Added a
base url field
Roger Ineichen
roger at projekt01.ch
Thu Nov 23 23:32:25 EST 2006
Log message for revision 71299:
Added a base url field
Changed:
A z3c.schema/trunk/src/z3c/schema/baseurl/
A z3c.schema/trunk/src/z3c/schema/baseurl/README.txt
A z3c.schema/trunk/src/z3c/schema/baseurl/__init__.py
A z3c.schema/trunk/src/z3c/schema/baseurl/field.py
A z3c.schema/trunk/src/z3c/schema/baseurl/interfaces.py
A z3c.schema/trunk/src/z3c/schema/baseurl/tests.py
-=-
Added: z3c.schema/trunk/src/z3c/schema/baseurl/README.txt
===================================================================
--- z3c.schema/trunk/src/z3c/schema/baseurl/README.txt 2006-11-24 03:47:49 UTC (rev 71298)
+++ z3c.schema/trunk/src/z3c/schema/baseurl/README.txt 2006-11-24 04:32:24 UTC (rev 71299)
@@ -0,0 +1,64 @@
+========
+BaseURL
+========
+
+A base url field is useful if you need to append view names to a url and
+doesn't like to check every time if the url ends with a backslash before you
+append the view name.
+
+Let's first create the BaseURL field:
+
+ >>> from z3c.schema.baseurl import BaseURL
+ >>> baseURL = BaseURL()
+
+Let's first check the validation of some common base urls.
+
+ >>> baseURL.validate("http://www.python.org/")
+ >>> baseURL.validate("http://www.python.org/foo/")
+ >>> baseURL.validate("http://123.123.123.123/")
+ >>> baseURL.validate("http://123.123.123.123/foo/")
+
+A port specification is also allowed:
+
+ >>> baseURL.validate("http://www.python.org:389/")
+ >>> baseURL.validate("http://www.python.org:389/foo/")
+
+However, a missing backslash is not permitted:
+
+ >>> baseURL.validate("http://www.python.org/foo")
+ Traceback (most recent call last):
+ ...
+ InvalidBaseURL: http://www.python.org/foo
+
+And a missing protocol is also not permitted:
+
+ >>> baseURL.validate("www.python.org/foo/")
+ Traceback (most recent call last):
+ ...
+ InvalidBaseURL: www.python.org/foo/
+
+Let's check some other invalid forms:
+
+ >>> baseURL.validate("$www.python.org/")
+ Traceback (most recent call last):
+ ...
+ InvalidBaseURL: $www.python.org/
+
+ >>> baseURL.validate("333.123.123.123/")
+ Traceback (most recent call last):
+ ...
+ InvalidBaseURL: 333.123.123.123/
+
+Let's also ensure that we can convert to base urls from unicode:
+
+ >>> baseURL.fromUnicode("http://www.python.org:389/")
+ 'http://www.python.org:389/'
+ >>> baseURL.fromUnicode(" http://www.python.org:389/")
+ 'http://www.python.org:389/'
+ >>> baseURL.fromUnicode(" \n http://www.python.org:389/\n")
+ 'http://www.python.org:389/'
+
+ >>> baseURL.fromUnicode("http://www.pyt hon.org:389/")
+ Traceback (most recent call last):
+ ...
+ InvalidBaseURL: http://www.pyt hon.org:389/
Property changes on: z3c.schema/trunk/src/z3c/schema/baseurl/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/baseurl/__init__.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/baseurl/__init__.py 2006-11-24 03:47:49 UTC (rev 71298)
+++ z3c.schema/trunk/src/z3c/schema/baseurl/__init__.py 2006-11-24 04:32:24 UTC (rev 71299)
@@ -0,0 +1,20 @@
+##############################################################################
+#
+# 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.baseurl.interfaces import *
+from z3c.schema.baseurl.field import isValidBaseURL
+from z3c.schema.baseurl.field import BaseURL
Property changes on: z3c.schema/trunk/src/z3c/schema/baseurl/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/baseurl/field.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/baseurl/field.py 2006-11-24 03:47:49 UTC (rev 71298)
+++ z3c.schema/trunk/src/z3c/schema/baseurl/field.py 2006-11-24 04:32:24 UTC (rev 71299)
@@ -0,0 +1,45 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Special Fields
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import re
+
+import zope.interface
+import zope.schema
+
+from z3c.schema.baseurl import interfaces
+
+
+isValidBaseURL = re.compile(
+ r"[a-zA-z0-9+.-]+:" # scheme
+ r"\S*$" # non space (should be pickier)
+ ).match
+
+
+class BaseURL(zope.schema.URI):
+ """Base URL field.
+
+ Such a base url must end with a ``/``. This makes it simpler for
+ append a view name.
+ """
+
+ zope.interface.implements(interfaces.IBaseURL)
+
+ def _validate(self, value):
+ if isValidBaseURL(value) and value.endswith('/') and \
+ not value.endswith(':/'):
+ return
+
+ raise interfaces.InvalidBaseURL, value
+
+ def fromUnicode(self, value):
+ v = str(value.strip())
+ self.validate(v)
+ return v
Property changes on: z3c.schema/trunk/src/z3c/schema/baseurl/field.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/baseurl/interfaces.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/baseurl/interfaces.py 2006-11-24 03:47:49 UTC (rev 71298)
+++ z3c.schema/trunk/src/z3c/schema/baseurl/interfaces.py 2006-11-24 04:32:24 UTC (rev 71299)
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# 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 IBaseURL(zope.schema.interfaces.IURI,
+ zope.schema.interfaces.IFromUnicode):
+ """Base URL field.
+
+ Such a base url must end with a ``/``. This makes it simpler for
+ append a view name.
+ """
+
+
+class InvalidBaseURL(zope.schema.ValidationError):
+ __doc__ = _("""The specified BaseURL is not valid.""")
Property changes on: z3c.schema/trunk/src/z3c/schema/baseurl/interfaces.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.schema/trunk/src/z3c/schema/baseurl/tests.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/baseurl/tests.py 2006-11-24 03:47:49 UTC (rev 71298)
+++ z3c.schema/trunk/src/z3c/schema/baseurl/tests.py 2006-11-24 04:32:24 UTC (rev 71299)
@@ -0,0 +1,59 @@
+###############################################################################
+#
+# Copyright 2006 by refline (Schweiz) AG, CH-5630 Muri
+#
+###############################################################################
+"""Refline Recruiter Tests
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.schema.interfaces import RequiredMissing
+from zope.schema.tests.test_field import FieldTestBase
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+from z3c.schema.baseurl import BaseURL
+from z3c.schema.baseurl import InvalidBaseURL
+
+
+class BaseURLTest(FieldTestBase):
+
+ _Field_Factory = BaseURL
+ _convert = str
+
+ def testValidate(self):
+ field = self._Field_Factory(title=u'BaseURL field', description=u'',
+ readonly=False, required=False)
+ field.validate(None)
+ field.validate(self._convert('host:123.123.123.123/'))
+ field.validate(self._convert('host:123/'))
+ field.validate(self._convert('http://www.host.com:123/'))
+ field.validate(self._convert('http://123.123.123.123:123/'))
+
+ def testValidateRequired(self):
+ field = self._Field_Factory(title=u'BaseURL field', description=u'',
+ readonly=False, required=True)
+ self.assertRaises(RequiredMissing, field.validate, None)
+
+ def testBadStringType(self):
+ field = self._Field_Factory(title=u'BaseURL field')
+ self.assertRaises(InvalidBaseURL, field.validate, u'123.123')
+
+ def test_newlines(self):
+ field = self._Field_Factory(title=u'BaseURL field')
+ self.assertRaises(InvalidBaseURL, field.validate,
+ self._convert('host\nfoo'))
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocFileSuite('README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+ unittest.makeSuite(BaseURLTest),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.schema/trunk/src/z3c/schema/baseurl/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
More information about the Checkins
mailing list