Implement events for Comment (ICommentModifiedEvent) and Reply(IReplyModifiedEvent) modification

This commit is contained in:
Érico Andrei 2021-09-08 13:02:34 +02:00
parent 49d584e8db
commit 6f3fa11f10
6 changed files with 99 additions and 0 deletions

1
news/183.feature Normal file
View File

@ -0,0 +1 @@
Implement events for Comment (ICommentModifiedEvent) and Reply(IReplyModifiedEvent) modification [ericof]

View File

@ -13,8 +13,10 @@ from OFS.Traversable import Traversable
from persistent import Persistent from persistent import Persistent
from plone.app.discussion import _ from plone.app.discussion import _
from plone.app.discussion.events import CommentAddedEvent from plone.app.discussion.events import CommentAddedEvent
from plone.app.discussion.events import CommentModifiedEvent
from plone.app.discussion.events import CommentRemovedEvent from plone.app.discussion.events import CommentRemovedEvent
from plone.app.discussion.events import ReplyAddedEvent from plone.app.discussion.events import ReplyAddedEvent
from plone.app.discussion.events import ReplyModifiedEvent
from plone.app.discussion.events import ReplyRemovedEvent from plone.app.discussion.events import ReplyRemovedEvent
from plone.app.discussion.interfaces import IComment from plone.app.discussion.interfaces import IComment
from plone.app.discussion.interfaces import IConversation from plone.app.discussion.interfaces import IConversation
@ -258,6 +260,16 @@ def notify_comment_added(obj, event):
return notify(CommentAddedEvent(context, obj)) return notify(CommentAddedEvent(context, obj))
def notify_comment_modified(obj, event):
""" Notify custom discussion events when a comment, or a reply, is modified
"""
conversation = aq_parent(obj)
context = aq_parent(conversation)
if getattr(obj, 'in_reply_to', None):
return notify(ReplyModifiedEvent(context, obj))
return notify(CommentModifiedEvent(context, obj))
def notify_comment_removed(obj, event): def notify_comment_removed(obj, event):
""" Notify custom discussion events when a comment or reply is removed """ Notify custom discussion events when a comment or reply is removed
""" """

View File

@ -2,12 +2,14 @@
""" Custom discussion events """ Custom discussion events
""" """
from plone.app.discussion.interfaces import ICommentAddedEvent from plone.app.discussion.interfaces import ICommentAddedEvent
from plone.app.discussion.interfaces import ICommentModifiedEvent
from plone.app.discussion.interfaces import ICommentRemovedEvent from plone.app.discussion.interfaces import ICommentRemovedEvent
from plone.app.discussion.interfaces import IDiscussionEvent from plone.app.discussion.interfaces import IDiscussionEvent
from plone.app.discussion.interfaces import ICommentDeletedEvent from plone.app.discussion.interfaces import ICommentDeletedEvent
from plone.app.discussion.interfaces import ICommentPublishedEvent from plone.app.discussion.interfaces import ICommentPublishedEvent
from plone.app.discussion.interfaces import ICommentTransitionEvent from plone.app.discussion.interfaces import ICommentTransitionEvent
from plone.app.discussion.interfaces import IReplyAddedEvent from plone.app.discussion.interfaces import IReplyAddedEvent
from plone.app.discussion.interfaces import IReplyModifiedEvent
from plone.app.discussion.interfaces import IReplyRemovedEvent from plone.app.discussion.interfaces import IReplyRemovedEvent
from zope.interface import implementer from zope.interface import implementer
@ -35,6 +37,12 @@ class CommentAddedEvent(DiscussionEvent):
""" """
@implementer(ICommentModifiedEvent)
class CommentModifiedEvent(DiscussionEvent):
""" Event to be triggered when a Comment is modified
"""
@implementer(ICommentRemovedEvent) @implementer(ICommentRemovedEvent)
class CommentRemovedEvent(DiscussionEvent): class CommentRemovedEvent(DiscussionEvent):
""" Event to be triggered when a Comment is removed """ Event to be triggered when a Comment is removed
@ -47,6 +55,12 @@ class ReplyAddedEvent(DiscussionEvent):
""" """
@implementer(IReplyModifiedEvent)
class ReplyModifiedEvent(DiscussionEvent):
""" Event to be triggered when a Comment reply is modified
"""
@implementer(IReplyRemovedEvent) @implementer(IReplyRemovedEvent)
class ReplyRemovedEvent(DiscussionEvent): class ReplyRemovedEvent(DiscussionEvent):
""" Event to be triggered when a Comment reply is removed """ Event to be triggered when a Comment reply is removed

View File

@ -399,6 +399,11 @@ class ICommentAddedEvent(IDiscussionEvent):
""" """
class ICommentModifiedEvent(IDiscussionEvent):
""" Comment modified
"""
class ICommentRemovedEvent(IDiscussionEvent): class ICommentRemovedEvent(IDiscussionEvent):
""" Comment removed """ Comment removed
""" """
@ -409,6 +414,11 @@ class IReplyAddedEvent(IDiscussionEvent):
""" """
class IReplyModifiedEvent(IDiscussionEvent):
""" Comment reply modified
"""
class IReplyRemovedEvent(IDiscussionEvent): class IReplyRemovedEvent(IDiscussionEvent):
""" Comment reply removed """ Comment reply removed
""" """

View File

@ -9,6 +9,12 @@
handler=".comment.notify_workflow" handler=".comment.notify_workflow"
/> />
<subscriber
for="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler=".comment.notify_comment_modified"
/>
<subscriber <subscriber
for="plone.app.discussion.interfaces.IComment for="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectAddedEvent" zope.lifecycleevent.interfaces.IObjectAddedEvent"

View File

@ -6,6 +6,8 @@ from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID from plone.app.testing import TEST_USER_ID
from Zope2.App import zcml from Zope2.App import zcml
from zope.component import createObject from zope.component import createObject
from zope.event import notify
from zope.lifecycleevent import ObjectModifiedEvent
import Products.Five import Products.Five
import unittest import unittest
@ -20,8 +22,10 @@ class EventsRegistry(object):
""" Fake registry to be used while testing discussion events """ Fake registry to be used while testing discussion events
""" """
commentAdded = False commentAdded = False
commentModified = False
commentRemoved = False commentRemoved = False
replyAdded = False replyAdded = False
replyModified = False
replyRemoved = False replyRemoved = False
# #
@ -33,6 +37,10 @@ def comment_added(doc, evt):
EventsRegistry.commentAdded = True EventsRegistry.commentAdded = True
def comment_modified(doc, evt):
EventsRegistry.commentModified = True
def comment_removed(doc, evt): def comment_removed(doc, evt):
EventsRegistry.commentRemoved = True EventsRegistry.commentRemoved = True
@ -41,6 +49,10 @@ def reply_added(doc, evt):
EventsRegistry.replyAdded = True EventsRegistry.replyAdded = True
def reply_modified(doc, evt):
EventsRegistry.replyModified = True
def reply_removed(doc, evt): def reply_removed(doc, evt):
EventsRegistry.replyRemoved = True EventsRegistry.replyRemoved = True
@ -78,6 +90,12 @@ class CommentEventsTest(unittest.TestCase):
handler="plone.app.discussion.tests.test_events.comment_added" handler="plone.app.discussion.tests.test_events.comment_added"
/> />
<subscriber
for="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler="plone.app.discussion.tests.test_events.comment_modified"
/>
<subscriber <subscriber
for="OFS.interfaces.ISimpleItem for="OFS.interfaces.ISimpleItem
plone.app.discussion.interfaces.ICommentRemovedEvent" plone.app.discussion.interfaces.ICommentRemovedEvent"
@ -96,6 +114,18 @@ class CommentEventsTest(unittest.TestCase):
conversation.addComment(comment) conversation.addComment(comment)
self.assertTrue(self.registry.commentAdded) self.assertTrue(self.registry.commentAdded)
def test_modifyEvent(self):
self.assertFalse(self.registry.commentModified)
comment = createObject('plone.Comment')
conversation = IConversation(self.document)
new_id = conversation.addComment(comment)
comment = self.document.restrictedTraverse(
'++conversation++default/{0}'.format(new_id),
)
comment.text = "foo"
notify(ObjectModifiedEvent(comment))
self.assertTrue(self.registry.commentModified)
def test_removedEvent(self): def test_removedEvent(self):
self.assertFalse(self.registry.commentRemoved) self.assertFalse(self.registry.commentRemoved)
comment = createObject('plone.Comment') comment = createObject('plone.Comment')
@ -131,6 +161,12 @@ class RepliesEventsTest(unittest.TestCase):
handler="plone.app.discussion.tests.test_events.reply_added" handler="plone.app.discussion.tests.test_events.reply_added"
/> />
<subscriber
for="plone.app.discussion.interfaces.IComment
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler="plone.app.discussion.tests.test_events.reply_modified"
/>
<subscriber <subscriber
for="OFS.interfaces.ISimpleItem for="OFS.interfaces.ISimpleItem
plone.app.discussion.interfaces.IReplyRemovedEvent" plone.app.discussion.interfaces.IReplyRemovedEvent"
@ -163,6 +199,26 @@ class RepliesEventsTest(unittest.TestCase):
self.assertTrue(self.registry.replyAdded) self.assertTrue(self.registry.replyAdded)
def test_modifyEvent(self):
self.assertFalse(self.registry.replyModified)
conversation = IConversation(self.document)
replies = IReplies(conversation)
comment = createObject('plone.Comment')
comment.text = 'Comment text'
comment_id = replies.addComment(comment)
comment = self.document.restrictedTraverse(
'++conversation++default/{0}'.format(comment_id),
)
re_comment = createObject('plone.Comment')
re_comment.text = 'Comment text'
replies = IReplies(comment)
new_id = replies.addComment(re_comment)
reply = replies[new_id]
reply.text = "Another text"
notify(ObjectModifiedEvent(reply))
self.assertTrue(self.registry.replyModified)
def test_removedEvent(self): def test_removedEvent(self):
self.assertFalse(self.registry.replyRemoved) self.assertFalse(self.registry.replyRemoved)