Remove broken objects from index
authorThierry Florac <tflorac@ulthar.net>
Wed, 19 Sep 2018 10:49:26 +0200
changeset 955 3cee4d5a0c29
parent 954 e33220a8ab01
child 956 a8723fffbaf6
Remove broken objects from index
src/pyams_content/site.py
--- a/src/pyams_content/site.py	Tue Sep 18 16:28:38 2018 +0200
+++ b/src/pyams_content/site.py	Wed Sep 19 10:49:26 2018 +0200
@@ -12,19 +12,15 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 import transaction
-
-# import interfaces
+from ZODB.interfaces import IBroken
 from hypatia.interfaces import ICatalog
+from zope.interface import Interface
 from zope.intid.interfaces import IIntIds
 
-# import packages
 from pyams_utils.container import find_objects_providing
 from pyams_utils.registry import set_local_registry, get_utility
 from pyams_utils.site import site_factory
-from zope.interface import Interface
 
 
 def site_index(request):
@@ -38,10 +34,13 @@
             transaction.savepoint()
             intids = get_utility(IIntIds)
             for index, document in enumerate(find_objects_providing(application, Interface)):
-                print("Indexing: {0!r}".format(document))
-                catalog.reindex_doc(intids.register(document), document)
-                if not index % 100:
-                    transaction.savepoint()
+                if IBroken.providedBy(document):
+                    print("Skipping broken object: {0!r}".format(document))
+                else:
+                    print("Indexing: {0!r}".format(document))
+                    catalog.reindex_doc(intids.register(document), document)
+                    if not index % 100:
+                        transaction.savepoint()
         finally:
             set_local_registry(None)
         transaction.commit()