#
# 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.
#
__docformat__ = 'restructuredtext'
from pyramid.events import subscriber
from transaction.interfaces import ITransactionManager
from zope.intid import IIntIds
from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget
from pyams_utils.registry import get_utility, query_utility
#
# Documents events
#
[docs]def index_document(status, document):
if not status: # aborted transaction
return
indexer = query_utility(IContentIndexerUtility)
if indexer is not None:
indexer.index_document(document)
[docs]def unindex_document(status, document):
if not status: # aborted transaction
return
indexer = query_utility(IContentIndexerUtility)
if indexer is not None:
indexer.unindex_document(document)
[docs]@subscriber(IObjectAddedEvent, context_selector=IDocumentIndexTarget)
def handle_added_document(event):
"""Handle added document"""
document = event.object
ITransactionManager(document).get().addAfterCommitHook(index_document, kws={'document': document})
[docs]@subscriber(IObjectModifiedEvent, context_selector=IDocumentIndexTarget)
def handle_modified_document(event):
"""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()
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.indexed_documents'] = documents
[docs]@subscriber(IObjectRemovedEvent, context_selector=IDocumentIndexTarget)
def handle_removed_document(event):
"""Handle removed document"""
document = event.object
ITransactionManager(document).get().addAfterCommitHook(unindex_document, kws={'document': document})