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
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
from zope.interface import implements
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
from zope.component.factory import Factory
|
2010-02-11 22:05:28 +01:00
|
|
|
from zope.component import queryUtility
|
2009-05-11 18:52:16 +02:00
|
|
|
|
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
|
|
|
|
|
2009-10-17 18:29:45 +02:00
|
|
|
from Globals 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
|
|
|
|
|
|
|
|
from plone.app.discussion.interfaces import IComment, 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
|
|
|
|
except:
|
|
|
|
# Plone 3:
|
|
|
|
# Dummy imports to make Comment class happy
|
|
|
|
from OFS.Traversable import Traversable as CatalogAware
|
|
|
|
from OFS.Traversable import Traversable as WorkflowAware
|
|
|
|
|
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'
|
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
|
|
|
|
"""
|
2009-06-24 15:18:54 +02:00
|
|
|
return self.portal_type
|
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
|
|
|
|
|
|
|
def notify_user(obj, event):
|
|
|
|
"""Tell the user when a comment is added
|
|
|
|
"""
|
2010-02-11 22:05:28 +01:00
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
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-02-08 13:02:54 +01:00
|
|
|
if not sender:
|
|
|
|
return
|
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
for comment in conversation.getComments():
|
|
|
|
if 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)
|
|
|
|
|
|
|
|
def notify_moderator(obj, index):
|
|
|
|
"""Tell the moderator when a comment needs attention
|
|
|
|
"""
|
2010-02-08 13:02:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
# check if notification is enabled
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
if not settings.moderator_notification_enabled:
|
|
|
|
return
|
|
|
|
|
|
|
|
# check if comment review workflow is enabled
|
|
|
|
wf = getToolByName(obj, 'portal_workflow')
|
|
|
|
if wf.getChainForPortalType('Discussion Item') != \
|
|
|
|
('comment_review_workflow',):
|
|
|
|
return
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
# check if a sender address is available
|
|
|
|
if not sender:
|
|
|
|
return
|
|
|
|
|
|
|
|
conversation = aq_parent(obj)
|
|
|
|
content_object = aq_parent(conversation)
|
|
|
|
|
|
|
|
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(),)
|
|
|
|
mail_host.send(message, mto, sender, subject)
|