2010-10-02 20:16:49 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-05-13 16:54:06 +02:00
|
|
|
"""The default comment class and factory.
|
2009-05-11 18:52:16 +02:00
|
|
|
"""
|
2010-11-04 16:56:12 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2009-05-30 08:08:44 +02:00
|
|
|
from datetime import datetime
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2010-11-04 16:56:12 +01:00
|
|
|
from smtplib import SMTPException
|
|
|
|
|
2010-07-13 12:40:24 +02:00
|
|
|
from zope.annotation.interfaces import IAnnotatable
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
from zope.component.factory import Factory
|
2010-07-12 15:47:53 +02:00
|
|
|
from zope.component import queryUtility
|
2009-05-11 18:52:16 +02:00
|
|
|
|
2010-08-31 20:07:51 +02:00
|
|
|
from zope.i18n import translate
|
|
|
|
from zope.i18nmessageid import Message
|
2010-03-16 16:06:43 +01:00
|
|
|
from zope.interface import implements
|
|
|
|
|
2010-09-29 09:56:36 +02:00
|
|
|
from Acquisition import aq_parent, aq_base, Implicit
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
from AccessControl.Role import RoleManager
|
|
|
|
from AccessControl.Owned import Owned
|
|
|
|
|
2010-06-18 14:01:44 +02:00
|
|
|
from persistent import Persistent
|
2009-05-11 18:52:16 +02:00
|
|
|
|
2009-05-23 18:12:45 +02:00
|
|
|
from Products.CMFCore.DynamicType import DynamicType
|
2009-10-17 18:29:45 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
|
2010-11-23 15:03:47 +01:00
|
|
|
from Products.CMFPlone.utils import safe_unicode
|
|
|
|
|
2009-10-17 18:29:45 +02:00
|
|
|
from OFS.Traversable import Traversable
|
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
2010-08-31 20:07:51 +02:00
|
|
|
from plone.app.discussion import PloneAppDiscussionMessageFactory as _
|
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
|
2009-10-16 14:11:58 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Plone 4:
|
|
|
|
# Mixin CatalogAware and WorkflowAware into the Comment class
|
|
|
|
# is necessary for comments to be indexed in Plone4.
|
2010-10-06 11:02:26 +02:00
|
|
|
from Products.CMFCore.CMFCatalogAware import CatalogAware # pylint: disable-msg=W0611
|
|
|
|
from Products.CMFCore.CMFCatalogAware import WorkflowAware # pylint: disable-msg=W0611
|
2010-03-11 20:23:53 +01:00
|
|
|
PLONE_4 = True
|
2010-10-03 21:54:59 +02:00
|
|
|
except: # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
# Plone 3:
|
|
|
|
# Dummy imports to make Comment class happy
|
2010-10-06 11:02:26 +02:00
|
|
|
from OFS.Traversable import Traversable as CatalogAware # pylint: disable-msg=W0611
|
|
|
|
from OFS.Traversable import Traversable as WorkflowAware # pylint: disable-msg=W0611
|
2010-03-11 20:23:53 +01:00
|
|
|
PLONE_4 = False
|
2009-10-16 14:11:58 +02:00
|
|
|
|
2010-09-29 09:56:36 +02:00
|
|
|
COMMENT_TITLE = _(u"comment_title",
|
2010-10-02 20:16:49 +02:00
|
|
|
default=u"${creator} on ${content}")
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-08-31 20:07:51 +02:00
|
|
|
MAIL_NOTIFICATION_MESSAGE = _(u"mail_notification_message",
|
2010-10-30 17:02:05 +02:00
|
|
|
default=u"A comment on '${title}' "
|
2010-09-01 09:19:51 +02:00
|
|
|
"has been posted here: ${link}")
|
2010-08-28 17:35:21 +02:00
|
|
|
|
2010-11-04 16:56:12 +01:00
|
|
|
logger = logging.getLogger("plone.app.discussion")
|
|
|
|
|
2009-05-23 18:12:45 +02:00
|
|
|
|
2009-10-17 18:29:45 +02:00
|
|
|
class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable,
|
|
|
|
RoleManager, Owned, Implicit, Persistent):
|
2009-05-11 18:52:16 +02:00
|
|
|
"""A comment.
|
2009-06-13 18:46:37 +02: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.
|
|
|
|
"""
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
implements(IComment)
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-06-24 10:25:11 +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
|
|
|
|
fti_title = 'Comment'
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
__parent__ = None
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
comment_id = None # long
|
|
|
|
in_reply_to = None # long
|
2009-05-11 18:52:16 +02:00
|
|
|
|
|
|
|
title = u""
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
mime_type = "text/plain"
|
2009-05-11 18:52:16 +02:00
|
|
|
text = u""
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
creator = None
|
|
|
|
creation_date = None
|
|
|
|
modification_date = None
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
author_username = None
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
author_name = None
|
|
|
|
author_email = None
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
user_notification = None
|
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().
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
def __init__(self):
|
2010-09-07 14:03:07 +02:00
|
|
|
self.creation_date = self.modification_date = datetime.utcnow()
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
@property
|
|
|
|
def __name__(self):
|
2009-05-18 16:16:48 +02:00
|
|
|
return self.comment_id and unicode(self.comment_id) or None
|
2009-06-13 18:46:37 +02: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
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-11 18:52:16 +02:00
|
|
|
def getId(self):
|
2010-09-29 09:56:36 +02:00
|
|
|
"""The id of the comment, as a string.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
return self.id
|
2009-10-16 14:11:58 +02:00
|
|
|
|
2009-08-20 04:06:15 +02:00
|
|
|
def getText(self):
|
2010-09-29 09:56:36 +02:00
|
|
|
"""The body text of a comment.
|
|
|
|
"""
|
2009-08-20 04:06:15 +02:00
|
|
|
return self.text
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Title(self):
|
2010-09-29 09:56:36 +02:00
|
|
|
"""The title of the comment.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2010-09-29 09:56:36 +02:00
|
|
|
if not self.creator:
|
2010-12-16 00:52:56 +01:00
|
|
|
creator = translate(Message(_(u"label_anonymous",
|
2010-10-02 20:16:49 +02:00
|
|
|
default=u"Anonymous")))
|
2010-09-29 12:52:11 +02:00
|
|
|
else:
|
|
|
|
creator = self.creator
|
2010-10-04 15:48:54 +02:00
|
|
|
creator = creator
|
2010-10-22 12:14:07 +02: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(
|
|
|
|
Message(COMMENT_TITLE,
|
2010-09-29 12:52:11 +02:00
|
|
|
mapping={'creator': creator,
|
2010-11-23 15:03:47 +01:00
|
|
|
'content': safe_unicode(content.Title())}))
|
2010-09-29 09:56:36 +02:00
|
|
|
return title
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Creator(self):
|
2010-09-29 09:56:36 +02:00
|
|
|
"""The name of the person who wrote the comment.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
return self.creator
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-06-24 10:25:11 +02:00
|
|
|
def Type(self):
|
2010-09-29 09:56:36 +02:00
|
|
|
"""The Discussion Item content type.
|
2009-06-24 10:25:11 +02:00
|
|
|
"""
|
2010-03-18 15:42:52 +01:00
|
|
|
return self.fti_title
|
2009-06-24 10:25:11 +02:00
|
|
|
|
2009-10-16 14:11:58 +02:00
|
|
|
# CMF's event handlers assume any IDynamicType has these :(
|
|
|
|
|
2010-10-03 21:54:59 +02:00
|
|
|
def opaqueItems(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
|
|
|
|
2010-10-03 21:54:59 +02:00
|
|
|
def opaqueIds(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
|
|
|
|
2010-10-03 21:54:59 +02:00
|
|
|
def opaqueValues(self): # pragma: no cover
|
2009-10-16 14:11:58 +02:00
|
|
|
return []
|
|
|
|
|
2009-05-23 18:37:10 +02:00
|
|
|
CommentFactory = Factory(Comment)
|
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_workflow(obj, event):
|
2009-05-23 18:37:10 +02:00
|
|
|
"""Tell the workflow tool when a comment is added
|
|
|
|
"""
|
|
|
|
tool = getToolByName(obj, 'portal_workflow', None)
|
|
|
|
if tool is not None:
|
2009-07-04 18:18:48 +02:00
|
|
|
tool.notifyCreated(obj)
|
|
|
|
|
2010-08-28 18:07:44 +02:00
|
|
|
def notify_content_object(obj, event):
|
2009-07-04 18:18:48 +02:00
|
|
|
"""Tell the content object when a comment is added
|
|
|
|
"""
|
|
|
|
content_obj = aq_parent(aq_parent(obj))
|
2010-12-16 00:52:56 +01:00
|
|
|
content_obj.reindexObject(idxs=('total_comments',
|
|
|
|
'last_comment_date',
|
2010-02-11 22:05:28 +01:00
|
|
|
'commentators',))
|
2010-02-08 13:02:54 +01: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
|
|
|
|
deleted.
|
|
|
|
"""
|
|
|
|
if IAnnotatable.providedBy(obj):
|
|
|
|
conversation = IConversation(obj)
|
|
|
|
for comment in conversation.getComments():
|
|
|
|
del conversation[comment.id]
|
2010-10-03 21:54:59 +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.
|
2010-12-16 00:52:56 +01:00
|
|
|
|
|
|
|
This method composes and sends emails to all users that have added a
|
2010-06-17 08:57:51 +02:00
|
|
|
comment to this conversation and enabled user notification.
|
2010-12-16 00:52:56 +01:00
|
|
|
|
|
|
|
This requires the user_notification setting to be enabled in the
|
|
|
|
discussion control panel.
|
2010-02-08 13:02:54 +01:00
|
|
|
"""
|
2010-02-13 22:31:17 +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
|
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Get informations that are necessary to send an email
|
2010-02-08 13:02:54 +01:00
|
|
|
mail_host = getToolByName(obj, 'MailHost')
|
|
|
|
portal_url = getToolByName(obj, 'portal_url')
|
|
|
|
portal = portal_url.getPortalObject()
|
|
|
|
sender = portal.getProperty('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():
|
2010-12-16 00:41:57 +01:00
|
|
|
if (obj != comment and
|
|
|
|
comment.user_notification and comment.author_email):
|
|
|
|
emails.add(comment.author_email)
|
|
|
|
|
|
|
|
if not emails:
|
|
|
|
return
|
|
|
|
|
|
|
|
subject = translate(_(u"A comment has been posted."),
|
|
|
|
context=obj.REQUEST)
|
|
|
|
message = translate(Message(
|
|
|
|
MAIL_NOTIFICATION_MESSAGE,
|
2011-03-09 04:07:59 +01:00
|
|
|
mapping={'title': safe_unicode(content_object.title),
|
2010-12-16 00:41:57 +01:00
|
|
|
'link': content_object.absolute_url()}),
|
|
|
|
context=obj.REQUEST)
|
|
|
|
for email in emails:
|
|
|
|
# Send email
|
|
|
|
if PLONE_4:
|
|
|
|
try:
|
|
|
|
mail_host.send(message,
|
|
|
|
email,
|
|
|
|
sender,
|
|
|
|
subject,
|
|
|
|
charset='utf-8')
|
|
|
|
except SMTPException:
|
|
|
|
logger.error('SMTP exception while trying to send an ' +
|
|
|
|
'email from %s to %s',
|
|
|
|
sender,
|
|
|
|
email)
|
|
|
|
else: # pragma: no cover
|
|
|
|
try:
|
|
|
|
mail_host.secureSend(message,
|
|
|
|
email,
|
|
|
|
sender,
|
|
|
|
subject=subject,
|
|
|
|
charset='utf-8')
|
|
|
|
except SMTPException:
|
|
|
|
logger.error('SMTP exception while trying to send an ' +
|
|
|
|
'email from %s to %s',
|
|
|
|
sender,
|
|
|
|
email)
|
|
|
|
|
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.
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
This method sends an email to the site admin (mail control panel setting)
|
|
|
|
if comment moderation is enabled and a new comment has been added that
|
2010-12-16 00:52:56 +01:00
|
|
|
needs to be approved.
|
|
|
|
|
|
|
|
This requires the moderator_notification to be enabled in the discussion
|
|
|
|
control panel and the comment_review_workflow enabled for the comment
|
2010-06-17 08:57:51 +02:00
|
|
|
content type.
|
2010-02-11 22:05:28 +01:00
|
|
|
"""
|
2010-12-16 00:52:56 +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
|
2010-12-16 00:52:56 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Get informations that are necessary to send an email
|
2010-02-11 22:05:28 +01:00
|
|
|
mail_host = getToolByName(obj, 'MailHost')
|
|
|
|
portal_url = getToolByName(obj, 'portal_url')
|
|
|
|
portal = portal_url.getPortalObject()
|
|
|
|
sender = portal.getProperty('email_from_address')
|
|
|
|
mto = portal.getProperty('email_from_address')
|
2010-12-16 00:52:56 +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
|
|
|
|
|
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
2010-06-17 08:57:51 +02:00
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
# Compose email
|
2010-08-28 17:35:21 +02:00
|
|
|
#comment = conversation.getComments().next()
|
2010-09-01 09:32:28 +02:00
|
|
|
subject = translate(_(u"A comment has been posted."), context=obj.REQUEST)
|
2010-08-31 20:07:51 +02:00
|
|
|
message = translate(Message(MAIL_NOTIFICATION_MESSAGE,
|
2011-03-09 04:07:59 +01:00
|
|
|
mapping={'title': safe_unicode(content_object.title),
|
2010-08-31 20:07:51 +02:00
|
|
|
'link': content_object.absolute_url()}),
|
|
|
|
context=obj.REQUEST)
|
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Send email
|
2010-03-11 20:23:53 +01:00
|
|
|
if PLONE_4:
|
2010-11-04 16:56:12 +01:00
|
|
|
try:
|
|
|
|
mail_host.send(message, mto, sender, subject, charset='utf-8')
|
2010-11-29 09:27:09 +01:00
|
|
|
except SMTPException, e:
|
|
|
|
logger.error('SMTP exception (%s) while trying to send an ' +
|
|
|
|
'email notification to the comment moderator ' +
|
|
|
|
'(from %s to %s, message: %s)',
|
|
|
|
e,
|
2010-11-04 16:56:12 +01:00
|
|
|
sender,
|
2010-11-29 09:27:09 +01:00
|
|
|
mto,
|
|
|
|
message)
|
2010-11-04 16:56:12 +01:00
|
|
|
else: # pragma: no cover
|
|
|
|
try:
|
2010-12-16 00:52:56 +01:00
|
|
|
mail_host.secureSend(message,
|
|
|
|
mto,
|
|
|
|
sender,
|
|
|
|
subject=subject,
|
2010-11-04 16:56:12 +01:00
|
|
|
charset='utf-8')
|
2010-11-29 09:27:09 +01:00
|
|
|
except SMTPException, e:
|
|
|
|
logger.error('SMTP exception (%s) while trying to send an ' +
|
|
|
|
'email notification to the comment moderator ' +
|
|
|
|
'(from %s to %s, message: %s)',
|
|
|
|
e,
|
2010-11-04 16:56:12 +01:00
|
|
|
sender,
|
2010-11-29 09:27:09 +01:00
|
|
|
mto,
|
|
|
|
message)
|