2009-05-26 09:25:14 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2009-05-28 13:39:36 +02:00
|
|
|
from urllib import quote as url_quote
|
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
from zope.interface import implements
|
2009-05-28 08:08:55 +02:00
|
|
|
|
2009-06-08 10:00:15 +02:00
|
|
|
from zope.component import createObject, getMultiAdapter, queryUtility
|
2009-05-28 08:08:55 +02:00
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
from zope.viewlet.interfaces import IViewlet
|
2009-05-22 19:30:52 +02:00
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
from Acquisition import aq_inner, aq_parent, aq_base
|
2009-05-28 08:08:55 +02:00
|
|
|
|
2009-05-28 08:35:47 +02:00
|
|
|
from AccessControl import getSecurityManager
|
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
from Products.Five.browser import BrowserView
|
2009-05-22 19:30:52 +02:00
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
|
|
|
|
|
2009-05-28 08:08:55 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
|
2009-06-08 10:00:15 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
2009-05-28 13:39:36 +02:00
|
|
|
from plone.app.layout.viewlets.common import ViewletBase
|
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation, IComment, IReplies, IDiscussionSettings
|
2009-05-25 20:59:25 +02:00
|
|
|
from plone.app.discussion.conversation import conversationAdapterFactory
|
2009-05-22 19:30:52 +02:00
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
from plone.app.discussion.comment import CommentFactory
|
|
|
|
|
|
|
|
|
2009-05-27 15:23:25 +02:00
|
|
|
class View(BrowserView):
|
|
|
|
"""Comment View
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
# Redirect from /path/to/object/++conversation++default/123456789
|
|
|
|
# to /path/to/object#comment-123456789
|
|
|
|
comment_id = aq_parent(self).id
|
|
|
|
self.request.response.redirect(aq_parent(aq_parent(aq_parent(self))).absolute_url() + '#comment-' + comment_id)
|
|
|
|
|
2009-05-28 13:39:36 +02:00
|
|
|
class CommentsViewlet(ViewletBase):
|
2009-05-25 20:59:25 +02:00
|
|
|
"""Discussion Viewlet
|
|
|
|
"""
|
|
|
|
|
|
|
|
implements(IViewlet)
|
|
|
|
|
|
|
|
template = ViewPageTemplateFile('comments.pt')
|
|
|
|
|
2009-05-22 19:30:52 +02:00
|
|
|
def update(self):
|
2009-05-28 08:35:47 +02:00
|
|
|
super(CommentsViewlet, self).update()
|
|
|
|
self.portal_discussion = getToolByName(self.context, 'portal_discussion', None)
|
|
|
|
self.portal_membership = getToolByName(self.context, 'portal_membership', None)
|
|
|
|
|
|
|
|
def can_reply(self):
|
|
|
|
return getSecurityManager().checkPermission('Reply to item', aq_inner(self.context))
|
|
|
|
|
2009-06-11 15:51:33 +02:00
|
|
|
def can_manage(self):
|
|
|
|
return getSecurityManager().checkPermission('Manage portal', aq_inner(self.context))
|
|
|
|
|
2009-05-28 08:35:47 +02:00
|
|
|
def is_discussion_allowed(self):
|
2009-06-11 12:20:44 +02:00
|
|
|
conversation = conversationAdapterFactory(self.context)
|
|
|
|
return conversation.enabled
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
def get_replies(self, workflow_actions=False):
|
2009-05-28 17:08:36 +02:00
|
|
|
|
2009-06-13 20:02:59 +02:00
|
|
|
# Acquisition wrap the conversation
|
2009-06-13 18:46:37 +02:00
|
|
|
context = aq_inner(self.context)
|
|
|
|
conversation = IConversation(context)
|
|
|
|
conversation = IConversation(context).__of__(context)
|
|
|
|
|
2009-06-13 20:02:59 +02:00
|
|
|
def replies_with_workflow_actions():
|
|
|
|
# Return dict with workflow actions
|
|
|
|
#context = aq_inner(self.context)
|
|
|
|
wf = getToolByName(context, 'portal_workflow')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
# Return all direct replies
|
2009-05-28 17:08:36 +02:00
|
|
|
if conversation.total_comments > 0:
|
2009-06-13 18:46:37 +02:00
|
|
|
if workflow_actions:
|
2009-06-13 20:02:59 +02:00
|
|
|
return replies_with_workflow_actions()
|
2009-06-13 18:46:37 +02:00
|
|
|
else:
|
2009-06-13 20:02:59 +02:00
|
|
|
return conversation.getThreads()
|
2009-05-28 17:08:36 +02:00
|
|
|
else:
|
2009-06-13 20:02:59 +02:00
|
|
|
return None
|
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2009-06-07 22:58:41 +02:00
|
|
|
def get_commenter_home_url(self, username):
|
|
|
|
if username is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return "%s/author/%s" % (self.context.portal_url(), username)
|
|
|
|
|
|
|
|
def get_commenter_portrait(self, username):
|
|
|
|
|
|
|
|
if username is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self.portal_membership.getPersonalPortrait(username);
|
|
|
|
|
2009-06-08 10:00:15 +02:00
|
|
|
def anonymous_discussion_allowed(self):
|
|
|
|
# Check if anonymous comments are allowed in the registry
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.for_interface(IDiscussionSettings)
|
|
|
|
return settings.anonymous_comments
|
|
|
|
|
2009-06-08 10:23:18 +02:00
|
|
|
def show_commenter_image(self):
|
|
|
|
# Check if showing commenter image is enabled in the registry
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.for_interface(IDiscussionSettings)
|
|
|
|
return settings.show_commenter_image
|
|
|
|
|
2009-05-28 08:35:47 +02:00
|
|
|
def is_anonymous(self):
|
|
|
|
return self.portal_state.anonymous()
|
|
|
|
|
2009-05-28 13:39:36 +02:00
|
|
|
def login_action(self):
|
|
|
|
return '%s/login_form?came_from=%s' % (self.navigation_root_url, url_quote(self.request.get('URL', '')),)
|
2009-05-28 08:35:47 +02:00
|
|
|
|
2009-05-26 09:25:14 +02:00
|
|
|
def format_time(self, time):
|
2009-05-31 13:01:36 +02:00
|
|
|
# Todo: return localized time
|
|
|
|
return time.strftime("%a, %d %b %Y %H:%M")
|
|
|
|
# XXX: Not working, returns None !!!
|
|
|
|
#return self.context.restrictedTraverse('@@plone').toLocalizedTime(time, long_format=True)
|
2009-06-07 22:58:41 +02:00
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
class AddComment(BrowserView):
|
|
|
|
"""Add a comment to a conversation
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
|
|
|
if self.request.has_key('form.button.AddComment'):
|
|
|
|
|
|
|
|
subject = self.request.get('subject')
|
|
|
|
text = self.request.get('body_text')
|
2009-06-08 10:00:15 +02:00
|
|
|
author_username = self.request.get('author_username')
|
|
|
|
author_email = self.request.get('author_email')
|
2009-05-25 20:59:25 +02:00
|
|
|
|
|
|
|
# The add-comment view is called on the conversation object
|
|
|
|
conversation = self.context
|
|
|
|
|
|
|
|
# Create the comment
|
|
|
|
comment = CommentFactory()
|
|
|
|
comment.title = subject
|
|
|
|
comment.text = text
|
|
|
|
|
2009-05-28 08:08:55 +02:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
|
|
|
|
|
|
|
if portal_membership.isAnonymousUser():
|
2009-06-08 10:00:15 +02:00
|
|
|
comment.creator = author_username
|
|
|
|
comment.author_name = author_username
|
|
|
|
comment.author_email = author_email
|
|
|
|
comment.creation_date = comment.modification_date = datetime.now()
|
2009-05-28 08:08:55 +02:00
|
|
|
else:
|
|
|
|
member = portal_membership.getAuthenticatedMember()
|
|
|
|
comment.creator = member.getProperty('fullname')
|
|
|
|
comment.author_username = member.getUserName()
|
|
|
|
comment.author_name = member.getProperty('fullname')
|
|
|
|
comment.author_email = member.getProperty('email')
|
|
|
|
comment.creation_date = comment.modification_date = datetime.now()
|
|
|
|
|
2009-05-25 20:59:25 +02:00
|
|
|
# Add comment to the conversation
|
2009-05-28 08:08:55 +02:00
|
|
|
comment_id = conversation.addComment(comment)
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2009-05-28 08:11:09 +02:00
|
|
|
# Redirect to comment (inside a content object page)
|
|
|
|
self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url() + '#comment-' + str(comment_id))
|
2009-05-26 21:58:33 +02:00
|
|
|
|
|
|
|
class ReplyToComment(BrowserView):
|
|
|
|
"""Reply to a comment
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
|
|
|
if self.request.has_key('form.button.AddComment'):
|
|
|
|
|
|
|
|
reply_to_comment_id = self.request.get('form.reply_to_comment_id')
|
|
|
|
subject = self.request.get('subject')
|
|
|
|
text = self.request.get('body_text')
|
|
|
|
|
|
|
|
# The add-comment view is called on the conversation object
|
|
|
|
conversation = self.context
|
|
|
|
|
|
|
|
# Fetch the comment we want to reply to
|
|
|
|
comment_to_reply_to = conversation.get(reply_to_comment_id)
|
|
|
|
|
|
|
|
replies = IReplies(comment_to_reply_to)
|
|
|
|
|
|
|
|
# Create the comment
|
|
|
|
comment = CommentFactory()
|
|
|
|
comment.title = subject
|
|
|
|
comment.text = text
|
|
|
|
|
|
|
|
# Add the reply to the comment
|
|
|
|
new_re_id = replies.addComment(comment)
|
|
|
|
|
2009-05-28 08:11:09 +02:00
|
|
|
# Redirect to comment (inside a content object page)
|
2009-05-28 17:16:19 +02:00
|
|
|
self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url() + '#comment-' + str(reply_to_comment_id))
|
2009-06-11 12:20:44 +02:00
|
|
|
|
|
|
|
class DeleteComment(BrowserView):
|
|
|
|
"""Delete a comment from a conversation
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
|
|
|
comment = aq_inner(self.context)
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
comment_id = self.request.form['comment_id']
|
|
|
|
if comment_id != comment.getId():
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
conversation = self.context.__parent__
|
|
|
|
|
|
|
|
del conversation[comment_id]
|
|
|
|
|
|
|
|
class PublishComment(BrowserView):
|
|
|
|
"""Publish a comment
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
|
|
|
comment = aq_inner(self.context)
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
comment_id = self.request.form['comment_id']
|
|
|
|
if comment_id != comment.getId():
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
action = self.request.form['action']
|
|
|
|
portal_workflow = getToolByName(comment, 'portal_workflow')
|
|
|
|
portal_workflow.doActionFor(comment, action)
|