diff -r 1f4b299e041c -r 1f95776705d9 src/pyams_catalog/utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_catalog/utils.py Wed Apr 15 14:34:02 2015 +0200 @@ -0,0 +1,64 @@ +# +# Copyright (c) 2008-2015 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +from zope.keyreference.interfaces import NotYet + +__docformat__ = 'restructuredtext' + + +# import standard library + +# import interfaces +from hypatia.interfaces import ICatalog +from zope.intid.interfaces import IIntIds + +# import packages +from pyams_utils.registry import query_utility + + +def index_object(obj, catalog, ignore_notyet=False): + """Index given object into catalog""" + intids = query_utility(IIntIds) + if intids is not None: + try: + object_id = intids.register(obj) + except NotYet: + if not ignore_notyet: + raise + else: + if isinstance(catalog, str): + catalog = query_utility(ICatalog, name=catalog) + if catalog is not None: + catalog.index_doc(object_id, obj) + + +def reindex_object(obj, catalog): + """Reindex given object into catalog""" + intids = query_utility(IIntIds) + if intids is not None: + object_id = intids.queryId(obj) + if object_id is not None: + if isinstance(catalog, str): + catalog = query_utility(ICatalog, name=catalog) + if catalog is not None: + catalog.reindex_doc(object_id, obj) + + +def unindex_object(obj, catalog): + """Unindex given object from catalog""" + intids = query_utility(IIntIds) + if intids is not None: + object_id = intids.queryId(obj) + if object_id is not None: + if isinstance(catalog, str): + catalog = query_utility(ICatalog, name=catalog) + if catalog is not None: + catalog.unindex_doc(object_id)