Back to index

plone3  3.1.7
comments.py
Go to the documentation of this file.
00001 from urllib import quote as url_quote
00002 
00003 from Acquisition import aq_inner, aq_parent
00004 from AccessControl import getSecurityManager
00005 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00006 from Products.CMFCore.utils import getToolByName
00007 from Products.CMFDefault.DiscussionTool import DiscussionNotAllowed
00008 
00009 from plone.app.layout.viewlets.common import ViewletBase
00010 
00011 
00012 class CommentsViewlet(ViewletBase):
00013     index = ViewPageTemplateFile('comments.pt')
00014 
00015     def update(self):
00016         super(CommentsViewlet, self).update()
00017         self.portal_discussion = getToolByName(self.context, 'portal_discussion', None)
00018         self.portal_membership = getToolByName(self.context, 'portal_membership', None)
00019 
00020     def can_reply(self):
00021         return getSecurityManager().checkPermission('Reply to item', aq_inner(self.context))
00022 
00023     def is_discussion_allowed(self):
00024         if self.portal_discussion is None:
00025             return False
00026         else:
00027             return self.portal_discussion.isDiscussionAllowedFor(aq_inner(self.context))
00028 
00029     def get_replies(self):
00030         replies = []
00031 
00032         context = aq_inner(self.context)
00033         container = aq_parent(context)
00034         pd = self.portal_discussion
00035 
00036         def getRs(obj, replies, counter):
00037             rs = pd.getDiscussionFor(obj).getReplies()
00038             if len(rs) > 0:
00039                 rs.sort(lambda x, y: cmp(x.modified(), y.modified()))
00040                 for r in rs:
00041                     replies.append({'depth':counter, 'object':r})
00042                     getRs(r, replies, counter=counter + 1)
00043 
00044         try:
00045             getRs(context, replies, 0)
00046         except DiscussionNotAllowed:
00047             # We tried to get discussions for an object that has not only
00048             # discussions turned off but also no discussion container.
00049             return []
00050         return replies
00051 
00052     def is_anonymous(self):
00053         return self.portal_state.anonymous()
00054 
00055     def login_action(self):
00056         return '%s/login_form?came_from=%s' % (self.site_url, url_quote(self.request.get('URL', '')),)
00057 
00058     def can_manage(self):
00059         return getSecurityManager().checkPermission('Manage portal', aq_inner(self.context))
00060 
00061     def member_info(self, creator):
00062         if self.portal_membership is None:
00063             return None
00064         else:
00065             return self.portal_membership.getMemberInfo(creator)
00066 
00067     def format_time(self, time):
00068         context = aq_inner(self.context)
00069         util = getToolByName(context, 'translation_service')
00070         return util.ulocalized_time(time, 1, context, domain='plonelocales')