[Zope3-checkins] SVN: Zope3/branches/jim-index/src/zope/app/uniqueid/ Implemented a subscriber for ObjectAddedEvents which registers the object in

Albertas Agejevas alga at pov.lt
Sat Jun 12 04:52:20 EDT 2004


Log message for revision 25375:
Implemented a subscriber for ObjectAddedEvents which registers the object in
all IUniqueIdUtilities and sends on a UniqueIdAddedEvent to catalogs.



-=-
Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py	2004-06-12 08:18:19 UTC (rev 25374)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/__init__.py	2004-06-12 08:52:20 UTC (rev 25375)
@@ -22,8 +22,10 @@
 $Id$
 """
 import random
+from persistent import Persistent
 from zope.app.uniqueid.interfaces import IUniqueIdUtility, IReference
 from zope.app.uniqueid.interfaces import UniqueIdRemovedEvent
+from zope.app.uniqueid.interfaces import UniqueIdAddedEvent
 from zope.interface import implements
 from ZODB.interfaces import IConnection
 from BTrees import OIBTree, IOBTree
@@ -32,7 +34,7 @@
 from zope.security.proxy import trustedRemoveSecurityProxy
 from zope.event import notify
 
-class UniqueIdUtility:
+class UniqueIdUtility(Persistent):
     """This utility provides a two way mapping between objects and
     integer ids.
 
@@ -152,3 +154,13 @@
         except KeyError:
             pass
 
+def addUniqueIdSubscriber(event):
+    """A subscriber to ObjectAddedEvent
+
+    Registers the object added in all unique id utilities and fires
+    an event for the catalogs.
+    """
+    for utility in zapi.getAllUtilitiesRegisteredFor(IUniqueIdUtility):
+        utility.register(event.object)
+
+    notify(UniqueIdAddedEvent(event))

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/configure.zcml
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/configure.zcml	2004-06-12 08:18:19 UTC (rev 25374)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/configure.zcml	2004-06-12 08:52:20 UTC (rev 25375)
@@ -53,6 +53,11 @@
       for="zope.app.container.interfaces.IObjectRemovedEvent"
       />
 
+  <subscriber
+      factory=".addUniqueIdSubscriber"
+      for="zope.app.container.interfaces.IObjectAddedEvent"
+      />
+
   <!-- Views -->
   <include package=".browser" />
 

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/ftests.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/ftests.py	2004-06-12 08:18:19 UTC (rev 25374)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/ftests.py	2004-06-12 08:52:20 UTC (rev 25375)
@@ -64,7 +64,8 @@
         response = self.publish(self.basepath + '/uniqueid/@@index.html',
                                 basic='mgr:mgrpw')
         self.assertEquals(response.getStatus(), 200)
-        self.assert_(response.getBody().find('0 objects') > 0)
+        # The utility registers in itself when it is being added
+        self.assert_(response.getBody().find('1 objects') > 0)
         self.assert_('<a href="/++etc++site">/++etc++site</a>'
                      not in response.getBody())
 
@@ -76,7 +77,7 @@
                                 basic='mgr:mgrpw')
         self.assertEquals(response.getStatus(), 200)
         body = response.getBody()
-        self.assert_('2 objects' in body)
+        self.assert_('3 objects' in body)
         self.assert_('<a href="/++etc++site">/++etc++site</a>' in body)
         self.checkForBrokenLinks(body, response.getPath(), basic='mgr:mgrpw')
 

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py	2004-06-12 08:18:19 UTC (rev 25374)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/interfaces.py	2004-06-12 08:52:20 UTC (rev 25375)
@@ -62,7 +62,7 @@
     """
 
 
-class  IUniqueIdRemovedEvent(Interface):
+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.
     """
@@ -77,3 +77,19 @@
     implements(IUniqueIdRemovedEvent)
     def __init__(self, event):
         self.original_event = event
+
+
+class IUniqueIdAddedEvent(Interface):
+    """The event which gets sent when an object is registered in a
+    unique id utility.
+    """
+    original_event = Attribute("The ObjectAddedEvent related to this event")
+
+
+class UniqueIdAddedEvent:
+    """The event which gets sent when an object is registered in a
+    unique id utility.
+    """
+    implements(IUniqueIdAddedEvent)
+    def __init__(self, event):
+        self.original_event = event

Modified: Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py	2004-06-12 08:18:19 UTC (rev 25374)
+++ Zope3/branches/jim-index/src/zope/app/uniqueid/tests.py	2004-06-12 08:52:20 UTC (rev 25375)
@@ -192,7 +192,7 @@
         self.assertRaises(ValueError, connectionOfPersistent, object())
 
 
-class TestRemoveSubscriber(ReferenceSetupMixin, unittest.TestCase):
+class TestSubscribers(ReferenceSetupMixin, unittest.TestCase):
 
     def setUp(self):
         from zope.app.uniqueid.interfaces import IUniqueIdUtility
@@ -216,7 +216,7 @@
         self.utility1 = setup.addUtility(sm1_1, '2', IUniqueIdUtility,
                                          UniqueIdUtility())
 
-    def test(self):
+    def test_removeUniqueIdSubscriber(self):
         from zope.app.uniqueid import removeUniqueIdSubscriber
         from zope.app.container.contained import ObjectRemovedEvent
         from zope.app.uniqueid.interfaces import IUniqueIdRemovedEvent
@@ -240,13 +240,34 @@
         self.assertEquals(len(events), 1)
         self.assertEquals(events[0].original_event.object, folder)
 
+    def test_addUniqueIdSubscriber(self):
+        from zope.app.uniqueid import addUniqueIdSubscriber
+        from zope.app.container.contained import ObjectAddedEvent
+        from zope.app.uniqueid.interfaces import IUniqueIdAddedEvent
+        folder = self.root['folder1']['folder1_1']['folder1_1_1']
+        setSite(self.folder1_1)
 
+        events = []
+        ztapi.handle([IUniqueIdAddedEvent], events.append)
+
+        # This should unregister the object in all utilities, not just the
+        # nearest one.
+        addUniqueIdSubscriber(ObjectAddedEvent(folder))
+
+        # Check that the folder got registered
+        id = self.utility.getId(folder)
+        id1 = self.utility1.getId(folder)
+
+        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))
+    suite.addTest(unittest.makeSuite(TestSubscribers))
     return suite
 
 if __name__ == '__main__':




More information about the Zope3-Checkins mailing list