[Zope-CVS] SVN: book/trunk/redirect/ This is the code that goes with the new ZCML Directives chapter. Garrett

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Aug 23 15:51:17 EDT 2004


Log message for revision 27233:
  This is the code that goes with the new ZCML Directives chapter. Garrett 
  Smith wrote the original version of this code and though of the example, 
  which is great. I greatly simplified the code today be reusing the page 
  directive handler. This also saves us from writing a lot of unit tests. A 
  functional test at this point is totally sufficient.
  


Changed:
  A   book/trunk/redirect/
  A   book/trunk/redirect/__init__.py
  A   book/trunk/redirect/configure.zcml
  A   book/trunk/redirect/ftests.py
  A   book/trunk/redirect/meta.zcml
  A   book/trunk/redirect/metaconfigure.py
  A   book/trunk/redirect/metadirectives.py


-=-
Added: book/trunk/redirect/__init__.py
===================================================================
--- book/trunk/redirect/__init__.py	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/__init__.py	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1 @@
+# Make this directory a package

Added: book/trunk/redirect/configure.zcml
===================================================================
--- book/trunk/redirect/configure.zcml	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/configure.zcml	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1,10 @@
+<configure 
+    xmlns="http://namespaces.zope.org/browser">
+
+  <redirect
+      name="manage.html"
+      for="zope.app.folder.interfaces.IFolder"
+      url="manage"
+      permission="zope.Public" />
+
+</configure>

Added: book/trunk/redirect/ftests.py
===================================================================
--- book/trunk/redirect/ftests.py	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/ftests.py	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1,36 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation 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.
+#
+##############################################################################
+"""Functional tests for Payment Exception view
+
+$Id$
+"""
+import unittest
+
+from zope.app.tests.functional import BrowserTestCase
+
+
+class Test(BrowserTestCase):
+    
+    def test_RedirectManageHtml(self):
+        response = self.publish("/manage.html")
+        
+        self.assertEqual(response.getStatus(), 302)
+        self.assertEqual(response.getHeader('Location'), 'manage')
+  
+
+def test_suite():
+    return unittest.makeSuite(Test)
+  
+if __name__=='__main__':
+    unittest.main(defaultTest='test_suite')

Added: book/trunk/redirect/meta.zcml
===================================================================
--- book/trunk/redirect/meta.zcml	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/meta.zcml	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1,12 @@
+<configure 
+    xmlns="http://namespaces.zope.org/meta">
+
+  <directives namespace="http://namespaces.zope.org/browser">
+    <directive
+      name="redirect"
+      schema=".metadirectives.IRedirectDirective"
+      handler=".metaconfigure.redirect"
+      />
+  </directives>
+
+</configure>

Added: book/trunk/redirect/metaconfigure.py
===================================================================
--- book/trunk/redirect/metaconfigure.py	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/metaconfigure.py	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1,34 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation 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.
+#
+##############################################################################
+"""Directive Handlers
+
+$Id$
+"""
+from zope.app.publisher.browser.viewmeta import page
+
+class Redirect(object):
+   """Redirects to a specified URL."""
+   url = None
+
+   def __call__(self):
+       self.request.response.redirect(self.url)
+
+
+def redirect(_context, name, url, permission, for_=None, layer='default'):
+   
+   # define the class that performs the redirect
+   redirectClass = type(str("Redirect %s for %s to '%s'" %(name, for_, url)),
+       (Redirect,), {'url' : url})
+   
+   page(_context, name, permission, for_, layer, class_=redirectClass)

Added: book/trunk/redirect/metadirectives.py
===================================================================
--- book/trunk/redirect/metadirectives.py	2004-08-23 18:49:28 UTC (rev 27232)
+++ book/trunk/redirect/metadirectives.py	2004-08-23 19:51:16 UTC (rev 27233)
@@ -0,0 +1,45 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation 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.
+#
+##############################################################################
+"""Directive Interfaces
+
+$Id$
+"""
+from zope.interface import Interface
+from zope.configuration.fields import GlobalObject
+from zope.schema import Id, TextLine
+
+class IRedirectDirective(Interface):
+   """Redirects clients to a specified URL."""
+
+   name = TextLine(
+       title=u"Name",
+       description=u"The name of the requested view.")
+
+   for_ = GlobalObject(
+       title=u"For Interface",
+       description=u"The interface the directive is used for.",
+       required=False)
+
+   url = TextLine(
+       title=u"URL",
+       description=u"The URL the client should be redirected to.")
+
+   permission = Id(
+       title=u"Permission",
+       description=u"The permission needed to access the view.")
+
+   layer = TextLine(
+       title=u"Layer",
+       description=u"The layer the redirect is defined in.",
+       required=False)



More information about the Zope-CVS mailing list