--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content_es/site.py Thu Apr 21 18:24:52 2016 +0200
@@ -0,0 +1,67 @@
+#
+# 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'
+
+
+# import standard library
+import transaction
+
+# import interfaces
+from pyams_content_es.interfaces import IContentIndexerUtility, INDEXER_NAME, IDocumentIndexTarget
+from pyams_utils.interfaces.site import ISiteGenerations
+from zope.site.interfaces import INewLocalSite
+
+# import packages
+from pyams_content_es.utility import ContentIndexerUtility
+from pyams_utils.container import find_objects_providing
+from pyams_utils.registry import utility_config, set_local_registry, query_utility
+from pyams_utils.site import check_required_utilities, site_factory
+from pyramid.events import subscriber
+
+
+REQUIRED_UTILITIES = ((IContentIndexerUtility, '', ContentIndexerUtility, INDEXER_NAME), )
+
+
+@subscriber(INewLocalSite)
+def handle_new_local_site(event):
+ """Create a new indexer utility when a site is created"""
+ site = event.manager.__parent__
+ check_required_utilities(site, REQUIRED_UTILITIES)
+
+
+@utility_config(name='PyAMS content indexer', provides=ISiteGenerations)
+class ContentIndexerGenerationsChecker(object):
+ """Content indexer utility generations checker"""
+
+ generation = 1
+
+ def evolve(self, site, current=None):
+ """Check for required utilities"""
+ check_required_utilities(site, REQUIRED_UTILITIES)
+
+
+def site_index(request):
+ """Index all site contents in ElasticSearch"""
+ application = site_factory(request)
+ if application is not None:
+ try:
+ set_local_registry(application.getSiteManager())
+ indexer = query_utility(IContentIndexerUtility)
+ if indexer is not None:
+ for document in find_objects_providing(application, IDocumentIndexTarget):
+ print("Indexing: {0!r}".format(document))
+ indexer.index_document(document)
+ finally:
+ set_local_registry(None)
+ transaction.commit()
+ return application