Back to index

plone3  3.1.7
DiscussionTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Basic portal discussion access tool.
00014 
00015 $Id: DiscussionTool.py 81923 2007-11-18 22:51:13Z jens $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Acquisition import aq_base
00020 from Globals import DTMLFile
00021 from Globals import InitializeClass
00022 from OFS.SimpleItem import SimpleItem
00023 from zope.interface import implements
00024 
00025 from Products.CMFCore.interfaces import IDiscussionResponse
00026 from Products.CMFCore.interfaces import IDiscussionTool
00027 from Products.CMFCore.interfaces.Discussions \
00028         import DiscussionResponse as z2IDiscussionResponse
00029 from Products.CMFCore.interfaces.portal_discussion \
00030         import portal_discussion as z2IDiscussionTool
00031 from Products.CMFCore.utils import _checkPermission
00032 from Products.CMFCore.utils import registerToolInterface
00033 from Products.CMFCore.utils import UniqueObject
00034 
00035 from DiscussionItem import DiscussionItemContainer
00036 from exceptions import AccessControl_Unauthorized
00037 from exceptions import DiscussionNotAllowed
00038 from permissions import ManagePortal
00039 from permissions import ModifyPortalContent
00040 from utils import _dtmldir
00041 
00042 _marker = []
00043 
00044 
00045 class DiscussionTool(UniqueObject, SimpleItem):
00046 
00047     """ Links content to discussions.
00048     """
00049 
00050     implements(IDiscussionTool)
00051     __implements__ = (z2IDiscussionTool, )
00052 
00053     id = 'portal_discussion'
00054     meta_type = 'Default Discussion Tool'
00055 
00056     security = ClassSecurityInfo()
00057 
00058     manage_options = ( ({'label': 'Overview',
00059                          'action': 'manage_overview'},)
00060                      + SimpleItem.manage_options
00061                      )
00062 
00063     #
00064     #   ZMI methods
00065     #
00066     security.declareProtected(ManagePortal, 'manage_overview')
00067     manage_overview = DTMLFile( 'explainDiscussionTool', _dtmldir )
00068 
00069     #
00070     #   'portal_discussion' interface methods
00071     #
00072 
00073     security.declarePublic( 'overrideDiscussionFor' )
00074     def overrideDiscussionFor(self, content, allowDiscussion):
00075         """ Override discussability for the given object or clear the setting.
00076         """
00077         if not _checkPermission(ModifyPortalContent, content):
00078             raise AccessControl_Unauthorized
00079 
00080         if allowDiscussion is None or allowDiscussion == 'None':
00081             disc_flag = getattr(aq_base(content), 'allow_discussion', _marker)
00082             if disc_flag is not _marker:
00083                 try:
00084                     del content.allow_discussion
00085                 except AttributeError:
00086                     # https://bugs.launchpad.net/zope-cmf/+bug/162532
00087                     pass
00088         else:
00089             content.allow_discussion = bool(allowDiscussion)
00090 
00091     security.declarePublic( 'getDiscussionFor' )
00092     def getDiscussionFor(self, content):
00093         """ Get DiscussionItemContainer for content, create it if necessary.
00094         """
00095         if not self.isDiscussionAllowedFor( content ):
00096             raise DiscussionNotAllowed
00097 
00098         if not IDiscussionResponse.providedBy(content) and \
00099                 not z2IDiscussionResponse.isImplementedBy(content) and \
00100                 getattr( aq_base(content), 'talkback', None ) is None:
00101             # Discussion Items use the DiscussionItemContainer object of the
00102             # related content item, so only create one for other content items
00103             self._createDiscussionFor(content)
00104 
00105         return content.talkback # Return wrapped talkback
00106 
00107     security.declarePublic( 'isDiscussionAllowedFor' )
00108     def isDiscussionAllowedFor( self, content ):
00109         """ Get boolean indicating whether discussion is allowed for content.
00110         """
00111         if hasattr( aq_base(content), 'allow_discussion' ):
00112             return bool(content.allow_discussion)
00113         typeInfo = content.getTypeInfo()
00114         if typeInfo:
00115             return bool( typeInfo.allowDiscussion() )
00116         return False
00117 
00118     #
00119     #   Utility methods
00120     #
00121     security.declarePrivate( '_createDiscussionFor' )
00122     def _createDiscussionFor( self, content ):
00123         """ Create DiscussionItemContainer for content, if allowed.
00124         """
00125         if not self.isDiscussionAllowedFor( content ):
00126             raise DiscussionNotAllowed
00127 
00128         content.talkback = DiscussionItemContainer()
00129         return content.talkback
00130 
00131 InitializeClass( DiscussionTool )
00132 registerToolInterface('portal_discussion', IDiscussionTool)