Added utility functions to (un)index object
authorThierry Florac <thierry.florac@onf.fr>
Wed, 15 Apr 2015 14:34:02 +0200
changeset 5 1f95776705d9
parent 4 1f4b299e041c
child 6 5a5b6b43f977
Added utility functions to (un)index object
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 <tflorac AT ulthar.net>
+# 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)