plone.app.discussion/plone/app/discussion/browser/comments.py

534 lines
21 KiB
Python
Raw Permalink Normal View History

from AccessControl import getSecurityManager
from AccessControl import Unauthorized
from Acquisition import aq_inner
from DateTime import DateTime
from plone.app.discussion import _
from plone.app.discussion.browser.validator import CaptchaValidator
from plone.app.discussion.interfaces import ICaptcha
from plone.app.discussion.interfaces import IComment
from plone.app.discussion.interfaces import IConversation
from plone.app.discussion.interfaces import IDiscussionSettings
from plone.app.discussion.interfaces import IReplies
2022-10-21 15:16:58 +02:00
from plone.app.event.base import localized_now
from plone.app.layout.viewlets.common import ViewletBase
from plone.base.utils import safe_text
from plone.registry.interfaces import IRegistry
from plone.z3cform import z2
from plone.z3cform.fieldsets import extensible
from plone.z3cform.interfaces import IWrappedForm
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.statusmessages.interfaces import IStatusMessage
2022-05-01 23:27:37 +02:00
from urllib.parse import quote
from z3c.form import button
from z3c.form import field
from z3c.form import form
from z3c.form import interfaces
from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
from z3c.form.interfaces import IFormLayer
from zope.component import createObject
from zope.component import queryUtility
from zope.i18n import translate
from zope.i18nmessageid import Message
from zope.interface import alsoProvides
2018-01-25 13:04:11 +01:00
COMMENT_DESCRIPTION_PLAIN_TEXT = _(
"comment_description_plain_text",
default="You can add a comment by filling out the form below. "
"Plain text formatting.",
)
COMMENT_DESCRIPTION_MARKDOWN = _(
"comment_description_markdown",
default="You can add a comment by filling out the form below. "
"Plain text formatting. You can use the Markdown syntax for "
"links and images.",
)
COMMENT_DESCRIPTION_INTELLIGENT_TEXT = _(
"comment_description_intelligent_text",
default="You can add a comment by filling out the form below. "
"Plain text formatting. Web and email addresses are "
"transformed into clickable links.",
)
COMMENT_DESCRIPTION_MODERATION_ENABLED = _(
"comment_description_moderation_enabled",
default="Comments are moderated.",
)
class CommentForm(extensible.ExtensibleForm, form.Form):
2012-01-14 07:26:01 +01:00
ignoreContext = True # don't use context to get widget data
id = None
label = _("Add a comment")
2022-05-01 23:14:09 +02:00
fields = field.Fields(IComment).omit(
"portal_type",
"__parent__",
"__name__",
"comment_id",
"mime_type",
"creator",
"creation_date",
"modification_date",
"author_username",
"title",
)
def updateFields(self):
super().updateFields()
2022-05-01 23:14:09 +02:00
self.fields["user_notification"].widgetFactory = SingleCheckBoxFieldWidget
def updateWidgets(self):
super().updateWidgets()
# Widgets
2022-05-01 23:14:09 +02:00
self.widgets["in_reply_to"].mode = interfaces.HIDDEN_MODE
self.widgets["text"].addClass("autoresize")
self.widgets["user_notification"].label = _("")
# Reset widget field settings to their defaults, which may be changed
# further on. Otherwise, the email field might get set to required
# when an anonymous user visits, and then remain required when an
# authenticated user visits, making it impossible for an authenticated
# user to fill in the form without validation error. Or when in the
# control panel the field is set as not required anymore, that change
# would have no effect until the instance was restarted. Note that the
# widget is new each time, but the field is the same item in memory as
# the previous time.
2022-05-01 23:14:09 +02:00
self.widgets["author_email"].field.required = False
# The widget is new, but its 'required' setting is based on the
# previous value on the field, so we need to reset it here. Changing
# the field in updateFields does not help.
2022-05-01 23:14:09 +02:00
self.widgets["author_email"].required = False
# Rename the id of the text widgets because there can be css-id
# clashes with the text field of documents when using and overlay
# with TinyMCE.
2022-05-01 23:14:09 +02:00
self.widgets["text"].id = "form-widgets-comment-text"
# Anonymous / Logged-in
2022-05-01 23:14:09 +02:00
mtool = getToolByName(self.context, "portal_membership")
anon = mtool.isAnonymousUser()
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
if anon:
if settings.anonymous_email_enabled:
# according to IDiscussionSettings.anonymous_email_enabled:
# 'If selected, anonymous user will have to give their email.'
2022-05-01 23:14:09 +02:00
self.widgets["author_email"].field.required = True
self.widgets["author_email"].required = True
else:
2022-05-01 23:14:09 +02:00
self.widgets["author_email"].mode = interfaces.HIDDEN_MODE
else:
2022-05-01 23:14:09 +02:00
self.widgets["author_name"].mode = interfaces.HIDDEN_MODE
self.widgets["author_email"].mode = interfaces.HIDDEN_MODE
member = mtool.getAuthenticatedMember()
2022-05-01 23:14:09 +02:00
member_email = member.getProperty("email")
# Hide the user_notification checkbox if user notification is disabled
2012-01-14 07:26:01 +01:00
# or the user is not logged in. Also check if the user has a valid
# email address
2022-05-01 23:14:09 +02:00
member_email_is_empty = member_email == ""
2013-04-18 15:46:28 +02:00
user_notification_disabled = not settings.user_notification_enabled
if member_email_is_empty or user_notification_disabled or anon:
2022-05-01 23:14:09 +02:00
self.widgets["user_notification"].mode = interfaces.HIDDEN_MODE
def updateActions(self):
super().updateActions()
2022-05-01 23:14:09 +02:00
self.actions["cancel"].addClass("btn btn-secondary")
self.actions["cancel"].addClass("hide")
self.actions["comment"].addClass("btn btn-primary")
2016-06-10 02:45:33 +02:00
def get_author(self, data):
context = aq_inner(self.context)
# some attributes are not always set
author_name = ""
2012-01-14 07:26:01 +01:00
# Make sure author_name/ author_email is properly encoded
2022-05-01 23:14:09 +02:00
if "author_name" in data:
author_name = safe_text(data["author_name"])
2022-05-01 23:14:09 +02:00
if "author_email" in data:
author_email = safe_text(data["author_email"])
2012-01-14 07:26:01 +01:00
# Set comment author properties for anonymous users or members
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(context, "portal_membership")
2016-06-10 02:45:33 +02:00
anon = portal_membership.isAnonymousUser()
2022-05-01 23:14:09 +02:00
if not anon and getSecurityManager().checkPermission("Reply to item", context):
# Member
member = portal_membership.getAuthenticatedMember()
email = safe_text(member.getProperty("email"))
2022-05-01 23:14:09 +02:00
fullname = member.getProperty("fullname")
if not fullname or fullname == "":
fullname = member.getUserName()
fullname = safe_text(fullname)
2016-06-10 02:45:33 +02:00
author_name = fullname
email = safe_text(email)
# XXX: according to IComment interface author_email must not be # noqa T000
# set for logged in users, cite:
# 'for anonymous comments only, set to None for logged in comments'
2016-06-10 02:45:33 +02:00
author_email = email
# /XXX # noqa T000
2016-06-10 02:45:33 +02:00
return author_name, author_email
def create_comment(self, data):
context = aq_inner(self.context)
2022-05-01 23:14:09 +02:00
comment = createObject("plone.Comment")
2016-06-10 02:45:33 +02:00
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
anonymous_comments = settings.anonymous_comments
# Set comment mime type to current setting in the discussion registry
comment.mime_type = settings.text_transform
# Set comment attributes (including extended comment form attributes)
for attribute in self.fields.keys():
setattr(comment, attribute, data[attribute])
# Set dates
2022-10-21 15:16:58 +02:00
comment.creation_date = localized_now()
comment.modification_date = localized_now()
2016-06-10 02:45:33 +02:00
# Get author name and email
comment.author_name, comment.author_email = self.get_author(data)
# Set comment author properties for anonymous users or members
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(context, "portal_membership")
2016-06-10 02:45:33 +02:00
anon = portal_membership.isAnonymousUser()
if anon and anonymous_comments:
# Anonymous Users
comment.user_notification = None
elif not anon and getSecurityManager().checkPermission(
2022-05-01 23:14:09 +02:00
"Reply to item", context
):
2016-06-10 02:45:33 +02:00
# Member
member = portal_membership.getAuthenticatedMember()
memberid = member.getId()
user = member.getUser()
comment.changeOwnership(user, recursive=False)
2022-05-01 23:14:09 +02:00
comment.manage_setLocalRoles(memberid, ["Owner"])
2016-06-10 02:45:33 +02:00
comment.creator = memberid
comment.author_username = memberid
2012-01-14 07:26:01 +01:00
else: # pragma: no cover
2013-04-18 15:46:28 +02:00
raise Unauthorized(
"Anonymous user tries to post a comment, but anonymous "
"commenting is disabled. Or user does not have the "
"'reply to item' permission.",
2013-04-18 15:46:28 +02:00
)
2016-06-10 02:45:33 +02:00
return comment
@button.buttonAndHandler(_("add_comment_button", default="Comment"), name="comment")
2016-06-10 02:45:33 +02:00
def handleComment(self, action):
context = aq_inner(self.context)
# Check if conversation is enabled on this content object
if not self.__parent__.restrictedTraverse(
2022-05-01 23:14:09 +02:00
"@@conversation_view",
2016-06-10 02:45:33 +02:00
).enabled():
raise Unauthorized(
2022-05-01 23:14:09 +02:00
"Discussion is not enabled for this content object.",
2016-06-10 02:45:33 +02:00
)
# Validation form
data, errors = self.extractData()
if errors:
return
# Validate Captcha
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(self.context, "portal_membership")
captcha_enabled = settings.captcha != "disabled"
2016-06-10 02:45:33 +02:00
anonymous_comments = settings.anonymous_comments
anon = portal_membership.isAnonymousUser()
if captcha_enabled and anonymous_comments and anon:
2022-05-01 23:14:09 +02:00
if "captcha" not in data:
data["captcha"] = ""
2022-05-01 23:14:09 +02:00
captcha = CaptchaValidator(
self.context, self.request, None, ICaptcha["captcha"], None
)
captcha.validate(data["captcha"])
2016-06-10 02:45:33 +02:00
# Create comment
comment = self.create_comment(data)
# Add comment to conversation
conversation = IConversation(self.__parent__)
2022-05-01 23:14:09 +02:00
if data["in_reply_to"]:
# Add a reply to an existing comment
2022-05-01 23:14:09 +02:00
conversation_to_reply_to = conversation.get(data["in_reply_to"])
replies = IReplies(conversation_to_reply_to)
comment_id = replies.addComment(comment)
else:
# Add a comment to the conversation
comment_id = conversation.addComment(comment)
# Redirect after form submit:
# If a user posts a comment and moderation is enabled, a message is
# shown to the user that his/her comment awaits moderation. If the user
# has 'review comments' permission, he/she is redirected directly
# to the comment.
2022-05-01 23:14:09 +02:00
can_review = getSecurityManager().checkPermission("Review comments", context)
workflowTool = getToolByName(context, "portal_workflow")
comment_review_state = workflowTool.getInfoFor(
2013-04-18 15:46:28 +02:00
comment,
2022-05-01 23:14:09 +02:00
"review_state",
None,
)
2022-05-01 23:14:09 +02:00
if comment_review_state == "pending" and not can_review:
# Show info message when comment moderation is enabled
IStatusMessage(self.context.REQUEST).addStatusMessage(
2022-05-01 23:14:09 +02:00
_("Your comment awaits moderator approval."), type="info"
)
self.request.response.redirect(self.action)
else:
# Redirect to comment (inside a content object page)
2022-05-01 23:14:09 +02:00
self.request.response.redirect(self.action + "#" + str(comment_id))
@button.buttonAndHandler(_("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.
2012-01-14 07:26:01 +01:00
pass # pragma: no cover
class CommentsViewlet(ViewletBase):
form = CommentForm
2022-05-01 23:14:09 +02:00
index = ViewPageTemplateFile("comments.pt")
def update(self):
super().update()
2013-04-18 15:46:28 +02:00
discussion_allowed = self.is_discussion_allowed()
anonymous_allowed_or_can_reply = (
2022-05-01 23:14:09 +02:00
self.is_anonymous()
and self.anonymous_discussion_allowed()
or self.can_reply()
2013-04-18 15:46:28 +02:00
)
if discussion_allowed and anonymous_allowed_or_can_reply:
z2.switch_on(self, request_layer=IFormLayer)
self.form = self.form(aq_inner(self.context), self.request)
alsoProvides(self.form, IWrappedForm)
self.form.update()
# view methods
def can_reply(self):
2022-05-01 23:14:09 +02:00
"""Returns true if current user has the 'Reply to item' permission."""
return getSecurityManager().checkPermission(
"Reply to item", aq_inner(self.context)
)
def can_manage(self):
"""We keep this method for <= 1.0b9 backward compatibility. Since we do
2022-05-01 23:14:09 +02:00
not want any API changes in beta releases.
"""
return self.can_review()
def can_review(self):
2022-05-01 23:14:09 +02:00
"""Returns true if current user has the 'Review comments' permission."""
return getSecurityManager().checkPermission(
"Review comments", aq_inner(self.context)
)
def can_delete_own(self, comment):
"""Returns true if the current user can delete the comment. Only
comments without replies can be deleted.
"""
try:
2022-05-01 23:14:09 +02:00
return comment.restrictedTraverse("@@delete-own-comment").can_delete()
except Unauthorized:
return False
def could_delete_own(self, comment):
2014-08-17 04:48:44 +02:00
"""Returns true if the current user could delete the comment if it had
no replies. This is used to prepare hidden form buttons for JS.
"""
try:
2022-05-01 23:14:09 +02:00
return comment.restrictedTraverse("@@delete-own-comment").could_delete()
except Unauthorized:
return False
2013-09-17 14:03:46 +02:00
def can_edit(self, reply):
"""Returns true if current user has the 'Edit comments'
2013-09-17 14:03:46 +02:00
permission.
"""
2022-05-01 23:14:09 +02:00
return getSecurityManager().checkPermission("Edit comments", aq_inner(reply))
2013-09-17 14:03:46 +02:00
def can_delete(self, reply):
"""Returns true if current user has the 'Delete comments'
permission.
"""
2022-05-01 23:14:09 +02:00
return getSecurityManager().checkPermission("Delete comments", aq_inner(reply))
def is_discussion_allowed(self):
context = aq_inner(self.context)
2022-05-01 23:14:09 +02:00
return context.restrictedTraverse("@@conversation_view").enabled()
def comment_transform_message(self):
"""Returns the description that shows up above the comment text,
2022-05-01 23:14:09 +02:00
dependent on the text_transform setting and the comment moderation
workflow in the discussion control panel.
"""
context = aq_inner(self.context)
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
# text transform setting
2022-05-01 23:14:09 +02:00
if settings.text_transform == "text/x-web-intelligent":
message = translate(
Message(COMMENT_DESCRIPTION_INTELLIGENT_TEXT), context=self.request
)
elif settings.text_transform == "text/x-web-markdown":
message = translate(
Message(COMMENT_DESCRIPTION_MARKDOWN), context=self.request
)
else:
2022-05-01 23:14:09 +02:00
message = translate(
Message(COMMENT_DESCRIPTION_PLAIN_TEXT), context=self.request
)
# comment workflow
2022-05-01 23:14:09 +02:00
wftool = getToolByName(context, "portal_workflow", None)
workflow_chain = wftool.getChainForPortalType("Discussion Item")
if workflow_chain:
comment_workflow = workflow_chain[0]
comment_workflow = wftool[comment_workflow]
# check if the current workflow implements a pending state. If this
# is true comments are moderated
2022-05-01 23:14:09 +02:00
if "pending" in comment_workflow.states:
message = (
message
+ " "
+ translate(
Message(COMMENT_DESCRIPTION_MODERATION_ENABLED),
context=self.request,
)
)
return message
def has_replies(self, workflow_actions=False):
2022-05-01 23:14:09 +02:00
"""Returns true if there are replies."""
if self.get_replies(workflow_actions) is not None:
try:
2018-01-25 13:04:11 +01:00
next(self.get_replies(workflow_actions))
return True
2012-01-14 07:26:01 +01:00
except StopIteration: # pragma: no cover
pass
return False
def get_replies(self, workflow_actions=False):
"""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([])
2022-05-01 23:14:09 +02:00
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():
2022-05-01 23:14:09 +02:00
comment_obj = r["comment"]
# list all possible workflow actions
2013-04-18 15:46:28 +02:00
actions = [
2022-05-01 23:14:09 +02:00
a
for a in wf.listActionInfos(object=comment_obj)
if a["category"] == "workflow" and a["allowed"]
2013-04-18 15:46:28 +02:00
]
r = r.copy()
2022-05-01 23:14:09 +02:00
r["actions"] = actions
yield r
def published_replies():
# Generator that returns replies dict with workflow status.
for r in conversation.getThreads():
2022-05-01 23:14:09 +02:00
comment_obj = r["comment"]
workflow_status = wf.getInfoFor(comment_obj, "review_state")
if workflow_status == "published":
r = r.copy()
2022-05-01 23:14:09 +02:00
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_commenter_home_url(self, username=None):
if username is None:
return None
else:
return f"{self.context.portal_url()}/author/{username}"
def get_commenter_portrait(self, username=None):
if username is None:
# return the default user image if no username is given
2022-05-01 23:14:09 +02:00
return "defaultUser.png"
else:
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(self.context, "portal_membership", None)
return portal_membership.getPersonalPortrait(username).absolute_url()
def anonymous_discussion_allowed(self):
# Check if anonymous comments are allowed in the registry
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
return settings.anonymous_comments
2013-09-17 14:03:46 +02:00
def edit_comment_allowed(self):
# Check if editing comments is allowed in the registry
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
return settings.edit_comment_enabled
def delete_own_comment_allowed(self):
# Check if delete own comments is allowed in the registry
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
return settings.delete_own_comment_enabled
2013-09-17 14:03:46 +02:00
def show_commenter_image(self):
# Check if showing commenter image is enabled in the registry
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
return settings.show_commenter_image
def is_anonymous(self):
2022-05-01 23:14:09 +02:00
portal_membership = getToolByName(self.context, "portal_membership", None)
return portal_membership.isAnonymousUser()
def login_action(self):
return "{}/login_form?came_from={}".format(
self.navigation_root_url,
2022-05-01 23:14:09 +02:00
quote(self.request.get("URL", "")),
)
def format_time(self, time):
# We have to transform Python datetime into Zope DateTime
# before we can call toLocalizedTime.
2022-05-01 23:14:09 +02:00
util = getToolByName(self.context, "translation_service")
zope_time = DateTime(time.isoformat())
return util.toLocalizedTime(zope_time, long_format=True)