def test_delete_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # make sure the comment has been added self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments, 1) # delete the comment we just created del conversation[new_id] # make sure there is no comment left in the conversation self.assertEqual(len(list(conversation.getComments())), 0) self.assertEqual(len(tuple(conversation.getThreads())), 0) self.assertEqual(conversation.total_comments, 0)
def test_delete_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # make sure the comment has been added self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments, 1) # delete the comment we just created del conversation[new_id] # make sure there is no comment left in the conversation self.assertEqual(len(list(conversation.getComments())), 0) self.assertEqual(len(tuple(conversation.getThreads())), 0) self.assertEqual(conversation.total_comments, 0)
def plumi31to311(context, logger=None): catalog = getToolByName(context, 'portal_catalog') commenttool = queryUtility(ICommentingTool) # Reindex comments videos = catalog(portal_type='PlumiVideo') comments = 0 for video in videos: obj = video.getObject() conversation = IConversation(obj) for r in conversation.getThreads(): comment_obj = r['comment'] commenttool.reindexObject(comment_obj) comments = comments + 1 print str(comments) + 'comments updated in videos' callouts = catalog(portal_type='PlumiCallOut') comments = 0 for callout in callouts: obj = callout.getObject() conversation = IConversation(obj) for r in conversation.getThreads(): comment_obj = r['comment'] commenttool.reindexObject(comment_obj) comments = comments + 1 print str(comments) + 'comments updated in callouts' news = catalog(portal_type='News Item') comments = 0 for news_item in news: obj = news_item.getObject() conversation = IConversation(obj) for r in conversation.getThreads(): comment_obj = r['comment'] commenttool.reindexObject(comment_obj) comments = comments + 1 print str(comments) + 'comments updated in news' events = catalog(portal_type='Event') comments = 0 for event in events: obj = event.getObject() conversation = IConversation(obj) for r in conversation.getThreads(): comment_obj = r['comment'] commenttool.reindexObject(comment_obj) comments = comments + 1 print str(comments) + 'comments updated in events'
def test_add_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # Check that the conversation methods return the correct data self.assertTrue(isinstance(comment.comment_id, long)) self.assertTrue(IComment.providedBy(conversation[new_id])) self.assertEqual( aq_base(conversation[new_id].__parent__), aq_base(conversation) ) self.assertEqual(new_id, comment.comment_id) self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments(), 1) self.assertTrue( conversation.last_comment_date - datetime.utcnow() < timedelta(seconds=1) )
def test_add_comment(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) # Add a comment. Note: in real life, we always create comments via the # factory to allow different factories to be swapped in comment = createObject('plone.Comment') comment.text = 'Comment text' new_id = conversation.addComment(comment) # Check that the conversation methods return the correct data self.assertTrue(isinstance(comment.comment_id, int)) self.assertTrue(IComment.providedBy(conversation[new_id])) self.assertEqual( aq_base(conversation[new_id].__parent__), aq_base(conversation), ) self.assertEqual(new_id, comment.comment_id) self.assertEqual(len(list(conversation.getComments())), 1) self.assertEqual(len(tuple(conversation.getThreads())), 1) self.assertEqual(conversation.total_comments(), 1) self.assertTrue( conversation.last_comment_date - datetime.utcnow() < timedelta(seconds=1), )
def get_replies(self): """Returns all replies to a content object. """ context = aq_inner(self.context) conversation = IConversation(context, None) wf = api.portal.get_tool('portal_workflow') if len(conversation.objectIds()): replies = False for r in conversation.getThreads(): r = r.copy() # Yield comments for the field if any([ r['comment'].title == self.name, replies and r['comment'].in_reply_to, ]): # List all possible workflow actions r['actions'] = [ a for a in wf.listActionInfos(object=r['comment']) if a['category'] == 'workflow' and a['allowed'] ] r['review_state'] = wf.getInfoFor( # noqa: P001 r['comment'], 'review_state', 'acknowledged', ) # Yield yield r replies = True else: replies = False
def test_delete_recursive(self): # Create a conversation. In this case we doesn't assign it to an # object, as we just want to check the Conversation object API. conversation = IConversation(self.portal.doc1) IReplies(conversation) # Create a nested comment structure: # # Conversation # +- Comment 1 # +- Comment 1_1 # | +- Comment 1_1_1 # +- Comment 1_2 # +- Comment 2 # +- Comment 2_1 # Create all comments comment1 = createObject('plone.Comment') comment1.text = 'Comment text' comment1_1 = createObject('plone.Comment') comment1_1.text = 'Comment text' comment1_1_1 = createObject('plone.Comment') comment1_1_1.text = 'Comment text' comment1_2 = createObject('plone.Comment') comment1_2.text = 'Comment text' comment2 = createObject('plone.Comment') comment2.text = 'Comment text' comment2_1 = createObject('plone.Comment') comment2_1.text = 'Comment text' # Create the nested comment structure new_id_1 = conversation.addComment(comment1) new_id_2 = conversation.addComment(comment2) comment1_1.in_reply_to = new_id_1 new_id_1_1 = conversation.addComment(comment1_1) comment1_1_1.in_reply_to = new_id_1_1 conversation.addComment(comment1_1_1) comment1_2.in_reply_to = new_id_1 conversation.addComment(comment1_2) comment2_1.in_reply_to = new_id_2 new_id_2_1 = conversation.addComment(comment2_1) del conversation[new_id_1] self.assertEqual( [{'comment': comment2, 'depth': 0, 'id': new_id_2}, {'comment': comment2_1, 'depth': 1, 'id': new_id_2_1}, ], list(conversation.getThreads()))
def test_migrate_comment_with_creator(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') reply.author_username = '******' reply.email = '*****@*****.**' self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) self.assertEqual(reply.author_username, 'Jim') self.assertEqual(reply.email, '*****@*****.**') # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc) ) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual( comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6) ) self.assertEqual( comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7) ) self.assertEqual([ {'comment': comment1, 'depth': 0, 'id': long(comment1.id)} ], list(conversation.getThreads())) self.assertFalse(self.doc.talkback) # Though this should be Jimmy, but looks like getProperty won't pick # up 'author_username' (reply.author_username is not None), so it's # propagating Creator()..? self.assertEqual(comment1.author_username, 'Jim') self.assertEqual(comment1.author_name, 'Jimmy Jones') self.assertEqual(comment1.author_email, '*****@*****.**')
def test_migrate_comment_with_creator(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') reply.author_username = '******' reply.email = '*****@*****.**' self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) self.assertEqual(reply.author_username, 'Jim') self.assertEqual(reply.email, '*****@*****.**') # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc)) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual(comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6)) self.assertEqual(comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7)) self.assertEqual([{ 'comment': comment1, 'depth': 0, 'id': long(comment1.id) }], list(conversation.getThreads())) self.assertFalse(self.doc.talkback) # Though this should be Jimmy, but looks like getProperty won't pick # up 'author_username' (reply.author_username is not None), so it's # propagating Creator()..? self.assertEqual(comment1.author_username, 'Jim') self.assertEqual(comment1.author_name, 'Jimmy Jones') self.assertEqual(comment1.author_email, '*****@*****.**')
def test_delete_comment_when_content_object_is_deleted(self): # Make sure all comments of a content object are deleted when the # object itself is deleted. conversation = IConversation(self.portal.doc1) comment = createObject('plone.Comment') comment.text = 'Comment text' conversation.addComment(comment) # Delete the content object self.portal.manage_delObjects(['doc1']) # Make sure the comment has been deleted as well self.assertEqual(len(list(conversation.getComments())), 0) self.assertEqual(len(tuple(conversation.getThreads())), 0) self.assertEqual(conversation.total_comments, 0)
def test_delete_comment_when_content_object_is_deleted(self): # Make sure all comments of a content object are deleted when the # object itself is deleted. conversation = IConversation(self.portal.doc1) comment = createObject('plone.Comment') comment.text = 'Comment text' conversation.addComment(comment) # Delete the content object self.portal.manage_delObjects(['doc1']) # Make sure the comment has been deleted as well self.assertEqual(len(list(conversation.getComments())), 0) self.assertEqual(len(tuple(conversation.getThreads())), 0) self.assertEqual(conversation.total_comments, 0)
def getCommentsLen(self, item): """ Return the number of comments of the object """ if not item.restrictedTraverse('@@conversation_view').enabled(): return 0 wf = getToolByName(self.context, 'portal_workflow') discussions = IConversation(item) list_discussions = discussions.getThreads() num_discussions = 0 for discuss in list_discussions: comment_obj = discuss['comment'] workflow_status = wf.getInfoFor(comment_obj, 'review_state') if workflow_status == 'published': num_discussions += 1 return num_discussions
def test_migrate_comment(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc) ) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual( comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6) ) self.assertEqual( comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7) ) self.assertEqual([ {'comment': comment1, 'depth': 0, 'id': long(comment1.id)} ], list(conversation.getThreads())) self.assertFalse(self.doc.talkback)
def test_migrate_comment(self): # Create a comment talkback = self.discussion.getDiscussionFor(self.doc) self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') reply = talkback.getReplies()[0] reply.setReplyTo(self.doc) reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT') reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT') self._publish(reply) self.assertEqual(reply.Title(), 'My Title') self.assertEqual(reply.EditableBody(), 'My Text') self.assertTrue('Jim' in reply.listCreators()) self.assertEqual(talkback.replyCount(self.doc), 1) self.assertEqual(reply.inReplyTo(), self.doc) # Call migration script self.view() # Make sure a conversation has been created self.assertTrue( 'plone.app.discussion:conversation' in IAnnotations(self.doc)) conversation = IConversation(self.doc) # Check migration self.assertEqual(conversation.total_comments, 1) self.assertTrue(conversation.getComments().next()) comment1 = conversation.values()[0] self.assertTrue(IComment.providedBy(comment1)) self.assertEqual(comment1.Title(), 'My Title') self.assertEqual(comment1.text, '<p>My Text</p>\n') self.assertEqual(comment1.mime_type, 'text/html') self.assertEqual(comment1.Creator(), 'Jim') self.assertEqual(comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6)) self.assertEqual(comment1.modification_date, datetime(2009, 7, 12, 19, 38, 7)) self.assertEqual([{ 'comment': comment1, 'depth': 0, 'id': long(comment1.id) }], list(conversation.getThreads())) self.assertFalse(self.doc.talkback)
def update(self): show = False num_comments = 0 first_comment = self.context.absolute_url() if safe_hasattr(self.context, 'isDiscussable')\ and self.context.isDiscussable()\ and self.context.absolute_url() not in self.request.get('URL'): conversations = IConversation(self.context) num_comments = conversations.total_comments if num_comments > 0: try: comment_id = conversations.getThreads().next()['id'] first_comment = '%s#%s' % (first_comment, comment_id) except StopIteration: pass show = True self.show = show self.num_comments = num_comments self.first_comment = first_comment
def fix_comments_1001(context): from plone.app.discussion.interfaces import IConversation brains = api.content.find( context=api.portal.get(), total_comments={ "query": [1, 1000], "range": "min:max" }, ) for brain in brains: obj = brain.getObject() conversation = IConversation(obj, None) for thread in conversation.getThreads(): comment = thread["comment"] if comment.author_name == comment.author_email: user = api.user.get(username=comment.author_username) infos = [ user.getProperty("first_name"), user.getProperty("last_name") ] comment.author_name = " ".join([i for i in infos if i])
def test_migrate_nested_comments(self): # Create some nested comments and migrate them # # self.doc # +- First comment # +- Re: First comment # + Re: Re: First comment # + Re: Re: Re: First comment # +- Re: First comment (2) # +- Re: First comment (3) # +- Re: First comment (4) # +- Second comment talkback = self.discussion.getDiscussionFor(self.doc) # First comment talkback.createReply(title='First comment', text='This is my first comment.') comment1 = talkback.getReplies()[0] self._publish(comment1) talkback_comment1 = self.discussion.getDiscussionFor(comment1) # Re: First comment talkback_comment1.createReply(title='Re: First comment', text='This is my first reply.') comment1_1 = talkback_comment1.getReplies()[0] self._publish(comment1_1) talkback_comment1_1 = self.discussion.getDiscussionFor(comment1_1) self.assertEqual(len(talkback.getReplies()), 1) self.assertEqual(len(talkback_comment1.getReplies()), 1) self.assertEqual(len(talkback_comment1_1.getReplies()), 0) #Re: Re: First comment talkback_comment1_1.createReply(title='Re: Re: First comment', text='This is my first re-reply.') comment1_1_1 = talkback_comment1_1.getReplies()[0] self._publish(comment1_1_1) talkback_comment1_1_1 = self.discussion.getDiscussionFor(comment1_1_1) # Re: Re: Re: First comment talkback_comment1_1_1.createReply(title='Re: Re: Re: First comment', text='This is my first re-re-reply.') self._publish(talkback_comment1_1_1.getReplies()[0]) # Re: First comment (2) talkback_comment1.createReply(title='Re: First comment (2)', text='This is my first reply (2).') self._publish(talkback_comment1.getReplies()[1]) # Re: First comment (3) talkback_comment1.createReply(title='Re: First comment (3)', text='This is my first reply (3).') self._publish(talkback_comment1.getReplies()[2]) # Re: First comment (4) talkback_comment1.createReply(title='Re: First comment (4)', text='This is my first reply (4).') self._publish(talkback_comment1.getReplies()[3]) # Second comment talkback.createReply(title='Second comment', text='This is my second comment.') self._publish(talkback.getReplies()[1]) # Call migration script self.view() # Check migration conversation = IConversation(self.doc) self.assertEqual(conversation.total_comments, 8) comment1 = conversation.values()[0] comment1_1 = conversation.values()[1] comment1_1_1 = conversation.values()[2] comment1_1_1_1 = conversation.values()[3] comment1_2 = conversation.values()[4] comment1_3 = conversation.values()[5] comment1_4 = conversation.values()[6] comment2 = conversation.values()[7] self.assertEqual([ {'comment': comment1, 'depth': 0, 'id': long(comment1.id)}, {'comment': comment1_1, 'depth': 1, 'id': long(comment1_1.id)}, {'comment': comment1_1_1, 'depth': 2, 'id': long(comment1_1_1.id)}, {'comment': comment1_1_1_1, 'depth': 3, 'id': long(comment1_1_1_1.id)}, {'comment': comment1_2, 'depth': 1, 'id': long(comment1_2.id)}, {'comment': comment1_3, 'depth': 1, 'id': long(comment1_3.id)}, {'comment': comment1_4, 'depth': 1, 'id': long(comment1_4.id)}, {'comment': comment2, 'depth': 0, 'id': long(comment2.id)}, ], list(conversation.getThreads())) talkback = self.discussion.getDiscussionFor(self.doc) self.assertEqual(len(talkback.getReplies()), 0)
def amount_of_replies(self, brain): obj = brain.getObject() conversation = IConversation(obj) return len([thread for thread in conversation.getThreads()])
def amount_of_replies(self, brain): obj = brain.getObject() conversation = IConversation(obj) return len([thread for thread in conversation.getThreads()])
def comments(self, story): conversation = IConversation(story) return [i for i in conversation.getThreads()]
def test_migrate_nested_comments(self): # Create some nested comments and migrate them # # self.doc # +- First comment # +- Re: First comment # + Re: Re: First comment # + Re: Re: Re: First comment # +- Re: First comment (2) # +- Re: First comment (3) # +- Re: First comment (4) # +- Second comment talkback = self.discussion.getDiscussionFor(self.doc) # First comment talkback.createReply(title='First comment', text='This is my first comment.') comment1 = talkback.getReplies()[0] self._publish(comment1) talkback_comment1 = self.discussion.getDiscussionFor(comment1) # Re: First comment talkback_comment1.createReply(title='Re: First comment', text='This is my first reply.') comment1_1 = talkback_comment1.getReplies()[0] self._publish(comment1_1) talkback_comment1_1 = self.discussion.getDiscussionFor(comment1_1) self.assertEqual(len(talkback.getReplies()), 1) self.assertEqual(len(talkback_comment1.getReplies()), 1) self.assertEqual(len(talkback_comment1_1.getReplies()), 0) #Re: Re: First comment talkback_comment1_1.createReply(title='Re: Re: First comment', text='This is my first re-reply.') comment1_1_1 = talkback_comment1_1.getReplies()[0] self._publish(comment1_1_1) talkback_comment1_1_1 = self.discussion.getDiscussionFor(comment1_1_1) # Re: Re: Re: First comment talkback_comment1_1_1.createReply(title='Re: Re: Re: First comment', text='This is my first re-re-reply.') self._publish(talkback_comment1_1_1.getReplies()[0]) # Re: First comment (2) talkback_comment1.createReply(title='Re: First comment (2)', text='This is my first reply (2).') self._publish(talkback_comment1.getReplies()[1]) # Re: First comment (3) talkback_comment1.createReply(title='Re: First comment (3)', text='This is my first reply (3).') self._publish(talkback_comment1.getReplies()[2]) # Re: First comment (4) talkback_comment1.createReply(title='Re: First comment (4)', text='This is my first reply (4).') self._publish(talkback_comment1.getReplies()[3]) # Second comment talkback.createReply(title='Second comment', text='This is my second comment.') self._publish(talkback.getReplies()[1]) # Call migration script self.view() # Check migration conversation = IConversation(self.doc) self.assertEqual(conversation.total_comments, 8) comment1 = conversation.values()[0] comment1_1 = conversation.values()[1] comment1_1_1 = conversation.values()[2] comment1_1_1_1 = conversation.values()[3] comment1_2 = conversation.values()[4] comment1_3 = conversation.values()[5] comment1_4 = conversation.values()[6] comment2 = conversation.values()[7] self.assertEqual([ { 'comment': comment1, 'depth': 0, 'id': long(comment1.id) }, { 'comment': comment1_1, 'depth': 1, 'id': long(comment1_1.id) }, { 'comment': comment1_1_1, 'depth': 2, 'id': long(comment1_1_1.id) }, { 'comment': comment1_1_1_1, 'depth': 3, 'id': long(comment1_1_1_1.id) }, { 'comment': comment1_2, 'depth': 1, 'id': long(comment1_2.id) }, { 'comment': comment1_3, 'depth': 1, 'id': long(comment1_3.id) }, { 'comment': comment1_4, 'depth': 1, 'id': long(comment1_4.id) }, { 'comment': comment2, 'depth': 0, 'id': long(comment2.id) }, ], list(conversation.getThreads())) talkback = self.discussion.getDiscussionFor(self.doc) self.assertEqual(len(talkback.getReplies()), 0)