2009-05-13 16:54:06 +02:00
|
|
|
"""The default comment class and factory.
|
2009-05-11 18:52:16 +02:00
|
|
|
"""
|
2015-05-03 08:16:39 +02:00
|
|
|
from AccessControl import ClassSecurityInfo
|
|
|
|
from AccessControl.SecurityManagement import getSecurityManager
|
|
|
|
from Acquisition import aq_base
|
|
|
|
from Acquisition import aq_parent
|
2016-02-05 01:39:53 +01:00
|
|
|
from Acquisition import Implicit
|
2015-05-03 08:16:39 +02:00
|
|
|
from OFS.owner import Owned
|
|
|
|
from OFS.role import RoleManager
|
2009-10-17 18:29:45 +02:00
|
|
|
from OFS.Traversable import Traversable
|
2022-10-23 10:35:27 +02:00
|
|
|
from datetime import timezone
|
2015-05-03 08:16:39 +02:00
|
|
|
from persistent import Persistent
|
2015-11-05 00:26:49 +01:00
|
|
|
from plone.app.discussion import _
|
2014-04-16 17:54:19 +02:00
|
|
|
from plone.app.discussion.events import CommentAddedEvent
|
2021-09-08 13:02:34 +02:00
|
|
|
from plone.app.discussion.events import CommentModifiedEvent
|
2014-04-16 17:54:19 +02:00
|
|
|
from plone.app.discussion.events import CommentRemovedEvent
|
|
|
|
from plone.app.discussion.events import ReplyAddedEvent
|
2021-09-08 13:02:34 +02:00
|
|
|
from plone.app.discussion.events import ReplyModifiedEvent
|
2014-04-16 17:54:19 +02:00
|
|
|
from plone.app.discussion.events import ReplyRemovedEvent
|
2010-03-16 16:06:43 +01:00
|
|
|
from plone.app.discussion.interfaces import IComment
|
|
|
|
from plone.app.discussion.interfaces import IConversation
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
2022-10-21 15:16:58 +02:00
|
|
|
from plone.app.event.base import localized_now
|
2022-05-02 00:39:34 +02:00
|
|
|
from plone.base.interfaces.controlpanel import IMailSchema
|
|
|
|
from plone.base.utils import safe_text
|
2014-12-13 16:20:08 +01:00
|
|
|
from plone.registry.interfaces import IRegistry
|
2016-02-05 01:39:53 +01:00
|
|
|
from Products.CMFCore import permissions
|
|
|
|
from Products.CMFCore.CMFCatalogAware import CatalogAware
|
|
|
|
from Products.CMFCore.CMFCatalogAware import WorkflowAware
|
|
|
|
from Products.CMFCore.DynamicType import DynamicType
|
|
|
|
from Products.CMFCore.utils import getToolByName
|
2015-05-03 08:16:39 +02:00
|
|
|
from smtplib import SMTPException
|
|
|
|
from zope.annotation.interfaces import IAnnotatable
|
|
|
|
from zope.component import getUtility
|
|
|
|
from zope.component import queryUtility
|
|
|
|
from zope.component.factory import Factory
|
|
|
|
from zope.event import notify
|
|
|
|
from zope.i18n import translate
|
|
|
|
from zope.i18nmessageid import Message
|
2016-07-05 23:12:08 +02:00
|
|
|
from zope.interface import implementer
|
2014-12-13 16:20:08 +01:00
|
|
|
|
2015-05-03 08:16:39 +02:00
|
|
|
import logging
|
2018-01-25 13:04:11 +01:00
|
|
|
|
|
|
|
|
2011-04-22 19:09:09 +02:00
|
|
|
COMMENT_TITLE = _(
|
2022-05-01 23:14:41 +02:00
|
|
|
"comment_title",
|
|
|
|
default="${author_name} on ${content}",
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2011-04-22 19:09:09 +02:00
|
|
|
MAIL_NOTIFICATION_MESSAGE = _(
|
2022-05-01 23:14:41 +02:00
|
|
|
"mail_notification_message",
|
|
|
|
default='A comment on "${title}" '
|
|
|
|
"has been posted here: ${link}\n\n"
|
|
|
|
"---\n"
|
|
|
|
"${text}\n"
|
|
|
|
"---\n",
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
2010-08-28 17:35:21 +02:00
|
|
|
|
2011-04-22 19:09:09 +02:00
|
|
|
MAIL_NOTIFICATION_MESSAGE_MODERATOR = _(
|
2022-05-01 23:14:41 +02:00
|
|
|
"mail_notification_message_moderator2",
|
|
|
|
default='A comment on "${title}" '
|
|
|
|
"has been posted by ${commentator}\n"
|
|
|
|
"here: ${link}\n\n"
|
|
|
|
"---\n\n"
|
|
|
|
"${text}\n\n"
|
|
|
|
"---\n\n"
|
|
|
|
"Log in to moderate.\n\n",
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
2011-04-22 19:09:09 +02:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
logger = logging.getLogger("plone.app.discussion")
|
2010-11-04 16:56:12 +01:00
|
|
|
|
2009-05-23 18:12:45 +02:00
|
|
|
|
2016-07-05 23:12:08 +02:00
|
|
|
@implementer(IComment)
|
2022-05-01 23:14:09 +02:00
|
|
|
class Comment(
|
|
|
|
CatalogAware,
|
|
|
|
WorkflowAware,
|
|
|
|
DynamicType,
|
|
|
|
Traversable,
|
|
|
|
RoleManager,
|
|
|
|
Owned,
|
|
|
|
Implicit,
|
|
|
|
Persistent,
|
|
|
|
):
|
2009-05-11 18:52:16 +02:00
|
|
|
"""A comment.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
This object attempts to be as lightweight as possible. We implement a
|
|
|
|
number of standard methods instead of subclassing, to have total control
|
|
|
|
over what goes into the object.
|
|
|
|
"""
|
2022-05-01 23:14:09 +02:00
|
|
|
|
2012-11-13 09:45:17 +01:00
|
|
|
security = ClassSecurityInfo()
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
meta_type = portal_type = "Discussion Item"
|
2010-03-18 15:42:52 +01:00
|
|
|
# This needs to be kept in sync with types/Discussion_Item.xml title
|
2022-05-01 23:14:09 +02:00
|
|
|
fti_title = "Comment"
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
__parent__ = None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
|
|
|
comment_id = None # long
|
|
|
|
in_reply_to = None # long
|
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
title = ""
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2013-06-04 15:18:40 +02:00
|
|
|
mime_type = None
|
2022-05-01 23:14:41 +02:00
|
|
|
text = ""
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
creator = None
|
|
|
|
creation_date = None
|
|
|
|
modification_date = None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
author_username = None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
author_name = None
|
|
|
|
author_email = None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
user_notification = None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-06-13 18:46:37 +02:00
|
|
|
# Note: we want to use zope.component.createObject() to instantiate
|
2009-05-18 17:15:36 +02:00
|
|
|
# comments as far as possible. comment_id and __parent__ are set via
|
|
|
|
# IConversation.addComment().
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
def __init__(self):
|
2022-10-21 15:16:58 +02:00
|
|
|
self.creation_date = self.modification_date = localized_now()
|
2022-05-01 23:14:09 +02:00
|
|
|
self.mime_type = "text/plain"
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2013-09-17 14:03:46 +02:00
|
|
|
user = getSecurityManager().getUser()
|
|
|
|
if user and user.getId():
|
|
|
|
aclpath = [x for x in user.getPhysicalPath() if x]
|
2022-05-01 23:14:09 +02:00
|
|
|
self._owner = (
|
|
|
|
aclpath,
|
|
|
|
user.getId(),
|
|
|
|
)
|
2013-09-17 14:03:46 +02:00
|
|
|
self.__ac_local_roles__ = {
|
2022-05-01 23:14:09 +02:00
|
|
|
user.getId(): ["Owner"],
|
2013-09-17 14:03:46 +02:00
|
|
|
}
|
|
|
|
|
2022-10-21 15:18:09 +02:00
|
|
|
def __getattribute__(self, attr):
|
|
|
|
# In older versions of the add-on dates were set timezone naive.
|
|
|
|
# In tz aware versions, the value is stored as self._creation_date
|
|
|
|
if attr in ["creation_date", "modification_date"]:
|
|
|
|
old_date = super(Comment, self).__getattribute__(attr)
|
|
|
|
if old_date.tzinfo is None:
|
|
|
|
# Naive dates were always stored utc
|
2022-10-23 10:24:59 +02:00
|
|
|
return old_date.replace(tzinfo=timezone.utc)
|
2022-10-21 15:18:09 +02:00
|
|
|
return old_date
|
2022-10-24 22:10:26 +02:00
|
|
|
return super().__getattribute__(attr)
|
2022-10-21 15:18:09 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
@property
|
|
|
|
def __name__(self):
|
2022-05-01 23:14:41 +02:00
|
|
|
return self.comment_id and str(self.comment_id) or None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
@property
|
|
|
|
def id(self):
|
2009-05-18 16:16:48 +02:00
|
|
|
return self.comment_id and str(self.comment_id) or None
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
def getId(self):
|
2016-05-04 10:40:36 +02:00
|
|
|
# The id of the comment, as a string.
|
2009-05-13 16:54:06 +02:00
|
|
|
return self.id
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2011-04-02 21:51:37 +02:00
|
|
|
def getText(self, targetMimetype=None):
|
2019-09-07 11:31:53 +02:00
|
|
|
"""The body text of a comment."""
|
2022-05-01 23:14:09 +02:00
|
|
|
transforms = getToolByName(self, "portal_transforms")
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2011-04-02 21:51:37 +02:00
|
|
|
if targetMimetype is None:
|
2022-05-01 23:14:09 +02:00
|
|
|
targetMimetype = "text/x-html-safe"
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
sourceMimetype = getattr(self, "mime_type", None)
|
2011-04-02 21:51:37 +02:00
|
|
|
if sourceMimetype is None:
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
|
|
|
sourceMimetype = settings.text_transform
|
|
|
|
text = self.text
|
2011-11-30 13:05:59 +01:00
|
|
|
if text is None:
|
2022-05-01 23:14:09 +02:00
|
|
|
return ""
|
2012-07-12 10:26:39 +02:00
|
|
|
transform = transforms.convertTo(
|
2022-05-01 23:14:09 +02:00
|
|
|
targetMimetype, text, context=self, mimetype=sourceMimetype
|
|
|
|
)
|
2012-07-12 10:26:39 +02:00
|
|
|
if transform:
|
|
|
|
return transform.getData()
|
|
|
|
else:
|
2022-05-01 23:14:09 +02:00
|
|
|
logger = logging.getLogger("plone.app.discussion")
|
|
|
|
msg = (
|
2022-05-01 23:14:41 +02:00
|
|
|
'Transform "{0}" => "{1}" not available. Failed to '
|
|
|
|
'transform comment "{2}".'
|
2022-05-01 23:14:09 +02:00
|
|
|
)
|
2016-02-05 01:39:53 +01:00
|
|
|
logger.error(
|
2018-06-18 17:04:41 +02:00
|
|
|
msg.format(
|
|
|
|
sourceMimetype,
|
|
|
|
targetMimetype,
|
|
|
|
self.absolute_url(),
|
|
|
|
),
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
2012-07-12 10:26:39 +02:00
|
|
|
return text
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Title(self):
|
2016-05-04 10:40:36 +02:00
|
|
|
# The title of the comment.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2011-04-02 23:16:24 +02:00
|
|
|
if self.title:
|
|
|
|
return self.title
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2012-01-30 13:30:46 +01:00
|
|
|
if not self.author_name:
|
2013-04-18 16:12:00 +02:00
|
|
|
author_name = translate(
|
2018-06-18 17:04:41 +02:00
|
|
|
Message(
|
|
|
|
_(
|
2022-05-01 23:14:41 +02:00
|
|
|
"label_anonymous",
|
|
|
|
default="Anonymous",
|
2018-06-18 17:04:41 +02:00
|
|
|
),
|
|
|
|
),
|
2013-04-18 16:12:00 +02:00
|
|
|
)
|
2010-09-29 12:52:11 +02:00
|
|
|
else:
|
2012-01-30 13:30:46 +01:00
|
|
|
author_name = self.author_name
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
# Fetch the content object (the parent of the comment is the
|
2010-09-29 09:56:36 +02:00
|
|
|
# conversation, the parent of the conversation is the content object).
|
|
|
|
content = aq_base(self.__parent__.__parent__)
|
|
|
|
title = translate(
|
2022-05-01 23:14:09 +02:00
|
|
|
Message(
|
|
|
|
COMMENT_TITLE,
|
|
|
|
mapping={
|
2022-05-02 00:39:34 +02:00
|
|
|
"author_name": safe_text(author_name),
|
|
|
|
"content": safe_text(content.Title()),
|
2022-05-01 23:14:09 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2010-09-29 09:56:36 +02:00
|
|
|
return title
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Creator(self):
|
2016-05-04 10:40:36 +02:00
|
|
|
# The name of the person who wrote the comment.
|
2013-09-17 14:03:46 +02:00
|
|
|
return self.creator or self.author_name
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2016-02-05 01:39:53 +01:00
|
|
|
@security.protected(permissions.View)
|
2009-06-24 10:25:11 +02:00
|
|
|
def Type(self):
|
2016-05-04 10:40:36 +02:00
|
|
|
# The Discussion Item content type.
|
2010-03-18 15:42:52 +01:00
|
|
|
return self.fti_title
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2009-10-16 14:11:58 +02:00
|
|
|
# CMF's event handlers assume any IDynamicType has these :(
|
2012-01-09 16:31:52 +01:00
|
|
|
|
|
|
|
def opaqueItems(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
2012-01-09 16:31:52 +01:00
|
|
|
|
|
|
|
def opaqueIds(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
2012-01-09 16:31:52 +01:00
|
|
|
|
|
|
|
def opaqueValues(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
|
|
|
|
2017-01-24 11:58:39 +01:00
|
|
|
|
2009-05-23 18:37:10 +02:00
|
|
|
CommentFactory = Factory(Comment)
|
|
|
|
|
2011-04-22 19:09:09 +02:00
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_workflow(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Tell the workflow tool when a comment is added"""
|
|
|
|
tool = getToolByName(obj, "portal_workflow", None)
|
2009-05-23 18:37:10 +02:00
|
|
|
if tool is not None:
|
2009-07-04 18:18:48 +02:00
|
|
|
tool.notifyCreated(obj)
|
|
|
|
|
2011-04-22 19:09:09 +02:00
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_content_object(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Tell the content object when a comment is added"""
|
2009-07-04 18:18:48 +02:00
|
|
|
content_obj = aq_parent(aq_parent(obj))
|
2022-05-01 23:14:09 +02:00
|
|
|
content_obj.reindexObject(
|
|
|
|
idxs=("total_comments", "last_comment_date", "commentators")
|
|
|
|
)
|
2010-02-08 13:02:54 +01:00
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_content_object_deleted(obj, event):
|
2010-03-16 16:06:43 +01:00
|
|
|
"""Remove all comments of a content object when the content object has been
|
2022-05-01 23:14:09 +02:00
|
|
|
deleted.
|
2010-03-16 16:06:43 +01:00
|
|
|
"""
|
|
|
|
if IAnnotatable.providedBy(obj):
|
|
|
|
conversation = IConversation(obj)
|
2011-04-14 23:30:32 +02:00
|
|
|
while conversation:
|
2018-01-25 13:04:11 +01:00
|
|
|
del conversation[list(conversation.keys())[0]]
|
2011-04-14 23:30:32 +02:00
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2014-04-16 17:54:19 +02:00
|
|
|
def notify_comment_added(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Notify custom discussion events when a comment is added or replied"""
|
2014-04-17 13:57:41 +02:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
context = aq_parent(conversation)
|
2022-05-01 23:14:09 +02:00
|
|
|
if getattr(obj, "in_reply_to", None):
|
2014-04-17 13:57:41 +02:00
|
|
|
return notify(ReplyAddedEvent(context, obj))
|
|
|
|
return notify(CommentAddedEvent(context, obj))
|
2014-04-16 17:54:19 +02:00
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2021-09-08 13:02:34 +02:00
|
|
|
def notify_comment_modified(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Notify custom discussion events when a comment, or a reply, is modified"""
|
2021-09-08 13:02:34 +02:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
context = aq_parent(conversation)
|
2022-05-01 23:14:09 +02:00
|
|
|
if getattr(obj, "in_reply_to", None):
|
2021-09-08 13:02:34 +02:00
|
|
|
return notify(ReplyModifiedEvent(context, obj))
|
|
|
|
return notify(CommentModifiedEvent(context, obj))
|
|
|
|
|
|
|
|
|
2014-04-16 17:54:19 +02:00
|
|
|
def notify_comment_removed(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Notify custom discussion events when a comment or reply is removed"""
|
2014-04-17 13:57:41 +02:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
context = aq_parent(conversation)
|
2022-05-01 23:14:09 +02:00
|
|
|
if getattr(obj, "in_reply_to", None):
|
2014-04-17 13:57:41 +02:00
|
|
|
return notify(ReplyRemovedEvent(context, obj))
|
|
|
|
return notify(CommentRemovedEvent(context, obj))
|
2010-10-03 21:54:59 +02:00
|
|
|
|
2015-05-03 08:22:51 +02:00
|
|
|
|
2011-07-03 20:36:02 +02:00
|
|
|
def notify_content_object_moved(obj, event):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Update all comments of a content object that has been moved."""
|
|
|
|
if (
|
|
|
|
event.oldParent is None
|
|
|
|
or event.newParent is None
|
|
|
|
or event.oldName is None
|
|
|
|
or event.newName is None
|
|
|
|
):
|
2011-07-03 20:36:02 +02:00
|
|
|
return
|
2012-09-17 16:43:19 +02:00
|
|
|
|
2013-04-18 16:12:00 +02:00
|
|
|
# This method is also called for sublocations of moved objects. We
|
|
|
|
# therefore can't assume that event.object == obj and event.
|
|
|
|
# {old,new}{Parent,Name} may refer to the actually moved object further up
|
|
|
|
# in the object hierarchy. The object is already moved at this point. so
|
|
|
|
# obj.getPhysicalPath retruns the new path get the part of the path that
|
|
|
|
# was moved.
|
2022-05-01 23:14:09 +02:00
|
|
|
moved_path = obj.getPhysicalPath()[len(event.newParent.getPhysicalPath()) + 1 :]
|
2012-09-17 16:43:19 +02:00
|
|
|
|
2011-07-04 09:07:42 +02:00
|
|
|
# Remove comments at the old location from catalog
|
2022-05-01 23:14:09 +02:00
|
|
|
catalog = getToolByName(obj, "portal_catalog")
|
|
|
|
old_path = "/".join(
|
|
|
|
event.oldParent.getPhysicalPath() + (event.oldName,) + moved_path,
|
|
|
|
)
|
|
|
|
brains = catalog.searchResults(
|
|
|
|
dict(
|
|
|
|
path={"query": old_path},
|
|
|
|
portal_type="Discussion Item",
|
|
|
|
)
|
2013-04-18 16:12:00 +02:00
|
|
|
)
|
2011-08-04 17:53:21 +02:00
|
|
|
for brain in brains:
|
2011-07-03 20:36:02 +02:00
|
|
|
catalog.uncatalog_object(brain.getPath())
|
2011-07-04 09:07:42 +02:00
|
|
|
# Reindex comment at the new location
|
2011-08-19 20:54:44 +02:00
|
|
|
conversation = IConversation(obj, None)
|
|
|
|
if conversation is not None:
|
|
|
|
for comment in conversation.getComments():
|
2012-09-17 16:43:19 +02:00
|
|
|
comment.reindexObject()
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2011-07-03 20:36:02 +02:00
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
def notify_user(obj, event):
|
2010-06-17 08:57:51 +02:00
|
|
|
"""Tell users when a comment has been added.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
This method composes and sends emails to all users that have added a
|
|
|
|
comment to this conversation and enabled user notification.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
This requires the user_notification setting to be enabled in the
|
|
|
|
discussion control panel.
|
2010-02-08 13:02:54 +01:00
|
|
|
"""
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if user notification is enabled
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-10-29 12:43:46 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
2010-02-13 22:31:17 +01:00
|
|
|
if not settings.user_notification_enabled:
|
|
|
|
return
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Get informations that are necessary to send an email
|
2022-05-01 23:14:09 +02:00
|
|
|
mail_host = getToolByName(obj, "MailHost")
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
2022-05-01 23:14:09 +02:00
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix="plone")
|
2014-12-13 16:20:08 +01:00
|
|
|
sender = mail_settings.email_from_address
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if a sender address is available
|
2010-02-08 13:02:54 +01:00
|
|
|
if not sender:
|
|
|
|
return
|
2010-02-13 22:31:17 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Compose and send emails to all users that have add a comment to this
|
2010-10-30 17:02:05 +02:00
|
|
|
# conversation and enabled user_notification.
|
2010-02-13 22:31:17 +01:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
2010-12-16 00:41:57 +01:00
|
|
|
|
|
|
|
# Avoid sending multiple notification emails to the same person
|
|
|
|
# when he has commented multiple times.
|
|
|
|
emails = set()
|
2010-02-11 22:05:28 +01:00
|
|
|
for comment in conversation.getComments():
|
2013-04-18 16:12:00 +02:00
|
|
|
obj_is_not_the_comment = obj != comment
|
|
|
|
valid_user_email = comment.user_notification and comment.author_email
|
|
|
|
if obj_is_not_the_comment and valid_user_email:
|
2010-12-16 00:41:57 +01:00
|
|
|
emails.add(comment.author_email)
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-12-16 00:41:57 +01:00
|
|
|
if not emails:
|
|
|
|
return
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
subject = translate(_("A comment has been posted."), context=obj.REQUEST)
|
2013-04-18 16:12:00 +02:00
|
|
|
message = translate(
|
|
|
|
Message(
|
2010-12-16 00:41:57 +01:00
|
|
|
MAIL_NOTIFICATION_MESSAGE,
|
2013-04-18 16:12:00 +02:00
|
|
|
mapping={
|
2022-05-02 00:39:34 +02:00
|
|
|
"title": safe_text(content_object.title),
|
2022-05-01 23:14:09 +02:00
|
|
|
"link": content_object.absolute_url() + "/view#" + obj.id,
|
|
|
|
"text": obj.text,
|
2018-06-18 17:04:41 +02:00
|
|
|
},
|
2013-04-18 16:12:00 +02:00
|
|
|
),
|
2018-06-18 17:04:41 +02:00
|
|
|
context=obj.REQUEST,
|
2013-04-18 16:12:00 +02:00
|
|
|
)
|
2010-12-16 00:41:57 +01:00
|
|
|
for email in emails:
|
|
|
|
# Send email
|
2011-02-08 10:28:51 +01:00
|
|
|
try:
|
2018-06-18 17:04:41 +02:00
|
|
|
mail_host.send(
|
|
|
|
message,
|
|
|
|
email,
|
|
|
|
sender,
|
|
|
|
subject,
|
2022-05-01 23:14:09 +02:00
|
|
|
charset="utf-8",
|
2018-06-18 17:04:41 +02:00
|
|
|
)
|
2011-02-08 10:28:51 +01:00
|
|
|
except SMTPException:
|
2018-06-18 17:04:41 +02:00
|
|
|
logger.error(
|
2022-05-01 23:14:09 +02:00
|
|
|
"SMTP exception while trying to send an " + "email from %s to %s",
|
2018-06-18 17:04:41 +02:00
|
|
|
sender,
|
|
|
|
email,
|
|
|
|
)
|
2010-12-16 00:41:57 +01:00
|
|
|
|
2010-10-31 11:34:58 +01:00
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_moderator(obj, event):
|
2010-06-17 08:57:51 +02:00
|
|
|
"""Tell the moderator when a comment needs attention.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
This method sends an email to the moderator if comment moderation a new
|
|
|
|
comment has been added that needs to be approved.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
The moderator_notification setting has to be enabled in the discussion
|
|
|
|
control panel.
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2022-05-01 23:14:09 +02:00
|
|
|
Configure the moderator e-mail address in the discussion control panel.
|
|
|
|
If no moderator is configured but moderator notifications are turned on,
|
|
|
|
the site admin email (from the mail control panel) will be used.
|
2010-02-11 22:05:28 +01:00
|
|
|
"""
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if moderator notification is enabled
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-10-29 12:43:46 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
2010-02-11 22:05:28 +01:00
|
|
|
if not settings.moderator_notification_enabled:
|
|
|
|
return
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Get informations that are necessary to send an email
|
2022-05-01 23:14:09 +02:00
|
|
|
mail_host = getToolByName(obj, "MailHost")
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
2022-05-01 23:14:09 +02:00
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix="plone")
|
2014-12-13 16:20:08 +01:00
|
|
|
sender = mail_settings.email_from_address
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2011-04-07 23:31:56 +02:00
|
|
|
if settings.moderator_email:
|
|
|
|
mto = settings.moderator_email
|
|
|
|
else:
|
|
|
|
mto = sender
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if a sender address is available
|
2010-02-11 22:05:28 +01:00
|
|
|
if not sender:
|
|
|
|
return
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
# Compose email
|
2022-05-01 23:14:41 +02:00
|
|
|
subject = translate(_("A comment has been posted."), context=obj.REQUEST)
|
2013-04-18 16:12:00 +02:00
|
|
|
message = translate(
|
|
|
|
Message(
|
|
|
|
MAIL_NOTIFICATION_MESSAGE_MODERATOR,
|
|
|
|
mapping={
|
2022-05-02 00:39:34 +02:00
|
|
|
"title": safe_text(content_object.title),
|
2022-05-01 23:14:09 +02:00
|
|
|
"link": content_object.absolute_url() + "/view#" + obj.id,
|
|
|
|
"text": obj.text,
|
|
|
|
"commentator": obj.author_email
|
|
|
|
or translate(
|
|
|
|
Message(
|
|
|
|
_(
|
2022-05-01 23:14:41 +02:00
|
|
|
"label_anonymous",
|
|
|
|
default="Anonymous",
|
2019-12-01 19:47:08 +01:00
|
|
|
),
|
2022-05-01 23:14:09 +02:00
|
|
|
),
|
|
|
|
),
|
2018-06-18 17:04:41 +02:00
|
|
|
},
|
2013-04-18 16:12:00 +02:00
|
|
|
),
|
2018-06-18 17:04:41 +02:00
|
|
|
context=obj.REQUEST,
|
2013-04-18 16:12:00 +02:00
|
|
|
)
|
2012-01-09 16:31:52 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Send email
|
2011-02-08 10:28:51 +01:00
|
|
|
try:
|
2022-05-01 23:14:09 +02:00
|
|
|
mail_host.send(message, mto, sender, subject, charset="utf-8")
|
2018-01-25 13:04:11 +01:00
|
|
|
except SMTPException as e:
|
2018-06-18 17:04:41 +02:00
|
|
|
logger.error(
|
2022-05-01 23:14:09 +02:00
|
|
|
"SMTP exception (%s) while trying to send an "
|
|
|
|
+ "email notification to the comment moderator "
|
|
|
|
+ "(from %s to %s, message: %s)",
|
2018-06-18 17:04:41 +02:00
|
|
|
e,
|
|
|
|
sender,
|
|
|
|
mto,
|
|
|
|
message,
|
|
|
|
)
|