Skip to content. | Skip to navigation

Traversing Zope from Document to Discussion

Below is a clouseau session where I walk from a document to its comments. Looks
like the reverse is not immediately simple, as the comments are not bidirectionally
linked. But there are other ways to solve that.

This should be for our blog/wiki/whatever site.

I've thrown some comments in. Also, one thing I notice from what's below is that
I don't get the comments back in order. There must be some other mechanism
used for threading them.

F.

>>> # some easy shortcuts in a session so you don't have to remember them
>>> catalog = portal.portal_catalog
>>> types = portal.portal_types
>>> workflow = portal.portal_workflow
>>> skins = portal.portal_skins
>>> fp = portal.restrictedTraverse('front-page')
### get a commented document object
>>> doc = portal.restrictedTraverse('docs/test-search.pdf')
### verify its title
>>> doc.title
u'pdf search test'
### This reflects the "allow comments" check box or site setting
>>> doc.isDiscussable()
True
### one way to get the discussion tool
>>> dtool = portal.portal_discussion
>>> dc = dtool.getDiscussionFor(doc)
>>> dc
<Products.CMFDefault.DiscussionItem.DiscussionItemContainer object at 0xb25a49ec>
### Each discussion item has an ID
>>> dc.objectIds()
['1233848085',
 '1233848126',
 '1233847068',
 '1233846367',
 '1233847292',
 '1233846617']
>>> dtool.getId
<bound method DiscussionTool.getId of <DiscussionTool at /testplone/portal_discussion>>
>>> dtool.getId()
'portal_discussion'
>>> dc.getId()
'talkback'
### Another way to get directly to the DiscussionItemContainer for your doc
>>> doc.talkback
<Products.CMFDefault.DiscussionItem.DiscussionItemContainer object at 0xb25a49ec>
### This method gets tuples with ids and discussion item objects.
>>> dc.objectItems()
[('1233848085', <DiscussionItem at 1233848085>),
 ('1233848126', <DiscussionItem at 1233848126>),
 ('1233847068', <DiscussionItem at 1233847068>),
 ('1233846367', <DiscussionItem at 1233846367>),
 ('1233847292', <DiscussionItem at 1233847292>),
 ('1233846617', <DiscussionItem at 1233846617>)]
### This way gets a list of items
>>> dc.objectValues()
[<DiscussionItem at 1233848085>,
 <DiscussionItem at 1233848126>,
 <DiscussionItem at 1233847068>,
 <DiscussionItem at 1233846367>,
 <DiscussionItem at 1233847292>,
 <DiscussionItem at 1233846617>]
### Pick a discussion object from the list
>>> di = dc.objectValues()[2]
>>> di
<DiscussionItem at 1233847068>
>>> di.id
'1233847068'
>>> di.title
'3'
>>> di.description
'3'
>>> di.text
'e'
### Note this one was not a reply to another one (no returned value)
>>> di.in_reply_to
### This one was a reply
>>> di = dc.objectValues()[1]
>>> di.title
'x'
>>> di.text
'who says?'
### The in_reply_to attribute lists its parent discussion item
>>> di.in_reply_to
'1233848085'

Document Actions

The discussion item container has a "getReplies()" method

The discussion item container has a "getReplies()" method which
returns all the top-level comments in order. Then, each one can
be checked for replies to the reply, etc.

You can move upward from comment to root document

Each comment has an inReplyTo() method. For top-level comments, this returns the root document. For replies to replies (to replies, etc.), this method returns the parent reply.