2009-05-13 16:54:06 +02:00
|
|
|
"""The conversation and replies adapters
|
|
|
|
|
|
|
|
The conversation is responsible for storing all comments. It provides a
|
|
|
|
dict-like API for accessing comments, where keys are integers and values
|
|
|
|
are IComment objects. It also provides features for finding comments quickly.
|
|
|
|
|
|
|
|
The two IReplies adapters - one for the IConversation and one for IComment -
|
|
|
|
manipulate the same data structures, but provide an API for finding and
|
|
|
|
manipulating the comments directly in reply to a particular comment or at the
|
|
|
|
top level of the conversation.
|
|
|
|
"""
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
import time
|
|
|
|
|
2009-05-16 11:47:10 +02:00
|
|
|
from persistent import Persistent
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
from zope.interface import implements, implementer
|
2011-04-15 06:29:46 +02:00
|
|
|
from zope.component import adapts
|
|
|
|
from zope.component import adapter
|
2009-05-23 06:55:06 +02:00
|
|
|
|
|
|
|
from zope.annotation.interfaces import IAnnotations, IAnnotatable
|
2009-05-16 17:05:22 +02:00
|
|
|
|
|
|
|
from zope.event import notify
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-06-18 23:02:32 +02:00
|
|
|
from Acquisition import aq_base, aq_inner, aq_parent
|
2009-05-18 16:16:48 +02:00
|
|
|
from Acquisition import Explicit
|
2009-05-18 16:23:46 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
from OFS.Traversable import Traversable
|
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
from OFS.event import ObjectWillBeAddedEvent
|
|
|
|
from OFS.event import ObjectWillBeRemovedEvent
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-10-16 02:42:08 +02:00
|
|
|
from zope.container.contained import ContainerModifiedEvent
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2010-11-28 13:01:27 +01:00
|
|
|
from zope.lifecycleevent import ObjectCreatedEvent
|
|
|
|
|
2011-02-08 10:28:51 +01:00
|
|
|
from zope.lifecycleevent import ObjectAddedEvent
|
|
|
|
from zope.lifecycleevent import ObjectRemovedEvent
|
2009-05-13 17:20:02 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
from BTrees.OIBTree import OIBTree
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2011-02-08 10:28:51 +01:00
|
|
|
from BTrees.LOBTree import LOBTree
|
|
|
|
from BTrees.LLBTree import LLSet
|
2009-05-16 13:21:23 +02:00
|
|
|
|
2012-05-29 11:27:59 +02:00
|
|
|
from Products.CMFPlone.interfaces import IHideFromBreadcrumbs
|
|
|
|
|
2010-01-27 18:11:55 +01:00
|
|
|
from plone.app.discussion.interfaces import IConversation
|
|
|
|
from plone.app.discussion.interfaces import IReplies
|
2009-05-18 16:23:46 +02:00
|
|
|
from plone.app.discussion.comment import Comment
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2012-06-13 13:17:22 +02:00
|
|
|
from AccessControl.SpecialUsers import nobody as user_nobody
|
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
ANNOTATION_KEY = 'plone.app.discussion:conversation'
|
2009-05-16 17:05:22 +02:00
|
|
|
|
2010-08-28 18:08:36 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
class Conversation(Traversable, Persistent, Explicit):
|
2009-05-13 16:54:06 +02:00
|
|
|
"""A conversation is a container for all comments on a content object.
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
It manages internal data structures for comment threading and efficient
|
|
|
|
comment lookup.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2012-05-29 11:27:59 +02:00
|
|
|
implements(IConversation, IHideFromBreadcrumbs)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-23 13:52:57 +02:00
|
|
|
__allow_access_to_unprotected_subobjects__ = True
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-23 13:52:57 +02:00
|
|
|
def __init__(self, id="++conversation++default"):
|
2009-05-13 16:54:06 +02:00
|
|
|
self.id = id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# username -> count of comments; key is removed when count reaches 0
|
|
|
|
self._commentators = OIBTree()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# id -> comment - find comment by id
|
2009-05-23 16:18:35 +02:00
|
|
|
self._comments = LOBTree()
|
|
|
|
|
2010-01-27 18:11:55 +01:00
|
|
|
# id -> LLSet (children) - find all children for a given comment.
|
|
|
|
# 0 signifies root.
|
2009-05-16 13:21:23 +02:00
|
|
|
self._children = LOBTree()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def getId(self):
|
2009-05-23 13:52:57 +02:00
|
|
|
"""Get the id of the conversation. This is used to construct a
|
|
|
|
URL.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
return self.id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def enabled(self):
|
2010-01-27 18:11:55 +01:00
|
|
|
parent = aq_inner(self.__parent__)
|
2011-04-15 06:29:46 +02:00
|
|
|
return parent.restrictedTraverse('@@conversation_view').enabled()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2014-05-13 07:07:37 +02:00
|
|
|
@property
|
2009-05-13 16:54:06 +02:00
|
|
|
def total_comments(self):
|
2013-04-18 16:12:00 +02:00
|
|
|
public_comments = [
|
2014-05-13 07:07:37 +02:00
|
|
|
x for x in self._comments.values()
|
2013-04-18 16:12:00 +02:00
|
|
|
if user_nobody.has_permission('View', x)
|
|
|
|
]
|
2012-06-13 13:17:22 +02:00
|
|
|
return len(public_comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2014-05-13 07:07:37 +02:00
|
|
|
@property
|
2009-05-13 16:54:06 +02:00
|
|
|
def last_comment_date(self):
|
2013-03-28 14:28:22 +01:00
|
|
|
# self._comments is an Instance of a btree. The keys
|
|
|
|
# are always ordered
|
|
|
|
comment_keys = self._comments.keys()
|
|
|
|
for comment_key in reversed(comment_keys):
|
2014-05-13 07:07:37 +02:00
|
|
|
comment = self._comments[comment_key]
|
2013-03-28 14:28:22 +01:00
|
|
|
if user_nobody.has_permission('View', comment):
|
|
|
|
return comment.creation_date
|
|
|
|
return None
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
@property
|
|
|
|
def commentators(self):
|
2009-07-02 19:50:20 +02:00
|
|
|
return self._commentators
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2014-05-13 07:07:37 +02:00
|
|
|
@property
|
2013-03-28 14:28:22 +01:00
|
|
|
def public_commentators(self):
|
|
|
|
retval = set()
|
2014-05-13 07:07:37 +02:00
|
|
|
for comment in self._comments.values():
|
2013-03-28 14:28:22 +01:00
|
|
|
if not user_nobody.has_permission('View', comment):
|
|
|
|
continue
|
|
|
|
retval.add(comment.author_username)
|
|
|
|
return tuple(retval)
|
|
|
|
|
2009-06-16 13:52:00 +02:00
|
|
|
def objectIds(self):
|
|
|
|
return self._comments.keys()
|
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def getComments(self, start=0, size=None):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get unthreaded comments
|
|
|
|
"""
|
2009-05-24 18:04:49 +02:00
|
|
|
count = 0l
|
|
|
|
for comment in self._comments.values(min=start):
|
2011-08-18 08:39:25 +02:00
|
|
|
# Yield the acquisition wrapped comment
|
|
|
|
yield self[comment.id]
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
count += 1
|
|
|
|
if size and count > size:
|
|
|
|
return
|
|
|
|
|
|
|
|
def getThreads(self, start=0, size=None, root=0, depth=None):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get threaded comments
|
|
|
|
"""
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
def recurse(comment_id, d=0):
|
|
|
|
# Yield the current comment before we look for its children
|
2009-06-13 18:46:37 +02:00
|
|
|
yield {'id': comment_id, 'comment': self[comment_id], 'depth': d}
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
# Recurse if there are children and we are not out of our depth
|
|
|
|
if depth is None or d + 1 < depth:
|
|
|
|
children = self._children.get(comment_id, None)
|
|
|
|
if children is not None:
|
|
|
|
for child_id in children:
|
2012-01-14 07:13:39 +01:00
|
|
|
for value in recurse(child_id, d + 1):
|
2009-05-24 18:04:49 +02:00
|
|
|
yield value
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
# Find top level threads
|
|
|
|
comments = self._children.get(root, None)
|
|
|
|
if comments is not None:
|
|
|
|
count = 0l
|
|
|
|
for comment_id in comments.keys(min=start):
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
# Abort if we have found all the threads we want
|
|
|
|
count += 1
|
|
|
|
if size and count > size:
|
|
|
|
return
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:04:49 +02:00
|
|
|
# Let the closure recurse
|
|
|
|
for value in recurse(comment_id):
|
|
|
|
yield value
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def addComment(self, comment):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Add a new comment. The parent id should have been set already. The
|
|
|
|
comment id may be modified to find a free key. The id used will be
|
|
|
|
returned.
|
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
# Make sure we don't have a wrapped object
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
comment = aq_base(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
id = long(time.time() * 1e6)
|
|
|
|
while id in self._comments:
|
|
|
|
id += 1
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
comment.comment_id = id
|
2009-05-16 17:05:22 +02:00
|
|
|
notify(ObjectWillBeAddedEvent(comment, self, id))
|
2009-05-16 12:34:12 +02:00
|
|
|
self._comments[id] = comment
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2010-09-28 12:37:40 +02:00
|
|
|
comment.__parent__ = aq_base(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
# Record unique users who've commented (for logged in users only)
|
2009-05-18 16:16:48 +02:00
|
|
|
commentator = comment.author_username
|
|
|
|
if commentator:
|
|
|
|
if not commentator in self._commentators:
|
|
|
|
self._commentators[commentator] = 0
|
|
|
|
self._commentators[commentator] += 1
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
reply_to = comment.in_reply_to
|
2009-05-18 16:16:48 +02:00
|
|
|
if not reply_to:
|
|
|
|
# top level comments are in reply to the faux id 0
|
|
|
|
comment.in_reply_to = reply_to = 0
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
if not reply_to in self._children:
|
2009-05-16 13:21:23 +02:00
|
|
|
self._children[reply_to] = LLSet()
|
2009-05-16 12:34:12 +02:00
|
|
|
self._children[reply_to].insert(id)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2011-04-14 18:17:29 +02:00
|
|
|
# Add the annotation if not already done
|
|
|
|
annotions = IAnnotations(self.__parent__)
|
|
|
|
if not ANNOTATION_KEY in annotions:
|
2011-04-14 21:06:24 +02:00
|
|
|
annotions[ANNOTATION_KEY] = aq_base(self)
|
2011-04-14 18:17:29 +02:00
|
|
|
|
2009-05-17 20:38:45 +02:00
|
|
|
# Notify that the object is added. The object must here be
|
|
|
|
# acquisition wrapped or the indexing will fail.
|
2010-11-28 12:39:19 +01:00
|
|
|
notify(ObjectCreatedEvent(comment))
|
2009-05-17 20:38:45 +02:00
|
|
|
notify(ObjectAddedEvent(comment.__of__(self), self, id))
|
2009-05-16 17:05:22 +02:00
|
|
|
notify(ContainerModifiedEvent(self))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
return id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# Dict API
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self._comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self._comments
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
def __getitem__(self, key):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get an item by its long key
|
|
|
|
"""
|
2012-11-14 13:25:10 +01:00
|
|
|
try:
|
|
|
|
comment_id = long(key)
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
return self._comments[comment_id].__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
def __delitem__(self, key, suppress_container_modified=False):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Delete an item by its long key
|
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
key = long(key)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
comment = self[key].__of__(self)
|
2009-05-18 16:16:48 +02:00
|
|
|
commentator = comment.author_username
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
notify(ObjectWillBeRemovedEvent(comment, self, key))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
# Remove all children
|
|
|
|
for child_id in self._children.get(key, []):
|
|
|
|
# avoid sending ContainerModifiedEvent multiple times
|
|
|
|
self.__delitem__(child_id, suppress_container_modified=True)
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
# Remove the comment from _comments
|
2009-05-23 16:18:35 +02:00
|
|
|
self._comments.pop(key)
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
# Remove this comment as a child of its parent
|
|
|
|
if not suppress_container_modified:
|
|
|
|
parent = comment.in_reply_to
|
|
|
|
if parent is not None:
|
|
|
|
parent_children = self._children.get(parent, None)
|
|
|
|
if parent_children is not None and key in parent_children:
|
|
|
|
parent_children.remove(key)
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
# Remove commentators
|
2009-05-18 16:16:48 +02:00
|
|
|
if commentator and commentator in self._commentators:
|
|
|
|
if self._commentators[commentator] <= 1:
|
|
|
|
del self._commentators[commentator]
|
|
|
|
else:
|
|
|
|
self._commentators[commentator] -= 1
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
notify(ObjectRemovedEvent(comment, self, key))
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 18:19:21 +02:00
|
|
|
if not suppress_container_modified:
|
|
|
|
notify(ContainerModifiedEvent(self))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def get(self, key, default=None):
|
2009-05-18 17:15:36 +02:00
|
|
|
comment = self._comments.get(long(key), default)
|
|
|
|
if comment is default:
|
|
|
|
return default
|
|
|
|
return comment.__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
def keys(self):
|
|
|
|
return self._comments.keys()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def items(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
return [(i[0], i[1].__of__(self),) for i in self._comments.items()]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def values(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
return [v.__of__(self) for v in self._comments.values()]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iterkeys(self):
|
|
|
|
return self._comments.iterkeys()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def itervalues(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
for v in self._comments.itervalues():
|
|
|
|
yield v.__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iteritems(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
for k, v in self._comments.iteritems():
|
|
|
|
yield (k, v.__of__(self),)
|
2009-05-13 17:20:02 +02:00
|
|
|
|
2010-01-28 14:44:56 +01:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
@implementer(IConversation)
|
|
|
|
@adapter(IAnnotatable)
|
|
|
|
def conversationAdapterFactory(content):
|
2011-04-14 18:17:29 +02:00
|
|
|
"""
|
|
|
|
Adapter factory to fetch the default conversation from annotations.
|
2009-05-13 17:20:02 +02:00
|
|
|
"""
|
2013-08-26 14:49:26 +02:00
|
|
|
annotations = IAnnotations(content)
|
|
|
|
if not ANNOTATION_KEY in annotations:
|
2009-05-16 17:05:22 +02:00
|
|
|
conversation = Conversation()
|
2009-06-18 22:53:25 +02:00
|
|
|
conversation.__parent__ = aq_base(content)
|
|
|
|
else:
|
2013-08-26 14:49:26 +02:00
|
|
|
conversation = annotations[ANNOTATION_KEY]
|
2009-06-18 22:53:25 +02:00
|
|
|
return conversation.__of__(content)
|
2009-05-13 17:20:02 +02:00
|
|
|
|
2010-01-28 14:44:56 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
from Products.LinguaPlone.interfaces import ITranslatable
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
2012-01-14 07:13:39 +01:00
|
|
|
@implementer(IConversation) # pragma: no cover
|
|
|
|
@adapter(IAnnotatable) # pragma: no cover
|
|
|
|
def conversationCanonicalAdapterFactory(content): # pragma: no cover
|
2010-01-28 14:44:56 +01:00
|
|
|
"""Adapter factory to fetch the default conversation from annotations.
|
|
|
|
Will create the conversation if it does not exist.
|
|
|
|
|
|
|
|
This adapter will fetch and store all comments on the canonical object,
|
|
|
|
so that comments will be shared across all translations.
|
|
|
|
"""
|
|
|
|
if ITranslatable.providedBy(content):
|
2010-06-25 19:16:15 +02:00
|
|
|
canonical = content.getCanonical()
|
|
|
|
if canonical is not None:
|
|
|
|
return conversationAdapterFactory(canonical)
|
2010-01-28 14:44:56 +01:00
|
|
|
return conversationAdapterFactory(content)
|
|
|
|
|
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
class ConversationReplies(object):
|
2009-05-13 17:20:02 +02:00
|
|
|
"""An IReplies adapter for conversations.
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
This makes it easy to work with top-level comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
implements(IReplies)
|
2012-01-14 07:13:39 +01:00
|
|
|
adapts(Conversation) # relies on implementation details
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
def __init__(self, context):
|
|
|
|
self.conversation = context
|
2009-05-24 16:19:06 +02:00
|
|
|
self.comment_id = 0l
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = None
|
|
|
|
return self.conversation.addComment(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
# Dict API
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self.children
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Get an item by its long key
|
|
|
|
"""
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
raise KeyError(key)
|
|
|
|
return self.conversation[key]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __delitem__(self, key):
|
|
|
|
"""Delete an item by its long key
|
|
|
|
"""
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
raise KeyError(key)
|
|
|
|
del self.conversation[key]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def get(self, key, default=None):
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
return default
|
|
|
|
return self.conversation.get(key)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def keys(self):
|
|
|
|
return self.children
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def items(self):
|
|
|
|
return [(k, self.conversation[k]) for k in self.children]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def values(self):
|
|
|
|
return [self.conversation[k] for k in self.children]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iterkeys(self):
|
|
|
|
return iter(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def itervalues(self):
|
|
|
|
for key in self.children:
|
|
|
|
yield self.conversation[key]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iteritems(self):
|
|
|
|
for key in self.children:
|
|
|
|
yield (key, self.conversation[key],)
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2009-05-24 16:19:06 +02:00
|
|
|
@property
|
|
|
|
def children(self):
|
|
|
|
# we need to look this up every time, because we may not have a
|
|
|
|
# dict yet when the adapter is first created
|
|
|
|
return self.conversation._children.get(self.comment_id, LLSet())
|
2009-06-11 12:14:44 +02:00
|
|
|
|
2012-01-14 07:13:39 +01:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
class CommentReplies(ConversationReplies):
|
|
|
|
"""An IReplies adapter for comments.
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
This makes it easy to work with replies to specific comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
implements(IReplies)
|
2009-05-18 16:23:46 +02:00
|
|
|
|
|
|
|
# depends on implementation details of conversation
|
|
|
|
# most likely, anyone writing a different type of Conversation will also
|
|
|
|
# have a different type of Comment
|
|
|
|
|
|
|
|
adapts(Comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
def __init__(self, context):
|
2009-05-18 16:16:48 +02:00
|
|
|
self.comment = context
|
2009-06-24 15:55:20 +02:00
|
|
|
self.conversation = aq_parent(self.comment)
|
2013-04-18 16:12:00 +02:00
|
|
|
conversation_has_no_children = not hasattr(
|
|
|
|
self.conversation,
|
|
|
|
'_children'
|
|
|
|
)
|
|
|
|
if self.conversation is None or conversation_has_no_children:
|
2010-01-27 18:11:55 +01:00
|
|
|
raise TypeError("This adapter doesn't know what to do with the "
|
|
|
|
"parent conversation")
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
self.comment_id = self.comment.comment_id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = self.comment_id
|
|
|
|
return self.conversation.addComment(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2010-12-16 00:52:56 +01:00
|
|
|
# Dict API is inherited, written in terms of self.conversation and
|
2010-08-28 18:08:36 +02:00
|
|
|
# self.children
|