From 8ffca9009ca6adedb8d7886cc352097d4f4fa0f4 Mon Sep 17 00:00:00 2001 From: Martin Aspeli Date: Sat, 23 May 2009 16:37:10 +0000 Subject: [PATCH] Ensure workflow is initialised when the comment is added. Also ensure that we won't get an error on unindex if the local utility can't be found. svn path=/plone.app.discussion/trunk/; revision=27070 --- plone/app/discussion/comment.py | 10 +++++++++- plone/app/discussion/configure.zcml | 13 ++++++++++--- plone/app/discussion/tests/test_comment.py | 19 ++++++++++++++++--- plone/app/discussion/tool.py | 20 +++++++++++++------- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/plone/app/discussion/comment.py b/plone/app/discussion/comment.py index a573ed2..4425033 100644 --- a/plone/app/discussion/comment.py +++ b/plone/app/discussion/comment.py @@ -12,6 +12,7 @@ from AccessControl.Owned import Owned from plone.app.discussion.interfaces import IComment from Products.CMFCore.DynamicType import DynamicType +from Products.CMFCore.utils import getToolByName class Comment(DynamicType, Traversable, RoleManager, Owned, Explicit): """A comment. @@ -85,4 +86,11 @@ class Comment(DynamicType, Traversable, RoleManager, Owned, Explicit): def opaqueValues(self): return [] -CommentFactory = Factory(Comment) \ No newline at end of file +CommentFactory = Factory(Comment) + +def notify_workflow(obj, event): + """Tell the workflow tool when a comment is added + """ + tool = getToolByName(obj, 'portal_workflow', None) + if tool is not None: + tool.notifyCreated(obj) \ No newline at end of file diff --git a/plone/app/discussion/configure.zcml b/plone/app/discussion/configure.zcml index 931a3f7..491d18d 100644 --- a/plone/app/discussion/configure.zcml +++ b/plone/app/discussion/configure.zcml @@ -26,7 +26,7 @@ component=".comment.CommentFactory" name="plone.Comment" /> - + @@ -38,16 +38,23 @@ + + + diff --git a/plone/app/discussion/tests/test_comment.py b/plone/app/discussion/tests/test_comment.py index 254ab11..999435b 100644 --- a/plone/app/discussion/tests/test_comment.py +++ b/plone/app/discussion/tests/test_comment.py @@ -57,9 +57,22 @@ class CommentTest(PloneTestCase): self.assertEquals('plone/doc1/%2B%2Bconversation%2B%2Bdefault/' + str(new_comment1_id), comment.absolute_url()) def test_workflow(self): - # ensure that we can assign a workflow to the comment type and perform - # workflow operations - pass + self.portal.portal_workflow.setChainForPortalTypes(('Discussion Item',), ('simple_publication_workflow,')) + + conversation = IConversation(self.portal.doc1).__of__(self.portal.doc1) + comment1 = createObject('plone.Comment') + new_comment1_id = conversation.addComment(comment1) + + comment = conversation[new_comment1_id] + + chain = self.portal.portal_workflow.getChainFor(comment) + self.assertEquals(('simple_publication_workflow',), chain) + + # ensure the initial state was entered and recorded + self.assertEquals(1, len(comment.workflow_history['simple_publication_workflow'])) + self.assertEquals(None, comment.workflow_history['simple_publication_workflow'][0]['action']) + + self.assertEquals('private', self.portal.portal_workflow.getInfoFor(comment, 'review_state')) def test_fti(self): # test that we can look up an FTI for Discussion Item diff --git a/plone/app/discussion/tool.py b/plone/app/discussion/tool.py index fb55bf1..b4d2478 100644 --- a/plone/app/discussion/tool.py +++ b/plone/app/discussion/tool.py @@ -6,7 +6,7 @@ BBB support for the old portal_discussion is provided in the bbb package. """ from zope import interface -from zope.component import getUtility +from zope.component import queryUtility from interfaces import ICommentingTool, IComment @@ -65,10 +65,16 @@ class CommentingTool(UniqueObject, SimpleItem): kw['object_provides'] = object_provides return catalog.searchResults(REQUEST, **kw) -def object_added_handler(obj, event): - tool = getUtility(ICommentingTool) - tool.indexObject(obj) +def index_object(obj, event): + """Index the object when added to the conversation + """ + tool = queryUtility(ICommentingTool) + if tool is not None: + tool.indexObject(obj) -def object_removed_handler(obj, event): - tool = getUtility(ICommentingTool) - tool.unindexObject(obj) +def unindex_object(obj, event): + """Unindex the object when removed + """ + tool = queryUtility(ICommentingTool) + if tool is not None: + tool.unindexObject(obj)