[Checkins] SVN: Products.ZCatalog/trunk/ - added support for using ZCatalog as local utility

Yvo Schubbe y.2011 at wcm-solutions.de
Thu Sep 22 08:12:30 EST 2011


Log message for revision 122892:
  - added support for using ZCatalog as local utility

Changed:
  U   Products.ZCatalog/trunk/CHANGES.txt
  UU  Products.ZCatalog/trunk/src/Products/ZCatalog/CatalogBrains.py
  U   Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_brains.py

-=-
Modified: Products.ZCatalog/trunk/CHANGES.txt
===================================================================
--- Products.ZCatalog/trunk/CHANGES.txt	2011-09-22 12:12:55 UTC (rev 122891)
+++ Products.ZCatalog/trunk/CHANGES.txt	2011-09-22 13:12:30 UTC (rev 122892)
@@ -4,6 +4,8 @@
 2.13.21 (unreleased)
 --------------------
 
+- Added support for using ZCatalog as local utility.
+  This feature requires the optional `five.globalrequest` dependency.
 
 2.13.20 (2011-08-23)
 --------------------

Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/CatalogBrains.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/CatalogBrains.py	2011-09-22 12:12:55 UTC (rev 122891)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/CatalogBrains.py	2011-09-22 13:12:30 UTC (rev 122892)
@@ -7,20 +7,34 @@
 # 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
+# FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
+"""Catalog brain implementation.
+"""
 
-from zope.interface import implements
+from pkg_resources import DistributionNotFound
+from pkg_resources import get_distribution
 
-import Acquisition
+try:
+    get_distribution('five.globalrequest')
+except DistributionNotFound:
+    _GLOBALREQUEST_INSTALLED = False
+else:
+    _GLOBALREQUEST_INSTALLED = True
+
+from .interfaces import ICatalogBrain
+from Acquisition import aq_base
 from Acquisition import aq_parent
-import Record
+from Acquisition import Implicit
+from Record import Record
+if _GLOBALREQUEST_INSTALLED:
+    from zope.globalrequest import getRequest
+from zope.interface import implements
+from ZPublisher.BaseRequest import RequestContainer
 
-from interfaces import ICatalogBrain
 
-
-class AbstractCatalogBrain(Record.Record, Acquisition.Implicit):
+class AbstractCatalogBrain(Record, Implicit):
     """Abstract base brain that handles looking up attributes as
     required, and provides just enough smarts to let us get the URL, path,
     and cataloged object without having to ask the catalog directly.
@@ -39,14 +53,27 @@
 
     def getURL(self, relative=0):
         """Generate a URL for this record"""
-        return self.REQUEST.physicalPathToURL(self.getPath(), relative)
+        request = getattr(self, 'REQUEST', None)
+        if request is None:
+            if _GLOBALREQUEST_INSTALLED:
+                request = getRequest()
+        return request.physicalPathToURL(self.getPath(), relative)
 
     def _unrestrictedGetObject(self):
         """Return the object for this record
 
         Same as getObject, but does not do security checks.
         """
-        return aq_parent(self).unrestrictedTraverse(self.getPath())
+        parent = aq_parent(self)
+        if getattr(parent, 'REQUEST', None) is None:
+            if _GLOBALREQUEST_INSTALLED:
+                request = getRequest()
+                if request is not None:
+                    # path should be absolute, starting at the physical root
+                    parent = self.getPhysicalRoot()
+                    request_container = RequestContainer(REQUEST=request)
+                    parent = aq_base(parent).__of__(request_container)
+        return parent.unrestrictedTraverse(self.getPath())
 
     def getObject(self, REQUEST=None):
         """Return the object for this record
@@ -63,6 +90,14 @@
         if not path:
             return None
         parent = aq_parent(self)
+        if getattr(parent, 'REQUEST', None) is None:
+            if _GLOBALREQUEST_INSTALLED:
+                request = getRequest()
+                if request is not None:
+                    # path should be absolute, starting at the physical root
+                    parent = self.getPhysicalRoot()
+                    request_container = RequestContainer(REQUEST=request)
+                    parent = aq_base(parent).__of__(request_container)
         if len(path) > 1:
             parent = parent.unrestrictedTraverse(path[:-1])
 


Property changes on: Products.ZCatalog/trunk/src/Products/ZCatalog/CatalogBrains.py
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
   - 1.10

Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_brains.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_brains.py	2011-09-22 12:12:55 UTC (rev 122891)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/tests/test_brains.py	2011-09-22 13:12:30 UTC (rev 122892)
@@ -21,6 +21,11 @@
 from zExceptions import Unauthorized
 from ZODB.POSException import ConflictError
 
+from ..CatalogBrains import _GLOBALREQUEST_INSTALLED
+if _GLOBALREQUEST_INSTALLED:
+    from zope.globalrequest import clearRequest
+    from zope.globalrequest import setRequest
+
 _marker = object()
 
 
@@ -83,6 +88,9 @@
 
     _paths = ['/conflicter', '/happy', '/secret', '/zonked']
 
+    def getPhysicalRoot(self):
+        return aq_parent(self)
+
     def getpath(self, rid):
         return self._paths[rid]
 
@@ -139,6 +147,15 @@
         self.root.REQUEST = request
         self.assertEqual(b.getURL(), 'http://superbad.com/conflicter')
 
+    if _GLOBALREQUEST_INSTALLED:
+        def testGetURL_catalog_as_utility(self):
+            request = DummyRequest()
+            b = self._makeBrain(0)
+
+            setRequest(request)
+            self.assertEqual(b.getURL(), 'http://superbad.com/conflicter')
+            clearRequest()
+
     def testGetRID(self):
         b = self._makeBrain(42)
         self.assertEqual(b.getRID(), 42)
@@ -153,6 +170,18 @@
         self.assertTrue(aq_base(b.getObject()) is
                         aq_base(self.cat.getobject(1)))
 
+    if _GLOBALREQUEST_INSTALLED:
+        def testGetObjectHappy_catalog_as_utility(self):
+            request = DummyRequest()
+            b = self._makeBrain(1)
+
+            setRequest(request)
+            self.assertEqual(b.getPath(), '/happy')
+            self.assertEqual(b.getObject().REQUEST, request)
+            self.assertTrue(aq_base(b.getObject()) is
+                            aq_base(self.cat.getobject(1)))
+            clearRequest()
+
     def testGetObjectPropagatesConflictErrors(self):
         b = self._makeBrain(0)
         self.assertEqual(b.getPath(), '/conflicter')



More information about the checkins mailing list