Optimized the conversation enabled method and avoid overlong lines

svn path=/plone.app.discussion/trunk/; revision=33514
This commit is contained in:
Hanno Schlichting 2010-01-27 17:11:55 +00:00
parent 9f4129a65f
commit faf339bec5

View File

@ -16,8 +16,6 @@ from persistent import Persistent
from plone.registry.interfaces import IRegistry from plone.registry.interfaces import IRegistry
from zope.site.hooks import getSite
from zope.interface import implements, implementer from zope.interface import implements, implementer
from zope.component import adapts, adapter, queryUtility from zope.component import adapts, adapter, queryUtility
@ -59,7 +57,9 @@ except ImportError:
from BTrees.OOBTree import OOBTree as LOBTree from BTrees.OOBTree import OOBTree as LOBTree
from BTrees.OOBTree import OOSet as LLSet from BTrees.OOBTree import OOSet as LLSet
from plone.app.discussion.interfaces import IConversation, IReplies, IDiscussionSettings from plone.app.discussion.interfaces import IConversation
from plone.app.discussion.interfaces import IDiscussionSettings
from plone.app.discussion.interfaces import IReplies
from plone.app.discussion.comment import Comment from plone.app.discussion.comment import Comment
ANNOTATION_KEY = 'plone.app.discussion:conversation' ANNOTATION_KEY = 'plone.app.discussion:conversation'
@ -84,7 +84,8 @@ class Conversation(Traversable, Persistent, Explicit):
# id -> comment - find comment by id # id -> comment - find comment by id
self._comments = LOBTree() self._comments = LOBTree()
# id -> LLSet (children) - find all children for a given comment. 0 signifies root. # id -> LLSet (children) - find all children for a given comment.
# 0 signifies root.
self._children = LOBTree() self._children = LOBTree()
def getId(self): def getId(self):
@ -96,9 +97,6 @@ class Conversation(Traversable, Persistent, Explicit):
def enabled(self): def enabled(self):
# Returns True if discussion is enabled on the conversation # Returns True if discussion is enabled on the conversation
portal_discussion = getToolByName(self, 'portal_discussion')
portal_types = getToolByName(self, 'portal_types')
# Fetch discussion registry # Fetch discussion registry
registry = queryUtility(IRegistry) registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings) settings = registry.forInterface(IDiscussionSettings)
@ -107,8 +105,11 @@ class Conversation(Traversable, Persistent, Explicit):
if not settings.globally_enabled: if not settings.globally_enabled:
return False return False
parent = aq_inner(self.__parent__)
# Always return False if object is a folder # Always return False if object is a folder
if IFolderish.providedBy(aq_inner(self.__parent__)) and not INonStructuralFolder.providedBy(aq_inner(self.__parent__)): if (IFolderish.providedBy(parent) and
not INonStructuralFolder.providedBy(parent)):
return False return False
def traverse_parents(obj): def traverse_parents(obj):
@ -116,18 +117,18 @@ class Conversation(Traversable, Persistent, Explicit):
# enabled in a parent folder. # enabled in a parent folder.
for obj in self.aq_chain: for obj in self.aq_chain:
if not IPloneSiteRoot.providedBy(obj): if not IPloneSiteRoot.providedBy(obj):
if IFolderish.providedBy(obj) and \ if (IFolderish.providedBy(obj) and
not INonStructuralFolder.providedBy(obj): not INonStructuralFolder.providedBy(obj)):
allow_discussion_flag = getattr(obj,'allow_discussion',None) flag = getattr(obj, 'allow_discussion', None)
if allow_discussion_flag is not None: if flag is not None:
return allow_discussion_flag return flag
return None return None
obj = aq_parent(self) obj = aq_parent(self)
# If discussion is disabled for the object, bail out # If discussion is disabled for the object, bail out
allow_discussion_flag = getattr(obj, 'allow_discussion', None) obj_flag = getattr(obj, 'allow_discussion', None)
if allow_discussion_flag is False: if obj_flag is False:
return False return False
# Check if traversal returned a folder with discussion_allowed set # Check if traversal returned a folder with discussion_allowed set
@ -138,16 +139,16 @@ class Conversation(Traversable, Persistent, Explicit):
if not getattr(self, 'allow_discussion', None): if not getattr(self, 'allow_discussion', None):
return True return True
elif folder_allow_discussion is False: elif folder_allow_discussion is False:
if getattr(aq_inner(self.__parent__), 'allow_discussion', None): if obj_flag:
return True return True
# Check if discussion is allowed on the content type # Check if discussion is allowed on the content type
portal_type = self.__parent__.portal_type portal_types = getToolByName(self, 'portal_types')
document_fti = getattr(portal_types, obj.portal_type) document_fti = getattr(portal_types, obj.portal_type)
if not document_fti.getProperty('allow_discussion'): if not document_fti.getProperty('allow_discussion'):
# If discussion is not allowed on the content type, # If discussion is not allowed on the content type,
# check if 'allow discussion' is overridden on the content object. # check if 'allow discussion' is overridden on the content object.
if not getattr(aq_inner(self.__parent__), 'allow_discussion', None): if not obj_flag:
return False return False
return True return True
@ -449,8 +450,10 @@ class CommentReplies(ConversationReplies):
self.comment = context self.comment = context
self.conversation = aq_parent(self.comment) self.conversation = aq_parent(self.comment)
if self.conversation is None or not hasattr(self.conversation, '_children'): if (self.conversation is None or
raise TypeError("This adapter doesn't know what to do with the parent conversation") not hasattr(self.conversation, '_children')):
raise TypeError("This adapter doesn't know what to do with the "
"parent conversation")
self.comment_id = self.comment.comment_id self.comment_id = self.comment.comment_id