From 9befc4c651dd2fc93b5e308c5ec0f1fdc9c49f17 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Wed, 14 Oct 2009 14:58:08 +0000 Subject: [PATCH] Monkey patch for clear-and-rebuild catalog added. This fixes story 847941. Nested comments are not reindexed yet. svn path=/plone.app.discussion/trunk/; revision=30574 --- plone/app/discussion/configure.zcml | 15 +++++++ plone/app/discussion/patches.py | 47 ++++++++++++++++++++++ plone/app/discussion/tests/test_catalog.py | 9 +++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 plone/app/discussion/patches.py diff --git a/plone/app/discussion/configure.zcml b/plone/app/discussion/configure.zcml index a5178a5..a8012cf 100644 --- a/plone/app/discussion/configure.zcml +++ b/plone/app/discussion/configure.zcml @@ -1,6 +1,7 @@ @@ -18,7 +19,20 @@ for="Products.CMFPlone.interfaces.IPloneSiteRoot" /> + + + + + + + @@ -30,6 +44,7 @@ /> + diff --git a/plone/app/discussion/patches.py b/plone/app/discussion/patches.py new file mode 100644 index 0000000..f7d7829 --- /dev/null +++ b/plone/app/discussion/patches.py @@ -0,0 +1,47 @@ +from Acquisition import aq_inner, aq_parent + +from zope.annotation.interfaces import IAnnotations + +from Products.CMFPlone.utils import base_hasattr +from Products.CMFPlone.utils import safe_callable + +from Products.CMFCore.utils import getToolByName + +from plone.app.discussion.conversation import ANNOTATION_KEY + +# security.declareProtected(ManageZCatalogEntries, 'clearFindAndRebuild') +def patchedClearFindAndRebuild(self): + """Empties catalog, then finds all contentish objects (i.e. objects + with an indexObject method), and reindexes them. + This may take a long time. + """ + def indexObject(obj, path): + + if (base_hasattr(obj, 'indexObject') and + safe_callable(obj.indexObject)): + + try: + obj.indexObject() + + annotions = IAnnotations(obj) + catalog = getToolByName(obj, 'portal_catalog', None) + if ANNOTATION_KEY in annotions: + conversation = annotions[ANNOTATION_KEY] + conversation = conversation.__of__(obj) + for comment in conversation.getComments(): + try: + comment = conversation.getComments().next() + comment = comment.__of__(conversation) + if catalog: + catalog.indexObject(comment) + except StopIteration: + pass + + except TypeError: + # Catalogs have 'indexObject' as well, but they + # take different args, and will fail + pass + + self.manage_catalogClear() + portal = aq_parent(aq_inner(self)) + portal.ZopeFindAndApply(portal, search_sub=True, apply_func=indexObject) \ No newline at end of file diff --git a/plone/app/discussion/tests/test_catalog.py b/plone/app/discussion/tests/test_catalog.py index fb4ccc0..4afd6f5 100644 --- a/plone/app/discussion/tests/test_catalog.py +++ b/plone/app/discussion/tests/test_catalog.py @@ -230,11 +230,14 @@ class CommentCatalogTest(PloneTestCase): self.assertEquals(self.comment_brain.in_response_to, 'doc1') def test_clear_and_rebuild_catalog(self): - # ToDo: This test fails if clear and rebuild is run - #self.catalog.clearFindAndRebuild() + # Clear and rebuild catalog + self.catalog.clearFindAndRebuild() + + # Check if comment is still there brains = self.catalog.searchResults(portal_type = 'Discussion Item') self.failUnless(brains) - #comment_brain = brains[0] + comment_brain = brains[0] + self.assertEquals(comment_brain.Title, 'Comment 1') def test_suite(): return unittest.defaultTestLoader.loadTestsFromName(__name__) \ No newline at end of file