2018-06-15 10:22:11 +02:00
|
|
|
from .comments import CommentForm
|
2013-09-17 14:03:46 +02:00
|
|
|
from AccessControl import getSecurityManager
|
2015-05-03 08:16:39 +02:00
|
|
|
from Acquisition import aq_inner
|
|
|
|
from Acquisition import aq_parent
|
2015-11-05 00:26:49 +01:00
|
|
|
from plone.app.discussion import _
|
2015-09-20 17:31:55 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
2013-09-17 14:03:46 +02:00
|
|
|
from plone.z3cform.layout import wrap_form
|
2016-02-05 01:39:53 +01:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
from Products.Five.browser import BrowserView
|
|
|
|
from Products.statusmessages.interfaces import IStatusMessage
|
2015-05-03 08:16:39 +02:00
|
|
|
from z3c.form import button
|
|
|
|
from zope.component import getMultiAdapter
|
2016-02-05 01:39:53 +01:00
|
|
|
from zope.component import getUtility
|
2016-04-07 18:30:30 +02:00
|
|
|
from zope.event import notify
|
|
|
|
from zope.lifecycleevent import ObjectModifiedEvent
|
2013-09-17 14:03:46 +02:00
|
|
|
|
2012-01-14 07:47:07 +01:00
|
|
|
|
2010-01-18 10:54:27 +01:00
|
|
|
class View(BrowserView):
|
|
|
|
"""Comment View.
|
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
When the view of a comment object is called directly, redirect to the
|
2010-09-06 12:38:54 +02:00
|
|
|
the page (content object) and the location (HTML-anchor) where the comment
|
|
|
|
has been posted.
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-09-06 12:38:54 +02:00
|
|
|
Redirect from the comment object URL
|
2016-02-05 01:39:53 +01:00
|
|
|
'/path/to/object/++conversation++default/123456789' to the content object
|
2010-12-16 00:52:56 +01:00
|
|
|
where the comment has been posted appended by an HTML anchor that points to
|
2016-02-05 01:39:53 +01:00
|
|
|
the comment '/path/to/object#comment-123456789'.
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-09-06 12:38:54 +02:00
|
|
|
Context is the comment object. The parent of the comment object is the
|
|
|
|
conversation. The parent of the conversation is the content object where
|
|
|
|
the comment has been posted.
|
2010-01-18 10:54:27 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
2010-09-06 12:38:54 +02:00
|
|
|
context = aq_inner(self.context)
|
2015-09-20 17:31:55 +02:00
|
|
|
|
|
|
|
registry = getUtility(IRegistry)
|
2022-05-01 23:14:09 +02:00
|
|
|
view_action_types = registry.get("plone.types_use_view_action_in_listings", [])
|
2015-09-20 17:31:55 +02:00
|
|
|
|
2012-03-14 23:01:17 +01:00
|
|
|
obj = aq_parent(aq_parent(context))
|
|
|
|
url = obj.absolute_url()
|
|
|
|
|
|
|
|
"""
|
|
|
|
Image and File types, as well as many other customized archetypes
|
|
|
|
require /view be appended to the url to see the comments, otherwise it
|
|
|
|
will redirect right to the binary object, bypassing comments.
|
|
|
|
"""
|
|
|
|
if obj.portal_type in view_action_types:
|
2022-05-01 23:14:41 +02:00
|
|
|
url = f"{url}/view"
|
2012-03-14 23:01:17 +01:00
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
self.request.response.redirect(f"{url}#{context.id}")
|
2013-09-17 14:03:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditCommentForm(CommentForm):
|
|
|
|
"""Form to edit an existing comment."""
|
2022-05-01 23:14:09 +02:00
|
|
|
|
2013-09-17 14:03:46 +02:00
|
|
|
ignoreContext = True
|
2022-05-01 23:14:09 +02:00
|
|
|
id = "edit-comment-form"
|
2022-05-01 23:14:41 +02:00
|
|
|
label = _("edit_comment_form_title", default="Edit comment")
|
2013-09-17 14:03:46 +02:00
|
|
|
|
|
|
|
def updateWidgets(self):
|
2022-05-01 23:14:41 +02:00
|
|
|
super().updateWidgets()
|
2022-05-01 23:14:09 +02:00
|
|
|
self.widgets["text"].value = self.context.text
|
2013-09-17 14:03:46 +02:00
|
|
|
# We have to rename the id, otherwise TinyMCE can't initialize
|
|
|
|
# because there are two textareas with the same id.
|
2022-05-01 23:14:09 +02:00
|
|
|
self.widgets["text"].id = "overlay-comment-text"
|
2013-09-17 14:03:46 +02:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
def _redirect(self, target=""):
|
2013-09-17 14:03:46 +02:00
|
|
|
if not target:
|
2022-05-01 23:14:09 +02:00
|
|
|
portal_state = getMultiAdapter(
|
2022-05-01 23:14:41 +02:00
|
|
|
(self.context, self.request), name="plone_portal_state"
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
2013-09-17 14:03:46 +02:00
|
|
|
target = portal_state.portal_url()
|
|
|
|
self.request.response.redirect(target)
|
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
@button.buttonAndHandler(_("label_save", default="Save"), name="comment")
|
2013-09-17 14:03:46 +02:00
|
|
|
def handleComment(self, action):
|
|
|
|
|
|
|
|
# Validate form
|
|
|
|
data, errors = self.extractData()
|
|
|
|
if errors:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Check permissions
|
2022-05-01 23:14:09 +02:00
|
|
|
can_edit = getSecurityManager().checkPermission("Edit comments", self.context)
|
|
|
|
mtool = getToolByName(self.context, "portal_membership")
|
2013-09-17 14:03:46 +02:00
|
|
|
if mtool.isAnonymousUser() or not can_edit:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Update text
|
2022-05-01 23:14:09 +02:00
|
|
|
self.context.text = data["text"]
|
2016-04-07 18:30:30 +02:00
|
|
|
# Notify that the object has been modified
|
|
|
|
notify(ObjectModifiedEvent(self.context))
|
2013-09-17 14:03:46 +02:00
|
|
|
|
|
|
|
# Redirect to comment
|
2022-05-01 23:14:09 +02:00
|
|
|
IStatusMessage(self.request).add(
|
2022-05-01 23:14:41 +02:00
|
|
|
_("comment_edit_notification", default="Comment was edited"), type="info"
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
|
|
|
return self._redirect(target=self.action.replace("@@edit-comment", "@@view"))
|
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
@button.buttonAndHandler(_("cancel_form_button", default="Cancel"), name="cancel")
|
2013-09-17 14:03:46 +02:00
|
|
|
def handle_cancel(self, action):
|
2016-02-05 01:39:53 +01:00
|
|
|
IStatusMessage(self.request).add(
|
2022-05-01 23:14:41 +02:00
|
|
|
_("comment_edit_cancel_notification", default="Edit comment cancelled"),
|
2022-05-01 23:14:09 +02:00
|
|
|
type="info",
|
|
|
|
)
|
2016-02-05 01:39:53 +01:00
|
|
|
return self._redirect(target=self.context.absolute_url())
|
2013-09-17 14:03:46 +02:00
|
|
|
|
|
|
|
|
2017-07-28 17:58:35 +02:00
|
|
|
EditComment = wrap_form(EditCommentForm)
|