[Checkins] SVN: zope.app.catalog/trunk/ bugfix: passing the context to getAllUtilitiesRegisteredFor in all

Bernd Roessl bernd.roessl at lovelysystems.com
Fri Sep 21 06:37:24 EDT 2007


Log message for revision 79790:
  bugfix: passing the context to getAllUtilitiesRegisteredFor in all
  eventhandlers because no catalog was found which was located in a
  sub site and for example the ObjectModifiesEvent get fired from somewhere
  in the root.
  
  

Changed:
  U   zope.app.catalog/trunk/CHANGES.txt
  U   zope.app.catalog/trunk/setup.py
  U   zope.app.catalog/trunk/src/zope/app/catalog/catalog.py
  U   zope.app.catalog/trunk/src/zope/app/catalog/tests.py

-=-
Modified: zope.app.catalog/trunk/CHANGES.txt
===================================================================
--- zope.app.catalog/trunk/CHANGES.txt	2007-09-21 10:29:29 UTC (rev 79789)
+++ zope.app.catalog/trunk/CHANGES.txt	2007-09-21 10:37:23 UTC (rev 79790)
@@ -2,6 +2,15 @@
 Changes for zope.app.catalog
 ============================
 
+2007/09/21 3.5.0a2:
+===================
+
+- bugfix: passing the context to getAllUtilitiesRegisteredFor in all
+  eventhandlers because no catalog was found which was located in a
+  sub site and for example the ObjectModifiesEvent get fired from somewhere
+  in the root.
+
+
 2007/06/26 3.5.0a1:
 ===================
 

Modified: zope.app.catalog/trunk/setup.py
===================================================================
--- zope.app.catalog/trunk/setup.py	2007-09-21 10:29:29 UTC (rev 79789)
+++ zope.app.catalog/trunk/setup.py	2007-09-21 10:37:23 UTC (rev 79790)
@@ -21,7 +21,7 @@
 from setuptools import setup, find_packages
 
 setup(name = 'zope.app.catalog',
-      version = '3.5.0a1',
+      version = '3.5.0a2',
       url = 'http://svn.zope.org/zope.app.catalog',
       license = 'ZPL 2.1',
       description = 'Zope app.catalog',

Modified: zope.app.catalog/trunk/src/zope/app/catalog/catalog.py
===================================================================
--- zope.app.catalog/trunk/src/zope/app/catalog/catalog.py	2007-09-21 10:29:29 UTC (rev 79789)
+++ zope.app.catalog/trunk/src/zope/app/catalog/catalog.py	2007-09-21 10:37:23 UTC (rev 79790)
@@ -166,7 +166,7 @@
     ob = event.object
     if INoAutoIndex.providedBy(ob):
         return
-    for cat in component.getAllUtilitiesRegisteredFor(ICatalog):
+    for cat in component.getAllUtilitiesRegisteredFor(ICatalog, context=ob):
         id = component.getUtility(IIntIds, context=cat).getId(ob)
         cat.index_doc(id, ob)
 
@@ -176,7 +176,7 @@
     ob = event.object
     if INoAutoReindex.providedBy(ob):
         return
-    for cat in component.getAllUtilitiesRegisteredFor(ICatalog):
+    for cat in component.getAllUtilitiesRegisteredFor(ICatalog, context=ob):
         id = component.getUtility(IIntIds, context=cat).queryId(ob)
         if id is not None:
             cat.index_doc(id, ob)
@@ -184,8 +184,8 @@
 
 def unindexDocSubscriber(event):
     """A subscriber to IntIdRemovedEvent"""
-    for cat in component.getAllUtilitiesRegisteredFor(ICatalog):
-        ob = event.object
+    ob = event.object
+    for cat in component.getAllUtilitiesRegisteredFor(ICatalog, context=ob):
         id = component.getUtility(IIntIds, context=cat).queryId(ob)
         if id is not None:
             cat.unindex_doc(id)

Modified: zope.app.catalog/trunk/src/zope/app/catalog/tests.py
===================================================================
--- zope.app.catalog/trunk/src/zope/app/catalog/tests.py	2007-09-21 10:29:29 UTC (rev 79789)
+++ zope.app.catalog/trunk/src/zope/app/catalog/tests.py	2007-09-21 10:37:23 UTC (rev 79790)
@@ -20,13 +20,14 @@
 """
 import unittest
 from zope.testing import doctest
-
+from zope import component
 from zope.interface import implements
 from zope.interface.verify import verifyObject
 from zope.app.testing import ztapi, setup, placelesssetup
 from BTrees.IFBTree import IFSet
 from zope.app.intid.interfaces import IIntIds
 from zope.app.component.hooks import setSite
+from zope.location.location import Location
 
 from zope.index.interfaces import IInjection, IIndexSearch
 from zope.app.catalog.interfaces import ICatalog
@@ -214,13 +215,10 @@
     def unindex_doc(self, docid):
         self.unregs.append(docid)
 
-class Stub:
+class Stub(Location):
+    pass
 
-    __name__ = None
-    __parent__ = None
 
-
-
 class TestEventSubscribers(unittest.TestCase):
 
     def setUp(self):
@@ -241,6 +239,9 @@
         ob = Stub()
         ob2 = Stub()
 
+        self.root['ob'] = ob
+        self.root['ob2'] = ob2
+
         id = self.utility.register(ob)
         indexDocSubscriber(IntIdAddedEvent(ob, ObjectAddedEvent(ob2)))
 
@@ -252,6 +253,8 @@
         from zope.lifecycleevent import ObjectModifiedEvent
 
         ob = Stub()
+        self.root['ob'] = ob
+
         id = self.utility.register(ob)
 
         reindexDocSubscriber(ObjectModifiedEvent(ob))
@@ -260,6 +263,8 @@
         self.assertEqual(self.cat.unregs, [])
 
         ob2 = Stub()
+        self.root['ob2'] = ob2
+
         reindexDocSubscriber(ObjectModifiedEvent(ob2))
         self.assertEqual(self.cat.regs, [(1, ob)])
         self.assertEqual(self.cat.unregs, [])
@@ -273,6 +278,10 @@
         ob = Stub()
         ob2 = Stub()
         ob3 = Stub()
+        self.root['ob'] = ob
+        self.root['ob2'] = ob2
+        self.root['ob3'] = ob3
+
         id = self.utility.register(ob)
 
         unindexDocSubscriber(
@@ -361,6 +370,128 @@
         self.assertEqual(names, [u'folder1_1_1', u'folder1_1_2'])
 
 
+class TestSubSiteCatalog(unittest.TestCase) :
+    """If a catalog is defined in a sub site and the hooks.setSite was
+    not set the catalog will not be found unless the context in
+    getAllUtilitiesRegisteredFor is set.
+    """
+
+    def setUp(self):
+
+        setup.placefulSetUp(True)
+
+        from zope.app.catalog.catalog import Catalog
+        from zope.app.container.contained import ContainerSublocations
+
+        self.root = setup.buildSampleFolderTree()
+
+        self.subfolder = self.root[u'folder1'][u'folder1_1']
+        root_sm = self.root_sm = setup.createSiteManager(self.root)
+        local_sm = self.local_sm = setup.createSiteManager(self.subfolder)
+        self.utility = setup.addUtility(root_sm, '', IIntIds, IntIdsStub())
+        self.cat = setup.addUtility(local_sm, '', ICatalog, Catalog())
+        self.cat['name'] = StubIndex('__name__', None)
+
+        for obj in self.iterAll(self.root) :
+            self.utility.register(obj)
+
+
+    def tearDown(self):
+        setup.placefulTearDown()
+
+    def iterAll(self, container) :
+        from zope.app.container.interfaces import IContainer
+        for value in container.values() :
+            yield value
+            if IContainer.providedBy(value) :
+                for obj in self.iterAll(value) :
+                    yield obj
+
+
+
+    def test_Index(self):
+        """ Setup a catalog deeper within the containment hierarchy
+        and call the updateIndexes method. The indexed objects should should
+        be restricted to the sublocations.
+        """
+        from zope.app.catalog.catalog import indexDocSubscriber
+        from zope.app.container.contained import ObjectAddedEvent
+
+        ob = Stub()
+        self.subfolder['ob'] = ob
+
+        id = self.utility.register(ob)
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 0)
+
+        setSite(None)
+        indexDocSubscriber(ObjectAddedEvent(ob))
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 1)
+
+
+    def test_updateIndex(self):
+        """ Setup a catalog deeper within the containment hierarchy
+        and call the updateIndexes method. The indexed objects should should
+        be restricted to the sublocations.
+        """
+        from zope.app.catalog.catalog import reindexDocSubscriber
+        from zope.lifecycleevent import ObjectModifiedEvent
+
+        ob = Stub()
+        self.subfolder['ob'] = ob
+
+        id = self.utility.register(ob)
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 0)
+
+        setSite(None)
+        reindexDocSubscriber(ObjectModifiedEvent(ob))
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 1)
+
+    def test_UnIndex(self):
+        """ Setup a catalog deeper within the containment hierarchy
+        and call the updateIndexes method. The indexed objects should should
+        be restricted to the sublocations.
+        """
+        from zope.app.catalog.catalog import indexDocSubscriber
+        from zope.app.container.contained import ObjectAddedEvent
+        from zope.app.catalog.catalog import unindexDocSubscriber
+        from zope.app.container.contained import ObjectRemovedEvent
+
+        ob = Stub()
+        self.subfolder['ob'] = ob
+
+        id = self.utility.register(ob)
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 0)
+
+        setSite(None)
+        indexDocSubscriber(ObjectAddedEvent(ob))
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 1)
+
+        setSite(None)
+        unindexDocSubscriber(ObjectRemovedEvent(ob))
+
+        setSite(self.subfolder)
+        res = self.cat.searchResults(name='ob')
+        self.assertEqual(len(res), 0)
+
+
 class TestCatalogBugs(placelesssetup.PlacelessSetup, unittest.TestCase):
     """I found that z.a.catalog, AttributeIndex failed to remove the previous
     value/object from the index IF the NEW value is None.
@@ -469,6 +600,7 @@
     suite.addTest(unittest.makeSuite(Test))
     suite.addTest(unittest.makeSuite(TestEventSubscribers))
     suite.addTest(unittest.makeSuite(TestIndexUpdating))
+    suite.addTest(unittest.makeSuite(TestSubSiteCatalog))
     suite.addTest(unittest.makeSuite(TestCatalogBugs))
     suite.addTest(unittest.makeSuite(TestIndexRaisingValueGetter))
     suite.addTest(doctest.DocTestSuite('zope.app.catalog.attribute'))



More information about the Checkins mailing list