10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
15 |
15 from pyramid.events import subscriber |
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget |
|
20 from transaction.interfaces import ITransactionManager |
16 from transaction.interfaces import ITransactionManager |
|
17 from zope.intid import IIntIds |
21 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent |
18 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent |
22 |
19 |
23 # import packages |
20 from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget |
24 from pyams_utils.registry import query_utility |
21 from pyams_utils.registry import get_utility, query_utility |
25 from pyramid.events import subscriber |
|
26 |
22 |
27 |
23 |
28 # |
24 # |
29 # Documents events |
25 # Documents events |
30 # |
26 # |
52 ITransactionManager(document).get().addAfterCommitHook(index_document, kws={'document': document}) |
48 ITransactionManager(document).get().addAfterCommitHook(index_document, kws={'document': document}) |
53 |
49 |
54 |
50 |
55 @subscriber(IObjectModifiedEvent, context_selector=IDocumentIndexTarget) |
51 @subscriber(IObjectModifiedEvent, context_selector=IDocumentIndexTarget) |
56 def handle_modified_document(event): |
52 def handle_modified_document(event): |
57 """Handle modified document""" |
53 """Handle modified document |
|
54 |
|
55 We add transaction annotations to avoid several indexations of the same document! |
|
56 """ |
|
57 intids = get_utility(IIntIds) |
58 document = event.object |
58 document = event.object |
|
59 document_id = intids.queryId(document) |
59 transaction = ITransactionManager(document).get() |
60 transaction = ITransactionManager(document).get() |
60 if 'pyams_content_es.index_document' not in transaction.extension: |
61 documents = transaction.extension.get('pyams_content_es.indexed_documents') or set() |
|
62 if document_id not in documents: |
|
63 documents.add(document_id) |
61 transaction.addAfterCommitHook(index_document, kws={'document': document}) |
64 transaction.addAfterCommitHook(index_document, kws={'document': document}) |
62 transaction.extension['pyams_content_es.index_document'] = True |
65 transaction.extension['pyams_content_es.indexed_documents'] = documents |
63 |
66 |
64 |
67 |
65 @subscriber(IObjectRemovedEvent, context_selector=IDocumentIndexTarget) |
68 @subscriber(IObjectRemovedEvent, context_selector=IDocumentIndexTarget) |
66 def handle_removed_document(event): |
69 def handle_removed_document(event): |
67 """Handle removed document""" |
70 """Handle removed document""" |