Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notification moderator: link to commented page to moderate comments instead of links to approve or delete with CSRF confirmation #163

Merged
merged 8 commits into from
Dec 3, 2019
4 changes: 4 additions & 0 deletions news/163.enhancement
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Notification moderator: - Email commentator added
- Link to commented page for editing, approving, deleting comment instead of link to /@@moderate-publish-comment and @@moderate-delete-comment
/@@moderate-publish-comment : publish only pending comment, else show status message "comment already approved"
[ksuess]
24 changes: 16 additions & 8 deletions plone/app/discussion/browser/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.statusmessages.interfaces import IStatusMessage
from zope.event import notify
from zope.interface import alsoProvides


class View(BrowserView):
Expand Down Expand Up @@ -209,18 +210,25 @@ def __call__(self):
content_object = aq_parent(aq_parent(comment))
workflowTool = getToolByName(comment, 'portal_workflow', None)
workflow_action = self.request.form.get('workflow_action', 'publish')
workflowTool.doActionFor(comment, workflow_action)
comment.reindexObject()
content_object.reindexObject(idxs=['total_comments'])
notify(CommentPublishedEvent(self.context, comment))
IStatusMessage(self.context.REQUEST).addStatusMessage(
_('Comment approved.'),
type='info')
review_state = workflowTool.getInfoFor(comment, 'review_state', '')
if review_state == "pending":
workflowTool.doActionFor(comment, workflow_action)
comment.reindexObject()
content_object.reindexObject(idxs=['total_comments'])
notify(CommentPublishedEvent(self.context, comment))
IStatusMessage(self.context.REQUEST).addStatusMessage(
_('Comment approved.'),
type='info')
else:
IStatusMessage(self.context.REQUEST).addStatusMessage(
_('Comment already approved.'),
type='info')
came_from = self.context.REQUEST.HTTP_REFERER
# if the referrer already has a came_from in it, don't redirect back
if (len(came_from) == 0 or 'came_from=' in came_from or
not getToolByName(
content_object, 'portal_url').isURLInPortal(came_from)):
content_object, 'portal_url').isURLInPortal(came_from) or
'@@confirm-action' in came_from):
came_from = content_object.absolute_url()
return self.context.REQUEST.RESPONSE.redirect(came_from)

Expand Down
22 changes: 13 additions & 9 deletions plone/app/discussion/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
MAIL_NOTIFICATION_MESSAGE_MODERATOR = _(
u'mail_notification_message_moderator',
default=u'A comment on "${title}" '
u'has been posted here: ${link}\n\n'
u'---\n'
u'${text}\n'
u'has been posted by ${commentator}\n'
u'here: ${link}\n\n'
u'---\n\n'
u'${text}\n\n'
u'---\n\n'
u'Approve comment:\n${link_approve}\n\n'
u'Delete comment:\n${link_delete}\n',
u'Log in to moderate.\n\n',
)

logger = logging.getLogger('plone.app.discussion')
Expand Down Expand Up @@ -419,17 +419,21 @@ def notify_moderator(obj, event):

# Compose email
subject = translate(_(u'A comment has been posted.'), context=obj.REQUEST)
link_approve = obj.absolute_url() + '/@@moderate-publish-comment'
link_delete = obj.absolute_url() + '/@@moderate-delete-comment'
message = translate(
Message(
MAIL_NOTIFICATION_MESSAGE_MODERATOR,
mapping={
'title': safe_unicode(content_object.title),
'link': content_object.absolute_url() + '/view#' + obj.id,
'text': obj.text,
'link_approve': link_approve,
'link_delete': link_delete,
'commentator': obj.author_email or translate(
Message(
_(
u'label_anonymous',
default=u'Anonymous',
),
),
)
},
),
context=obj.REQUEST,
Expand Down
26 changes: 10 additions & 16 deletions plone/app/discussion/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ def beforeTearDown(self):
provided=IMailHost)

def test_notify_moderator(self):
# Add a comment and make sure an email is send to the moderator.
"""Add a comment and make sure an email is send to the moderator."""
comment = createObject('plone.Comment')
comment.text = 'Comment text'
comment.author_email = '[email protected]'

comment_id = self.conversation.addComment(comment)

Expand All @@ -215,27 +216,20 @@ def test_notify_moderator(self):
# The output should be encoded in a reasonable manner
# (in this case quoted-printable):
self.assertTrue(
'A comment on "K=C3=B6lle Alaaf" has been posted here:'
in msg)
self.assertIn(
'http://nohost/plone/d=\noc1/view#{0}'.format(comment_id),
msg,
'A comment on "K=C3=B6lle Alaaf" has been posted'
in msg
)
self.assertIn(
'Comment text',
msg,
'http://nohost/plone/doc1/view#{0}'.format(comment_id),
msg
)
text = 'Approve comment:\nhttp://nohost/plone/doc1/' \
'++conversation++default/{0}/@@moderat=\ne-publish-comment'
self.assertIn(
text.format(comment_id),
msg,
comment.author_email,
msg
)
text = 'Delete comment:\nhttp://nohost/plone/doc1/' \
'++conversation++default/{0}/@@moderat=\ne-delete-comment'
self.assertIn(
text.format(comment_id),
msg,
comment.text,
msg
)

def test_notify_moderator_specific_address(self):
Expand Down