2010-07-13 11:19:20 +02:00
|
|
|
from Acquisition import aq_inner
|
2009-08-04 21:36:33 +02:00
|
|
|
|
|
|
|
from AccessControl import getSecurityManager
|
2009-08-01 23:47:50 +02:00
|
|
|
|
2009-05-26 09:25:14 +02:00
|
|
|
from datetime import datetime
|
2009-06-16 22:53:45 +02:00
|
|
|
from DateTime import DateTime
|
2009-05-26 09:25:14 +02:00
|
|
|
|
2009-05-28 13:39:36 +02:00
|
|
|
from urllib import quote as url_quote
|
|
|
|
|
2010-07-13 11:19:20 +02:00
|
|
|
from zope.component import createObject, queryUtility
|
2009-08-04 21:36:33 +02:00
|
|
|
|
2010-07-13 11:19:20 +02:00
|
|
|
from zope.interface import alsoProvides
|
2009-05-28 08:08:55 +02:00
|
|
|
|
2010-07-13 11:19:20 +02:00
|
|
|
from z3c.form import form, field, button, interfaces
|
2010-02-16 21:25:56 +01:00
|
|
|
from z3c.form.interfaces import IFormLayer
|
2009-05-28 08:35:47 +02:00
|
|
|
|
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-07-03 14:06:19 +02:00
|
|
|
from Products.statusmessages.interfaces import IStatusMessage
|
2009-06-21 09:48:33 +02:00
|
|
|
|
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
|
|
|
|
|
2010-07-13 11:19:20 +02:00
|
|
|
from plone.app.discussion.interfaces import _
|
|
|
|
from plone.app.discussion.interfaces import IConversation
|
|
|
|
from plone.app.discussion.interfaces import IComment
|
|
|
|
from plone.app.discussion.interfaces import IReplies
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
|
|
|
from plone.app.discussion.interfaces import ICaptcha
|
2009-08-13 21:21:52 +02:00
|
|
|
|
2009-08-15 12:21:26 +02:00
|
|
|
from plone.app.discussion.browser.validator import CaptchaValidator
|
2009-05-22 19:30:52 +02:00
|
|
|
|
2010-07-13 11:19:20 +02:00
|
|
|
from plone.z3cform import z2
|
2009-08-01 23:47:50 +02:00
|
|
|
from plone.z3cform.fieldsets import extensible
|
|
|
|
|
2010-05-31 11:50:46 +02:00
|
|
|
# starting from 0.6.0 version plone.z3cform has IWrappedForm interface
|
|
|
|
try:
|
|
|
|
from plone.z3cform.interfaces import IWrappedForm
|
|
|
|
HAS_WRAPPED_FORM = True
|
|
|
|
except ImportError:
|
2010-05-31 12:05:57 +02:00
|
|
|
HAS_WRAPPED_FORM = False
|
2009-08-28 15:03:16 +02:00
|
|
|
|
2009-08-01 23:47:50 +02:00
|
|
|
class CommentForm(extensible.ExtensibleForm, form.Form):
|
2009-08-02 19:32:41 +02:00
|
|
|
|
2009-08-01 23:47:50 +02:00
|
|
|
ignoreContext = True # don't use context to get widget data
|
2009-08-04 21:24:47 +02:00
|
|
|
label = _(u"Add a comment")
|
2009-08-05 11:01:14 +02:00
|
|
|
fields = field.Fields(IComment).omit('portal_type',
|
|
|
|
'__parent__',
|
|
|
|
'__name__',
|
|
|
|
'comment_id',
|
|
|
|
'mime_type',
|
|
|
|
'creator',
|
|
|
|
'creation_date',
|
|
|
|
'modification_date',
|
|
|
|
'author_username')
|
2009-08-04 13:35:05 +02:00
|
|
|
|
2010-02-08 13:02:54 +01:00
|
|
|
def updateFields(self):
|
2010-02-08 19:28:03 +01:00
|
|
|
super(CommentForm, self).updateFields()
|
2010-03-17 11:49:26 +01:00
|
|
|
#self.fields['author_notification'].widgetFactory = SingleCheckBoxFieldWidget
|
2010-02-08 19:28:03 +01:00
|
|
|
|
2009-08-04 17:12:15 +02:00
|
|
|
def updateWidgets(self):
|
|
|
|
super(CommentForm, self).updateWidgets()
|
2010-02-08 19:28:03 +01:00
|
|
|
|
|
|
|
# Widgets
|
2009-08-04 17:12:15 +02:00
|
|
|
self.widgets['in_reply_to'].mode = interfaces.HIDDEN_MODE
|
2010-01-18 20:47:46 +01:00
|
|
|
self.widgets['text'].addClass("autoresize")
|
2010-03-11 20:23:02 +01:00
|
|
|
#self.widgets['author_notification'].label = _(u"")
|
2010-02-08 19:28:03 +01:00
|
|
|
|
|
|
|
# Anonymous / Logged-in
|
2010-02-08 16:02:17 +01:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
2009-08-05 11:01:14 +02:00
|
|
|
if not portal_membership.isAnonymousUser():
|
|
|
|
self.widgets['author_name'].mode = interfaces.HIDDEN_MODE
|
|
|
|
self.widgets['author_email'].mode = interfaces.HIDDEN_MODE
|
2010-02-08 19:28:03 +01:00
|
|
|
|
2010-03-16 14:24:13 +01:00
|
|
|
# XXX: Since we are not using the author_email field in the
|
|
|
|
# current state, we hide it by default. But we keep the field for
|
|
|
|
# integrators or later use.
|
|
|
|
self.widgets['author_email'].mode = interfaces.HIDDEN_MODE
|
|
|
|
|
|
|
|
# XXX: Author notification code
|
2010-07-12 15:47:53 +02:00
|
|
|
#registry = queryUtility(IRegistry)
|
2010-03-11 20:23:02 +01:00
|
|
|
#settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
#if not settings.user_notification_enabled:
|
|
|
|
# self.widgets['author_notification'].mode = interfaces.HIDDEN_MODE
|
2010-01-27 20:20:25 +01:00
|
|
|
|
|
|
|
def updateActions(self):
|
|
|
|
super(CommentForm, self).updateActions()
|
|
|
|
self.actions['cancel'].addClass("standalone")
|
|
|
|
self.actions['cancel'].addClass("hide")
|
|
|
|
self.actions['comment'].addClass("context")
|
|
|
|
|
2010-01-24 15:00:51 +01:00
|
|
|
@button.buttonAndHandler(_(u"add_comment_button",default=u"Comment"), name='comment')
|
2009-08-04 22:47:25 +02:00
|
|
|
def handleComment(self, action):
|
2009-10-17 11:17:13 +02:00
|
|
|
context = aq_inner(self.context)
|
|
|
|
wf = getToolByName(context, 'portal_workflow')
|
2010-06-17 20:56:26 +02:00
|
|
|
|
2009-08-01 23:47:50 +02:00
|
|
|
data, errors = self.extractData()
|
2010-06-17 20:56:26 +02:00
|
|
|
if errors:
|
|
|
|
return
|
2009-08-28 15:12:14 +02:00
|
|
|
|
2009-08-28 15:03:16 +02:00
|
|
|
title = u""
|
|
|
|
text = u""
|
|
|
|
author_name = u""
|
|
|
|
author_username = u""
|
|
|
|
author_email = u""
|
2010-02-08 19:28:03 +01:00
|
|
|
author_notification = None
|
2009-08-04 13:56:52 +02:00
|
|
|
|
2009-11-01 10:24:27 +01:00
|
|
|
# Captcha check for anonymous users (if Captcha is enabled)
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2009-08-13 21:21:52 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
2009-08-15 13:35:54 +02:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
|
|
|
if settings.captcha != 'disabled' and portal_membership.isAnonymousUser():
|
2009-08-24 20:09:32 +02:00
|
|
|
if 'captcha' in data:
|
2009-11-01 10:24:27 +01:00
|
|
|
# Check Captcha only if there is a value, otherwise
|
2009-08-13 21:21:52 +02:00
|
|
|
# the default "required" validator is sufficient.
|
2010-06-10 12:51:41 +02:00
|
|
|
captcha = CaptchaValidator(self.context,
|
|
|
|
self.request,
|
|
|
|
None,
|
|
|
|
ICaptcha['captcha'],
|
|
|
|
None)
|
2009-08-13 21:21:52 +02:00
|
|
|
captcha.validate(data['captcha'])
|
|
|
|
else:
|
|
|
|
return
|
2009-08-12 22:45:47 +02:00
|
|
|
|
2010-06-17 20:56:26 +02:00
|
|
|
if 'title' in data:
|
2009-08-04 16:51:34 +02:00
|
|
|
title = data['title']
|
2010-06-17 20:56:26 +02:00
|
|
|
if 'text' in data:
|
2009-08-04 13:56:52 +02:00
|
|
|
text = data['text']
|
2010-06-17 20:56:26 +02:00
|
|
|
if 'author_name' in data:
|
|
|
|
author_name = data['author_name']
|
|
|
|
if 'author_username' in data:
|
|
|
|
author_username = data['author_username']
|
|
|
|
if 'author_email' in data:
|
|
|
|
author_email = data['author_email']
|
|
|
|
if 'author_notification' in data:
|
|
|
|
author_notification = data['author_notification']
|
|
|
|
|
|
|
|
# The add-comment view is called on the conversation object
|
|
|
|
conversation = IConversation(self.__parent__)
|
|
|
|
|
|
|
|
if data['in_reply_to']:
|
|
|
|
# Fetch the comment we want to reply to
|
|
|
|
conversation_to_reply_to = conversation.get(data['in_reply_to'])
|
|
|
|
replies = IReplies(conversation_to_reply_to)
|
|
|
|
|
|
|
|
# Create the comment
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.title = title
|
|
|
|
comment.text = text
|
2009-08-04 16:51:34 +02:00
|
|
|
|
2010-06-17 20:56:26 +02:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
|
|
|
|
|
|
|
if portal_membership.isAnonymousUser():
|
|
|
|
comment.creator = None
|
|
|
|
comment.author_name = author_name
|
|
|
|
comment.author_email = author_email
|
|
|
|
#comment.author_notification = author_notification
|
|
|
|
comment.creation_date = comment.modification_date = datetime.now()
|
|
|
|
else:
|
|
|
|
member = portal_membership.getAuthenticatedMember()
|
|
|
|
comment.creator = member.id
|
|
|
|
comment.author_username = member.getUserName()
|
|
|
|
comment.author_name = member.getProperty('fullname')
|
|
|
|
comment.author_email = member.getProperty('email')
|
|
|
|
#comment.author_notification = comment.author_notification
|
|
|
|
comment.creation_date = comment.modification_date = datetime.now()
|
|
|
|
|
|
|
|
# Check if the added comment is a reply to an existing comment
|
|
|
|
# or just a regular reply to the content object.
|
|
|
|
if data['in_reply_to']:
|
|
|
|
# Add a reply to an existing comment
|
|
|
|
comment_id = replies.addComment(comment)
|
|
|
|
else:
|
|
|
|
# Add a comment to the conversation
|
|
|
|
comment_id = conversation.addComment(comment)
|
|
|
|
|
|
|
|
# If a user post a comment and moderation is enabled, a message is shown
|
|
|
|
# to the user that his/her comment awaits moderation. If the user has manage
|
|
|
|
# right, he/she is redirected directly to the comment.
|
|
|
|
can_manage = getSecurityManager().checkPermission('Manage portal', context)
|
|
|
|
if wf.getChainForPortalType('Discussion Item') == \
|
|
|
|
('comment_review_workflow',) and not can_manage:
|
|
|
|
# Show info message when comment moderation is enabled
|
|
|
|
IStatusMessage(self.context.REQUEST).addStatusMessage(
|
|
|
|
_("Your comment awaits moderator approval."),
|
|
|
|
type="info")
|
|
|
|
self.request.response.redirect(context.absolute_url())
|
|
|
|
else:
|
|
|
|
# Redirect to comment (inside a content object page)
|
|
|
|
self.request.response.redirect(context.absolute_url() + '#' + str(comment_id))
|
2009-08-04 16:51:34 +02:00
|
|
|
|
2009-08-04 21:24:47 +02:00
|
|
|
@button.buttonAndHandler(_(u"Cancel"))
|
|
|
|
def handleCancel(self, action):
|
|
|
|
# This method should never be called, it's only there to show
|
|
|
|
# a cancel button that is handled by a jQuery method.
|
|
|
|
pass
|
2009-08-01 23:47:50 +02:00
|
|
|
|
2010-01-18 10:54:27 +01:00
|
|
|
|
2010-02-16 21:25:56 +01:00
|
|
|
class CommentsViewlet(ViewletBase):
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2009-08-01 23:47:50 +02:00
|
|
|
form = CommentForm
|
2009-08-20 04:09:53 +02:00
|
|
|
index = ViewPageTemplateFile('comments.pt')
|
2009-08-01 23:47:50 +02:00
|
|
|
|
2010-02-16 21:25:56 +01:00
|
|
|
def update(self):
|
|
|
|
super(CommentsViewlet, self).update()
|
|
|
|
z2.switch_on(self, request_layer=IFormLayer)
|
|
|
|
self.form = CommentForm(aq_inner(self.context), self.request)
|
2010-05-31 11:50:46 +02:00
|
|
|
if HAS_WRAPPED_FORM:
|
|
|
|
alsoProvides(self.form, IWrappedForm)
|
2010-02-16 21:25:56 +01:00
|
|
|
self.form.update()
|
2009-08-01 23:47:50 +02:00
|
|
|
|
|
|
|
# view methods
|
|
|
|
|
2009-05-28 08:35:47 +02:00
|
|
|
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-18 22:57:47 +02:00
|
|
|
context = aq_inner(self.context)
|
2009-06-19 12:00:31 +02:00
|
|
|
conversation = IConversation(context)
|
2009-08-28 13:39:02 +02:00
|
|
|
return conversation.enabled()
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2009-10-07 22:07:55 +02:00
|
|
|
def has_replies(self, workflow_actions=False):
|
|
|
|
"""Returns true if there are replies.
|
|
|
|
"""
|
2010-01-18 20:47:46 +01:00
|
|
|
if self.get_replies(workflow_actions):
|
2010-01-22 20:02:41 +01:00
|
|
|
try:
|
|
|
|
self.get_replies(workflow_actions).next()
|
|
|
|
return True
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
|
|
|
return False
|
2009-10-07 22:07:55 +02:00
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
def get_replies(self, workflow_actions=False):
|
2009-10-07 22:07:55 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2009-06-13 18:46:37 +02:00
|
|
|
context = aq_inner(self.context)
|
|
|
|
conversation = IConversation(context)
|
|
|
|
|
2009-10-07 22:07:55 +02:00
|
|
|
wf = getToolByName(context, 'portal_workflow')
|
2009-06-13 20:02:59 +02:00
|
|
|
|
2009-10-07 22:07:55 +02:00
|
|
|
# workflow_actions is only true when user
|
|
|
|
# has 'Manage portal' permission
|
|
|
|
|
|
|
|
def replies_with_workflow_actions():
|
|
|
|
# Generator that returns replies dict with workflow actions
|
2009-06-13 20:02:59 +02:00
|
|
|
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-10-07 22:07:55 +02:00
|
|
|
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
|
|
|
|
|
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-10-07 22:07:55 +02:00
|
|
|
return published_replies()
|
2009-05-25 20:59:25 +02:00
|
|
|
|
2010-01-18 12:38:57 +01:00
|
|
|
def get_commenter_home_url(self, username=None):
|
2009-06-07 22:58:41 +02:00
|
|
|
if username is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return "%s/author/%s" % (self.context.portal_url(), username)
|
|
|
|
|
2010-01-18 12:38:57 +01:00
|
|
|
def get_commenter_portrait(self, username=None):
|
2009-06-07 22:58:41 +02:00
|
|
|
|
|
|
|
if username is None:
|
2009-06-14 13:10:45 +02:00
|
|
|
# return the default user image if no username is given
|
|
|
|
return 'defaultUser.gif'
|
2009-06-07 22:58:41 +02:00
|
|
|
else:
|
2009-06-14 13:10:45 +02:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership', None)
|
|
|
|
return portal_membership.getPersonalPortrait(username).absolute_url();
|
2009-06-07 22:58:41 +02:00
|
|
|
|
2009-06-08 10:00:15 +02:00
|
|
|
def anonymous_discussion_allowed(self):
|
|
|
|
# Check if anonymous comments are allowed in the registry
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2009-07-12 21:13:42 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
2009-06-08 10:00:15 +02:00
|
|
|
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
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2009-07-12 21:13:42 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
2009-06-08 10:23:18 +02:00
|
|
|
return settings.show_commenter_image
|
|
|
|
|
2009-05-28 08:35:47 +02:00
|
|
|
def is_anonymous(self):
|
2010-02-16 21:25:56 +01:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership', None)
|
|
|
|
return portal_membership.isAnonymousUser()
|
2009-05-28 08:35:47 +02:00
|
|
|
|
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-06-16 22:53:45 +02:00
|
|
|
# We have to transform Python datetime into Zope DateTime
|
2009-06-21 22:33:02 +02:00
|
|
|
# before we can call toLocalizedTime.
|
2009-06-16 22:53:45 +02:00
|
|
|
util = getToolByName(self.context, 'translation_service')
|
|
|
|
zope_time = DateTime(time.year, time.month, time.day, time.hour, time.minute, time.second)
|
2009-08-20 04:09:53 +02:00
|
|
|
return util.toLocalizedTime(zope_time, long_format=True)
|