[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/uniqueid/ Added a handler for ObjectRemoveEvents which unregisters the object from all

Albertas Agejevas alga at pov.lt
Fri Jun 11 14:14:51 EDT 2004


Log message for revision 25363:
Added a handler for ObjectRemoveEvents which unregisters the object from all
UniqueIdUtilities.



-=-
Modified: Zope3/trunk/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-06-11 17:53:13 UTC (rev 25362)
+++ Zope3/trunk/src/zope/app/uniqueid/__init__.py	2004-06-11 18:14:50 UTC (rev 25363)
@@ -23,12 +23,14 @@
 """
 import random
 from zope.app.uniqueid.interfaces import IUniqueIdUtility, IReference
+from zope.app.uniqueid.interfaces import UniqueIdRemovedEvent
 from zope.interface import implements
 from ZODB.interfaces import IConnection
 from BTrees import OIBTree, IOBTree
 from zope.app import zapi
 from zope.app.location.interfaces import ILocation
 from zope.security.proxy import trustedRemoveSecurityProxy
+from zope.event import notify
 
 class UniqueIdUtility:
     """This utility provides a two way mapping between objects and
@@ -132,3 +134,21 @@
         if cur is None:
             raise ValueError('Can not get connection of %r' % (ob,))
     return cur._p_jar
+
+
+def removeUniqueIdSubscriber(event):
+    """A subscriber to ObjectRemovedEvent
+
+    Removes the unique ids registered for the object in all the unique
+    id utilities.
+    """
+
+    # Notify the catalogs that this object is about to be removed.
+    notify(UniqueIdRemovedEvent(event))
+
+    for utility in zapi.getAllUtilitiesRegisteredFor(IUniqueIdUtility):
+        try:
+            utility.unregister(event.object)
+        except KeyError:
+            pass
+

Modified: Zope3/trunk/src/zope/app/uniqueid/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-06-11 17:53:13 UTC (rev 25362)
+++ Zope3/trunk/src/zope/app/uniqueid/configure.zcml	2004-06-11 18:14:50 UTC (rev 25363)
@@ -48,6 +48,11 @@
         />
   </content>
 
+  <subscriber
+      factory=".removeUniqueIdSubscriber"
+      for="zope.app.container.interfaces.IObjectRemovedEvent"
+      />
+
   <!-- Views -->
   <include package=".browser" />
 

Modified: Zope3/trunk/src/zope/app/uniqueid/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-06-11 17:53:13 UTC (rev 25362)
+++ Zope3/trunk/src/zope/app/uniqueid/interfaces.py	2004-06-11 18:14:50 UTC (rev 25363)
@@ -3,7 +3,7 @@
 
 $Id$
 """
-from zope.interface import Interface
+from zope.interface import Interface, Attribute, implements
 
 
 class IReference(Interface):
@@ -60,3 +60,20 @@
 
     Allows to query object by id and id by object.
     """
+
+
+class  IUniqueIdRemovedEvent(Interface):
+    """The event which get published before the unique id is removed
+    from the utility so that the catalogs can unindex  the object.
+    """
+    original_event = Attribute(
+        """The IObjectRemoveEvent related to this event""")
+
+
+class UniqueIdRemovedEvent:
+    """The event which get published before the unique id is removed
+    from the utility so that the catalogs can unindex  the object.
+    """
+    implements(IUniqueIdRemovedEvent)
+    def __init__(self, event):
+        self.original_event = event

Modified: Zope3/trunk/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-06-11 17:53:13 UTC (rev 25362)
+++ Zope3/trunk/src/zope/app/uniqueid/tests.py	2004-06-11 18:14:50 UTC (rev 25363)
@@ -21,9 +21,13 @@
 from persistent import Persistent
 from persistent.interfaces import IPersistent
 from zope.app.tests import setup, ztapi
+from zope.app import zapi
 from zope.interface import implements
 from ZODB.interfaces import IConnection
 from zope.app.location.interfaces import ILocation
+from zope.app.component.hooks import setSite
+from zope.app.utility import LocalUtilityService
+from zope.app.servicenames import Utilities
 
 
 class P(Persistent):
@@ -44,7 +48,7 @@
         from zope.app.uniqueid import connectionOfPersistent
         from zope.app.uniqueid import ReferenceToPersistent
         from zope.app.uniqueid.interfaces import IReference
-        root = setup.placefulSetUp(site=True)
+        self.root = setup.placefulSetUp(site=True)
         ztapi.provideAdapter(ILocation, IConnection, connectionOfPersistent)
         ztapi.provideAdapter(IPersistent, IReference, ReferenceToPersistent)
 
@@ -188,12 +192,61 @@
         self.assertRaises(ValueError, connectionOfPersistent, object())
 
 
+class TestRemoveSubscriber(ReferenceSetupMixin, unittest.TestCase):
 
+    def setUp(self):
+        from zope.app.uniqueid.interfaces import IUniqueIdUtility
+        from zope.app.uniqueid import UniqueIdUtility
+        from zope.app.folder import Folder, rootFolder
+
+        ReferenceSetupMixin.setUp(self)
+
+        sm = zapi.getServices(self.root)
+        setup.addService(sm, Utilities, LocalUtilityService())
+        self.utility = setup.addUtility(sm, '1',
+                                        IUniqueIdUtility, UniqueIdUtility())
+
+        self.root['folder1'] = Folder()
+        self.root._p_jar = ConnectionStub()
+        self.root['folder1']['folder1_1'] = self.folder1_1 = Folder()
+        self.root['folder1']['folder1_1']['folder1_1_1'] = Folder()
+
+        sm1_1 = setup.createServiceManager(self.folder1_1)
+        setup.addService(sm1_1, Utilities, LocalUtilityService())
+        self.utility1 = setup.addUtility(sm1_1, '2', IUniqueIdUtility,
+                                         UniqueIdUtility())
+
+    def test(self):
+        from zope.app.uniqueid import removeUniqueIdSubscriber
+        from zope.app.container.contained import ObjectRemovedEvent
+        from zope.app.uniqueid.interfaces import IUniqueIdRemovedEvent
+        folder = self.root['folder1']['folder1_1']['folder1_1_1']
+        id = self.utility.register(folder)
+        id1 = self.utility1.register(folder)
+        self.assertEquals(self.utility.getObject(id), folder)
+        self.assertEquals(self.utility1.getObject(id1), folder)
+        setSite(self.folder1_1)
+
+        events = []
+        ztapi.handle([IUniqueIdRemovedEvent], events.append)
+
+        # This should unregister the object in all utilities, not just the
+        # nearest one.
+        removeUniqueIdSubscriber(ObjectRemovedEvent(folder))
+
+        self.assertRaises(KeyError, self.utility.getObject, id)
+        self.assertRaises(KeyError, self.utility1.getObject, id1)
+
+        self.assertEquals(len(events), 1)
+        self.assertEquals(events[0].original_event.object, folder)
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(TestUniqueIdUtility))
     suite.addTest(unittest.makeSuite(TestReferenceToPersistent))
     suite.addTest(unittest.makeSuite(TestConnectionOfPersistent))
+    suite.addTest(unittest.makeSuite(TestRemoveSubscriber))
     return suite
 
 if __name__ == '__main__':




More information about the Zope3-Checkins mailing list