def comment_data(abstract): """Helper function returning the number and concatenated text of comments made on 'abstract'""" # try old API first try: reply_count = abstract.portal_discussion.getDiscussionFor(abstract).replyCount(abstract) replies = abstract.portal_discussion.getDiscussionFor(abstract).getReplies() if not replies: return 0, 'None' comments = [] for r in replies: comments.append("%s (%s): %s - %s" % (r.Creator(), r.created().Date(), r.Title(), r.CookedBody())) comments = " -- ".join(comments) return reply_count, comments except AttributeError: # usually happens if plone.app.discussion is used # then the following should work from plone.app.discussion.interfaces import IConversation conversation = IConversation(abstract) reply_count = len(conversation.objectIds()) if not reply_count: return 0, 'None' replies = [conversation[id] for id in conversation.objectIds()] comments = [] for r in replies: comments.append("%s (%s): %s" % (r.author_name, r.modification_date.strftime('%Y-%m-%d %H:%M'), r.text)) comments = " -- ".join(comments) return reply_count, comments.encode('utf-8')
def getComments(self, workflow_actions=True): """Returns all replies to a content object. If workflow_actions is false, only published comments are returned. If workflow actions is true, comments are returned with workflow actions. """ context = aq_inner(self.context) conversation = IConversation(context, None) if conversation is None: return iter([]) wf = getToolByName(context, 'portal_workflow') # workflow_actions is only true when user # has 'Manage portal' permission def replies_with_workflow_actions(): # Generator that returns replies dict with workflow actions for r in conversation.getThreads(): comment_obj = r['comment'] # list all possible workflow actions actions = [ a for a in wf.listActionInfos(object=comment_obj) if a['category'] == 'workflow' and a['allowed'] ] r = r.copy() r['actions'] = actions yield r def published_replies(): # Generator that returns replies dict with workflow status. for r in conversation.getThreads(): comment_obj = r['comment'] workflow_status = wf.getInfoFor(comment_obj, 'review_state') if workflow_status == 'published': r = r.copy() r['workflow_status'] = workflow_status yield r # Return all direct replies lenvalue = len(conversation.objectIds()) if len(conversation.objectIds()): if workflow_actions: return replies_with_workflow_actions() else: return published_replies()
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 get_replies(self, workflow_actions=False): """Returns all replies to a content object. If workflow_actions is false, comments are returned without workflow actions. If workflow actions is true, comments are returned with workflow actions. """ context = aq_inner(self.context) conversation = IConversation(context, None) if conversation is None: return iter([]) wf = getToolByName(context, 'portal_workflow') pm = getToolByName(context, 'portal_membership') # workflow_actions is only true when user # has 'Manage portal' permission def replies_with_workflow_actions(): """ Generator that returns replies dict with workflow actions """ for r in conversation.getThreads(): comment_obj = r['comment'] # list all possible workflow actions actions = [ a for a in wf.listActionInfos(object=comment_obj) if a['category'] == 'workflow' and a['allowed'] ] r = r.copy() r['actions'] = actions yield r def published_replies(): """ Generator that returns replies dict with workflow status. """ for r in conversation.getThreads(): comment_obj = r['comment'] workflow_status = wf.getInfoFor(comment_obj, 'review_state') if pm.checkPermission('View', comment_obj): r = r.copy() r['workflow_status'] = workflow_status yield r # Return all direct replies if len(conversation.objectIds()): if workflow_actions: return replies_with_workflow_actions() else: return published_replies()
def get_replies(self, workflow_actions=False): context = aq_inner(self.context) conversation = IConversation(context) wf = getToolByName(context, 'portal_workflow') def replies_with_workflow_actions(): # Generator that returns replies dict with workflow actions for comment_obj in conversation.getComments(): # list all possible workflow actions actions = [a for a in wf.listActionInfos(object=comment_obj) if a['category'] == 'workflow' and a['allowed']] yield {'comment': comment_obj, 'actions': actions} if len(conversation.objectIds()) > 0: return replies_with_workflow_actions()
def actual_comment_count(self): """Count the actual comments on this context, not the comments in the catalog. """ context = aq_inner(self.context) if IConversation is not None: conversation = IConversation(context, None) if conversation is not None: return len(conversation.objectIds()) portal_discussion = getToolByName(context, 'portal_discussion') try: talkback = portal_discussion.getDiscussionFor(context) except DiscussionNotAllowed: # Try anyway: if not hasattr(aq_base(context), 'talkback'): return 0 talkback = getattr(context, 'talkback') return len(talkback.objectIds())
def get_replies(self, workflow_actions=False): context = aq_inner(self.context) conversation = IConversation(context) wf = getToolByName(context, 'portal_workflow') def replies_with_workflow_actions(): # Generator that returns replies dict with workflow actions for comment_obj in conversation.getComments(): # list all possible workflow actions actions = [ a for a in wf.listActionInfos(object=comment_obj) if a['category'] == 'workflow' and a['allowed'] ] yield {'comment': comment_obj, 'actions': actions} if len(conversation.objectIds()) > 0: return replies_with_workflow_actions()
def hasComments(self): """ """ conversation = IConversation(self) return len(conversation.objectIds()) > 0