# HG changeset patch # User Thierry Florac # Date 1429101212 -7200 # Node ID 1f4b299e041ceb99c2ced76d075cb79dfd15d84c # Parent 7c6f10ffdda0fd0746d07385ec2c8cbcc4ced5ea Modified event subscribers to automatically update all indexes diff -r 7c6f10ffdda0 -r 1f4b299e041c 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)