[Checkins] SVN: z3c.responseheaders/ initial import of the reponse header setter package

Bernd Dorn bernd.dorn at lovelysystems.com
Fri Nov 24 10:05:28 EST 2006


Log message for revision 71301:
  initial import of the reponse header setter package

Changed:
  A   z3c.responseheaders/
  A   z3c.responseheaders/trunk/
  A   z3c.responseheaders/trunk/src/
  A   z3c.responseheaders/trunk/src/z3c/
  A   z3c.responseheaders/trunk/src/z3c/__init__.py
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/README.txt
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/SETUP.cfg
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/__init__.py
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/configure.zcml
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/interfaces.py
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/setter.py
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/tests.py
  A   z3c.responseheaders/trunk/src/z3c/responseheaders/z3c.responseheaders-configure.zcml

-=-
Added: z3c.responseheaders/trunk/src/z3c/__init__.py
===================================================================


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

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/README.txt
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/README.txt	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/README.txt	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,62 @@
+===================================
+Response Header handling for Zope 3
+===================================
+
+This package provides an implementation for setting response headers
+(e.g. for chahe settings) on browser views by providing adapters to
+views. This way we do not have to change view code in order to set
+headers differently.
+
+To demonstrate this behaviour we create a browserview and a content
+class.
+
+  >>> from zope.publisher.browser import BrowserView
+  >>> from zope import component
+  >>> from zope import interface
+
+  >>> class IFoo(interface.Interface):
+  ...     pass
+  >>> class Foo(object):
+  ...     interface.implements(IFoo)
+  ...     pass
+  >>> class IFooView(interface.Interface):
+  ...     pass
+  >>> class FooView(BrowserView):
+  ...     interface.implements(IFooView)
+  ...     def __call__(self):
+  ...         return u'i am so foo'
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+  >>> foo = Foo()
+  >>> view = FooView(foo, request)
+
+Headers are applied before traversing the view. We do not set up
+events for this test because we don't need it. We just call the
+handler directly
+
+  >>> from z3c.responseheaders import setter
+  >>> setter.onBrowserViewBeforeTraverse(view, request)
+
+In the normal case no cache headers are applied because no adapters
+are registered.
+
+  >>> request.response.getHeaders()
+  [('X-Powered-By', 'Zope (www.zope.org), Python (www.python.org)')]
+
+Let us define a header setter adapter. The adapter has to adapt
+(context, view) to IResponseHeaderSetter.
+
+  >>> class MySetter(setter.BaseSetter):
+  ...     component.adapts(IFooView)
+  ...     headers = (('My-Header','My Header Value'),)
+
+  >>> component.provideAdapter(MySetter)
+
+And call the event handler again.
+
+  >>> setter.onBrowserViewBeforeTraverse(view, request)
+  >>> request.response.getHeaders()
+  [('X-Powered-By', 'Zope (www.zope.org),
+   Python (www.python.org)'),
+   ('My-Header', 'My Header Value')]


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

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/SETUP.cfg
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/SETUP.cfg	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/SETUP.cfg	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+    z3c.responseheaders-*.zcml
+</data-files>
\ No newline at end of file


Property changes on: z3c.responseheaders/trunk/src/z3c/responseheaders/SETUP.cfg
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/__init__.py
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/__init__.py	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/__init__.py	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,17 @@
+##############################################################################
+#
+# Copyright (c) 2005 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'


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

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/configure.zcml
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/configure.zcml	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/configure.zcml	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,10 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ i18n_domain="zope"
+ >
+  <subscriber
+   for="zope.publisher.interfaces.browser.IBrowserView
+        zope.app.publication.interfaces.IBeforeTraverseEvent"
+   handler=".setter.onBrowserViewBeforeTraverse"
+   />
+ </configure>


Property changes on: z3c.responseheaders/trunk/src/z3c/responseheaders/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/interfaces.py
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/interfaces.py	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/interfaces.py	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,7 @@
+from zope import interface
+
+class IResponseHeaderSetter(interface.Interface):
+
+    def setHeaders():
+        """sets the response headers for the view"""
+    


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

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/setter.py
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/setter.py	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/setter.py	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,24 @@
+from zope import interface
+from zope import component
+from zope.security.proxy import removeSecurityProxy
+import interfaces
+
+def onBrowserViewBeforeTraverse(obj, event):
+    adapter = interfaces.IResponseHeaderSetter(obj, None)
+    if adapter is None:
+        return
+    adapter.setHeaders()
+
+class BaseSetter(object):
+
+    interface.implements(interfaces.IResponseHeaderSetter)
+    headers = []
+
+    def __init__(self, context):
+        self.context = context
+        
+    def setHeaders(self):
+        request = removeSecurityProxy(self.context).request
+        for name, value in self.headers:
+            request.response.setHeader(name, value)
+


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

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/tests.py
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/tests.py	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/tests.py	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1,40 @@
+##############################################################################
+#
+# Copyright (c) 2005 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
+from zope.app.testing import setup
+
+
+def setUp(test):
+    root = setup.placefulSetUp()
+def tearDown(test):
+    setup.placefulTearDown()
+
+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.responseheaders/trunk/src/z3c/responseheaders/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.responseheaders/trunk/src/z3c/responseheaders/z3c.responseheaders-configure.zcml
===================================================================
--- z3c.responseheaders/trunk/src/z3c/responseheaders/z3c.responseheaders-configure.zcml	2006-11-24 11:38:03 UTC (rev 71300)
+++ z3c.responseheaders/trunk/src/z3c/responseheaders/z3c.responseheaders-configure.zcml	2006-11-24 15:05:27 UTC (rev 71301)
@@ -0,0 +1 @@
+<include package="z3c.responseheaders"/>
\ No newline at end of file


Property changes on: z3c.responseheaders/trunk/src/z3c/responseheaders/z3c.responseheaders-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list