Added script to update catalog indexes for all contents
authorThierry Florac <thierry.florac@onf.fr>
Tue, 27 Jun 2017 11:49:38 +0200
changeset 93 89ac3216c5dd
parent 92 3facc843c06f
child 94 db2826d440df
Added script to update catalog indexes for all contents
src/pyams_content/scripts/__init__.py
src/pyams_content/scripts/index.py
src/pyams_content/site.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/scripts/__init__.py	Tue Jun 27 11:49:38 2017 +0200
@@ -0,0 +1,20 @@
+#
+# 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 interfaces
+
+# import packages
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/scripts/index.py	Tue Jun 27 11:49:38 2017 +0200
@@ -0,0 +1,44 @@
+#
+# 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 argparse
+import sys
+import textwrap
+
+# import interfaces
+
+# import packages
+from pyams_content_es.site import site_index
+from pyramid.paster import bootstrap
+
+
+def index_site():
+    """Update all ElasticSearch indexes"""
+    usage = "usage: {0} config_uri".format(sys.argv[0])
+    description = """Update all internal catalog indexes with all database contents."""
+
+    parser = argparse.ArgumentParser(usage=usage,
+                                     description=textwrap.dedent(description))
+    parser.add_argument('config_uri', help='Name of configuration file')
+    args = parser.parse_args()
+
+    config_uri = args.config_uri
+    env = bootstrap(config_uri)
+    settings, closer = env['registry'].settings, env['closer']
+    try:
+        site_index(env['request'])
+    finally:
+        closer()
--- a/src/pyams_content/site.py	Tue Jun 27 11:49:01 2017 +0200
+++ b/src/pyams_content/site.py	Tue Jun 27 11:49:38 2017 +0200
@@ -14,15 +14,19 @@
 
 
 # import standard library
+import transaction
 
 # import interfaces
+from hypatia.interfaces import ICatalog
+from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_utils.interfaces.site import ISiteGenerations
 from zope.intid.interfaces import IIntIds
 from zope.site.interfaces import INewLocalSite
 
 # import packages
-from pyams_utils.registry import utility_config
-from pyams_utils.site import check_required_utilities
+from pyams_utils.container import find_objects_providing
+from pyams_utils.registry import utility_config, set_local_registry, get_utility
+from pyams_utils.site import check_required_utilities, site_factory
 from pyramid.events import subscriber
 from zope.intid import IntIds
 
@@ -46,3 +50,20 @@
     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 internal catalog"""
+    application = site_factory(request)
+    if application is not None:
+        try:
+            set_local_registry(application.getSiteManager())
+            catalog = get_utility(ICatalog)
+            intids = get_utility(IIntIds)
+            for document in find_objects_providing(application, IWfSharedContent):
+                print("Indexing: {0!r}".format(document))
+                catalog.index_doc(intids.register(document), document)
+        finally:
+            set_local_registry(None)
+        transaction.commit()
+    return application