[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/publication/ the new code for getting the publication would return a new publication each

Benji York benji at zope.com
Tue Aug 16 15:46:10 EDT 2005


Log message for revision 37966:
  the new code for getting the publication would return a new publication each
  time, the old code would reuse them; that behavior has been re-instated by
  using a publication cache; added tests to make sure the behavior is preserved
  

Changed:
  U   Zope3/trunk/src/zope/app/publication/httpfactory.py
  U   Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py

-=-
Modified: Zope3/trunk/src/zope/app/publication/httpfactory.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-08-16 19:15:02 UTC (rev 37965)
+++ Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-08-16 19:46:09 UTC (rev 37966)
@@ -34,17 +34,15 @@
 def chooseClasses(method, environment):
     if method in ('GET', 'POST', 'HEAD'):
         content_type = environment.get('CONTENT_TYPE', '')
-        is_xml = content_type.startswith('text/xml')
-
-        soap_request = component.queryUtility(interfaces.ISOAPRequestFactory)
-        if (method == 'POST' and is_xml and environment.get('HTTP_SOAPACTION')
-        and soap_request is not None):
-            request_class = soap_request
-            publication_class = SOAPPublication
-        elif (method == 'POST' and is_xml):
-            request_class = component.queryUtility(
-                interfaces.IXMLRPCRequestFactory, default=XMLRPCRequest)
-            publication_class = XMLRPCPublication
+        if method == 'POST' and content_type.startswith('text/xml'):
+            soap_req = component.queryUtility(interfaces.ISOAPRequestFactory)
+            if environment.get('HTTP_SOAPACTION') and soap_req is not None:
+                request_class = soap_req
+                publication_class = SOAPPublication
+            else:
+                request_class = component.queryUtility(
+                    interfaces.IXMLRPCRequestFactory, default=XMLRPCRequest)
+                publication_class = XMLRPCPublication
         else:
             request_class = component.queryUtility(
                 interfaces.IBrowserRequestFactory, default=BrowserRequest)
@@ -63,13 +61,20 @@
     def __init__(self, db):
         """See `zope.app.publication.interfaces.IPublicationRequestFactory`"""
         self._db = db
+        self._publication_cache = {}
 
     def __call__(self, input_stream, output_steam, env):
         """See `zope.app.publication.interfaces.IPublicationRequestFactory`"""
         method = env.get('REQUEST_METHOD', 'GET').upper()
         request_class, publication_class = chooseClasses(method, env)
+
+        publication = self._publication_cache.get(publication_class)
+        if publication is None:
+            publication = publication_class(self._db)
+            self._publication_cache[publication_class] = publication
+
         request = request_class(input_stream, output_steam, env)
-        request.setPublication(publication_class(self._db))
+        request.setPublication(publication)
         if IBrowserRequest.providedBy(request):
             # only browser requests have skins
             setDefaultSkin(request)

Modified: Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py	2005-08-16 19:15:02 UTC (rev 37965)
+++ Zope3/trunk/src/zope/app/publication/tests/test_browserpublication.py	2005-08-16 19:46:09 UTC (rev 37966)
@@ -36,6 +36,7 @@
 from zope.app.security.principalregistry import principalRegistry
 
 from zope.app.publication.browser import BrowserPublication
+from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
 from zope.app.publication.traversers import TestTraverser
 from zope.app.publication.tests.test_zopepublication \
      import BasePublicationTests as BasePublicationTests_
@@ -305,12 +306,20 @@
             '\r\n\xd1\x82\xd0\xb5\xd1\x81\xd1\x82')
 
 
+class HTTPPublicationRequestFactoryTests(BasePublicationTests):
+
+    def testGetBackSamePublication(self):
+        factory = HTTPPublicationRequestFactory(db=None)
+        args = (None, None, {})
+        self.assert_(id(factory(*args).publication) ==
+                     id(factory(*args).publication))
+
+
 def test_suite():
-    t2 = unittest.makeSuite(BrowserPublicationTests, 'test')
-    t3 = unittest.makeSuite(BrowserDefaultTests, 'test')
     return unittest.TestSuite((
-        t2,
-        t3,
+        unittest.makeSuite(BrowserPublicationTests, 'test'),
+        unittest.makeSuite(BrowserDefaultTests, 'test'),
+        unittest.makeSuite(HTTPPublicationRequestFactoryTests, 'test'),
         doctest.DocTestSuite('zope.app.publication.browser',
                              setUp=placelesssetup.setUp,
                              tearDown=placelesssetup.tearDown),



More information about the Zope3-Checkins mailing list