Modified event subscribers to automatically update all indexes
authorThierry Florac <thierry.florac@onf.fr>
Wed, 15 Apr 2015 14:33:32 +0200
changeset 4 1f4b299e041c
parent 3 7c6f10ffdda0
child 5 1f95776705d9
Modified event subscribers to automatically update all indexes
src/pyams_catalog/site.py
--- a/src/pyams_catalog/site.py	Wed Apr 15 14:32:55 2015 +0200
+++ b/src/pyams_catalog/site.py	Wed Apr 15 14:33:32 2015 +0200
@@ -21,13 +21,13 @@
 from hypatia.interfaces import ICatalog
 from persistent.interfaces import IPersistent
 from pyams_utils.interfaces.site import ISiteGenerations
-from zope.intid.interfaces import IIntIds
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent, IObjectModifiedEvent
 from zope.site.interfaces import INewLocalSite
 
 # import packages
 from hypatia.catalog import Catalog
-from pyams_utils.registry import utility_config, get_utilities_for, query_utility
+from pyams_catalog.utils import index_object, reindex_object, unindex_object
+from pyams_utils.registry import utility_config, get_utilities_for
 from pyams_utils.site import check_required_utilities
 from pyramid.events import subscriber
 from zope.interface.interface import InterfaceClass
@@ -80,30 +80,19 @@
 @subscriber(IObjectAddedEvent, context_selector=IPersistent)
 def handle_new_object(event):
     """Index new persistent object"""
-    intids = query_utility(IIntIds)
-    if intids is not None:
-        object_id = intids.register(event.object)
-        for name, catalog in get_utilities_for(ICatalog):
-            catalog.index_doc(object_id, event.object)
+    for name, catalog in get_utilities_for(ICatalog):
+        index_object(event.object, catalog, ignore_notyet=True)
 
 
 @subscriber(IObjectModifiedEvent, context_selector=IPersistent)
 def handle_modified_object(event):
     """Update catalog for modified object"""
-    intids = query_utility(IIntIds)
-    if intids is not None:
-        object_id = intids.queryId(event.object)
-        if object_id is not None:
-            for name, catalog in get_utilities_for(ICatalog):
-                catalog.reindex_doc(object_id, event.object)
+    for name, catalog in get_utilities_for(ICatalog):
+        reindex_object(event.object, catalog)
 
 
 @subscriber(IObjectRemovedEvent, context_selector=IPersistent)
 def handle_removed_object(event):
     """Un-index removed object"""
-    intids = query_utility(IIntIds)
-    if intids is not None:
-        object_id = intids.queryId(event.object)
-        if object_id is not None:
-            for name, catalog in get_utilities_for(ICatalog):
-                catalog.unindex_doc(object_id)
+    for name, catalog in get_utilities_for(ICatalog):
+        unindex_object(event.object, catalog)