Allow indexation of several objets in the same transaction
authorThierry Florac <thierry.florac@onf.fr>
Tue, 18 Dec 2018 16:36:48 +0100
changeset 120 5985b84fec2f
parent 119 e275b7f6e813
child 121 20be71212884
Allow indexation of several objets in the same transaction
src/pyams_content_es/index.py
--- a/src/pyams_content_es/index.py	Fri Dec 14 19:49:52 2018 +0100
+++ b/src/pyams_content_es/index.py	Tue Dec 18 16:36:48 2018 +0100
@@ -12,17 +12,13 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget
+from pyramid.events import subscriber
 from transaction.interfaces import ITransactionManager
+from zope.intid import IIntIds
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 
-# import packages
-from pyams_utils.registry import query_utility
-from pyramid.events import subscriber
+from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget
+from pyams_utils.registry import get_utility, query_utility
 
 
 #
@@ -54,12 +50,19 @@
 
 @subscriber(IObjectModifiedEvent, context_selector=IDocumentIndexTarget)
 def handle_modified_document(event):
-    """Handle modified document"""
+    """Handle modified document
+
+    We add transaction annotations to avoid several indexations of the same document!
+    """
+    intids = get_utility(IIntIds)
     document = event.object
+    document_id = intids.queryId(document)
     transaction = ITransactionManager(document).get()
-    if 'pyams_content_es.index_document' not in transaction.extension:
+    documents = transaction.extension.get('pyams_content_es.indexed_documents') or set()
+    if document_id not in documents:
+        documents.add(document_id)
         transaction.addAfterCommitHook(index_document, kws={'document': document})
-        transaction.extension['pyams_content_es.index_document'] = True
+        transaction.extension['pyams_content_es.indexed_documents'] = documents
 
 
 @subscriber(IObjectRemovedEvent, context_selector=IDocumentIndexTarget)