2009-05-13 16:54:06 +02:00
|
|
|
"""The default comment class and factory.
|
2009-05-11 18:52:16 +02:00
|
|
|
"""
|
2009-05-30 08:08:44 +02:00
|
|
|
from datetime import datetime
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2010-03-16 16:06:43 +01:00
|
|
|
from zope.annotation.interfaces import IAnnotations, 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:34:02 +02:00
|
|
|
from zope.component import getUtility
|
2009-05-11 18:52:16 +02:00
|
|
|
|
2010-03-16 16:06:43 +01:00
|
|
|
from zope.interface import implements
|
|
|
|
|
2009-07-04 18:18:48 +02:00
|
|
|
from Acquisition import aq_parent, 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
|
|
|
|
|
|
|
|
from OFS.Traversable import Traversable
|
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
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.
|
|
|
|
from Products.CMFCore.CMFCatalogAware import CatalogAware
|
|
|
|
from Products.CMFCore.CMFCatalogAware import WorkflowAware
|
2010-03-11 20:23:53 +01:00
|
|
|
PLONE_4 = True
|
2009-10-16 14:11:58 +02:00
|
|
|
except:
|
|
|
|
# Plone 3:
|
|
|
|
# Dummy imports to make Comment class happy
|
|
|
|
from OFS.Traversable import Traversable as CatalogAware
|
|
|
|
from OFS.Traversable import Traversable as WorkflowAware
|
2010-03-11 20:23:53 +01:00
|
|
|
PLONE_4 = False
|
2009-10-16 14:11:58 +02:00
|
|
|
|
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-02-08 13:02:54 +01:00
|
|
|
|
|
|
|
author_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):
|
2009-05-30 08:08:44 +02:00
|
|
|
self.creation_date = self.modification_date = datetime.now()
|
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):
|
2009-05-13 16:54:06 +02:00
|
|
|
"""The id of the comment, as a string
|
|
|
|
"""
|
|
|
|
return self.id
|
2009-10-16 14:11:58 +02:00
|
|
|
|
2009-08-20 04:06:15 +02:00
|
|
|
def getText(self):
|
|
|
|
'''the text'''
|
|
|
|
return self.text
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Title(self):
|
|
|
|
"""The title of the comment
|
|
|
|
"""
|
|
|
|
return self.title
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def Creator(self):
|
|
|
|
"""The name of the person who wrote the comment
|
|
|
|
"""
|
|
|
|
return self.creator
|
2009-06-13 18:46:37 +02:00
|
|
|
|
2009-06-24 10:25:11 +02:00
|
|
|
def Type(self):
|
|
|
|
"""The Discussion Item content type
|
|
|
|
"""
|
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 :(
|
|
|
|
|
|
|
|
def opaqueItems(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def opaqueIds(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def opaqueValues(self):
|
|
|
|
return []
|
|
|
|
|
2009-05-23 18:37:10 +02:00
|
|
|
CommentFactory = Factory(Comment)
|
|
|
|
|
|
|
|
def notify_workflow(obj, event):
|
|
|
|
"""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)
|
|
|
|
|
|
|
|
def notify_content_object(obj, event):
|
|
|
|
"""Tell the content object when a comment is added
|
|
|
|
"""
|
|
|
|
content_obj = aq_parent(aq_parent(obj))
|
2010-02-11 22:05:28 +01:00
|
|
|
content_obj.reindexObject(idxs=('total_comments',
|
|
|
|
'last_comment_date',
|
|
|
|
'commentators',))
|
2010-02-08 13:02:54 +01:00
|
|
|
|
2010-03-16 16:06:43 +01:00
|
|
|
def notify_content_object_deleted(obj, event):
|
|
|
|
"""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-02-08 13:02:54 +01:00
|
|
|
def notify_user(obj, event):
|
2010-06-17 08:57:51 +02:00
|
|
|
"""Tell users when a comment has been added.
|
|
|
|
|
|
|
|
This method composes and sends emails to all users that have added a
|
|
|
|
comment to this conversation and enabled user notification.
|
|
|
|
|
|
|
|
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:34:02 +02:00
|
|
|
registry = getUtility(IRegistry)
|
2010-02-13 22:31:17 +01:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
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
|
|
|
|
# conversation and enabled author_notification.
|
2010-02-13 22:31:17 +01:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
2010-02-08 13:02:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
for comment in conversation.getComments():
|
2010-02-13 22:31:17 +01:00
|
|
|
if obj != comment and \
|
|
|
|
comment.author_notification and \
|
|
|
|
comment.author_email:
|
|
|
|
subject = "A comment has been posted."
|
|
|
|
message = "A comment with the title '%s' has been posted here: %s" \
|
|
|
|
% (obj.title,
|
|
|
|
content_object.absolute_url(),)
|
|
|
|
mail_host.send(message, comment.author_email, sender, subject)
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
def notify_moderator(obj, index):
|
2010-06-17 08:57:51 +02:00
|
|
|
"""Tell the moderator when a comment needs attention.
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
content type.
|
2010-02-11 22:05:28 +01:00
|
|
|
"""
|
2010-02-08 13:02:54 +01:00
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if moderator notification is enabled
|
2010-07-12 15:34:02 +02:00
|
|
|
registry = getUtility(IRegistry)
|
2010-02-11 22:05:28 +01:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
if not settings.moderator_notification_enabled:
|
|
|
|
return
|
|
|
|
|
2010-06-17 08:57:51 +02:00
|
|
|
# Check if comment review workflow is enabled
|
2010-02-11 22:05:28 +01:00
|
|
|
wf = getToolByName(obj, 'portal_workflow')
|
|
|
|
if wf.getChainForPortalType('Discussion Item') != \
|
|
|
|
('comment_review_workflow',):
|
|
|
|
return
|
|
|
|
|
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-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
|
|
|
|
|
|
|
# Compose email
|
2010-02-11 22:05:28 +01:00
|
|
|
comment = conversation.getComments().next()
|
|
|
|
subject = "A comment has been posted."
|
|
|
|
message = "A comment with the title '%s' has been posted here: %s" \
|
|
|
|
% (obj.title,
|
|
|
|
content_object.absolute_url(),)
|
2010-06-17 08:57:51 +02:00
|
|
|
|
|
|
|
# Send email
|
2010-03-11 20:23:53 +01:00
|
|
|
if PLONE_4:
|
|
|
|
mail_host.send(message, mto, sender, subject)
|
|
|
|
else:
|
|
|
|
mail_host.secureSend(message, mto, sender, subject=subject)
|