allow_discussion on folder level implemented.

svn path=/plone.app.discussion/trunk/; revision=27517
This commit is contained in:
Timo Stollenwerk 2009-06-19 15:51:50 +00:00
parent d25e2b80c4
commit 23d4b25218
2 changed files with 55 additions and 26 deletions

View File

@ -106,26 +106,42 @@ class Conversation(Traversable, Persistent, Explicit):
if IFolderish.providedBy(aq_inner(self.__parent__)) and not INonStructuralFolder.providedBy(aq_inner(self.__parent__)): if IFolderish.providedBy(aq_inner(self.__parent__)) and not INonStructuralFolder.providedBy(aq_inner(self.__parent__)):
return False return False
# Check folders by traversing through the object tree # Traverse the object tree.
allowed = False def traverse_parent_folder(obj):
def traverse(obj): # Run through the object tree from context,
# Run from context to the Plone Site Root # to the Plone Site Root and look for the first folder object
# that has allow_discussion set to True or False.
if not IPloneSiteRoot.providedBy(obj): if not IPloneSiteRoot.providedBy(obj):
# Look for Plone folders if IFolderish.providedBy(obj) and \
if IFolderish.providedBy(obj) and not INonStructuralFolder.providedBy(obj): not INonStructuralFolder.providedBy(obj):
if portal_discussion.isDiscussionAllowedFor(obj): # If the current object is a Plone folder,
#print "FOLDER %s: discussion allowed => override" % obj.id # check if the discussion_allowed flag is set.
allowed = True allow_discussion_flag = getattr(obj, 'allow_discussion', None)
traverse(aq_parent(obj)) if allow_discussion_flag == True:
return True
elif allow_discussion_flag == False:
return False
else:
# If allow_discussion flag is not set, go on traversing.
pass
else:
# If the current object is not a Plone folder,
# go on with traversal.
foo = traverse_parent_folder(aq_parent(obj))
if foo:
return True
obj = aq_parent(self) obj = aq_parent(self)
#print ""
#print "### START TRAVERSE ###"
# Start traversing
#traverse(obj)
#print allowed
#print "### END TRAVERSE ###"
# Check if traversal returned a folder with discussion_allowed set
# to True or False.
folder_allow_discussion = traverse_parent_folder(obj)
if folder_allow_discussion == True:
if not getattr(self, 'allow_discussion', None):
return True
elif folder_allow_discussion == False:
if getattr(aq_inner(self.__parent__), 'allow_discussion', None):
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_type = self.__parent__.portal_type
@ -133,8 +149,7 @@ class Conversation(Traversable, Persistent, Explicit):
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 hasattr( aq_base(self.__parent__), 'allow_discussion' ): if not getattr(aq_inner(self.__parent__), 'allow_discussion', None):
if not portal_discussion.isDiscussionAllowedFor(aq_inner(self.__parent__)):
return False return False
return True return True

View File

@ -152,6 +152,10 @@ class ConversationTest(PloneTestCase):
# allow discussion attribute. Maybe we should remove this at # allow discussion attribute. Maybe we should remove this at
# some point. # some point.
# 1) allow_discussion attribute: Every content object in Plone
# has a allow_discussion attribute. By default it is set to None.
# Create a conversation. # Create a conversation.
conversation = IConversation(self.portal.doc1) conversation = IConversation(self.portal.doc1)
@ -167,8 +171,11 @@ class ConversationTest(PloneTestCase):
# By default, allow_discussion on newly created content objects is # By default, allow_discussion on newly created content objects is
# set to False # set to False
portal_discussion = getToolByName(self.portal, 'portal_discussion') portal_discussion = getToolByName(self.portal, 'portal_discussion')
self.failIf(portal_discussion.isDiscussionAllowedFor(self.portal.doc1)) self.assertEquals(portal_discussion.isDiscussionAllowedFor(self.portal.doc1), False)
self.failIf(self.portal.doc1.getTypeInfo().allowDiscussion()) self.assertEquals(self.portal.doc1.getTypeInfo().allowDiscussion(), False)
# The allow discussion flag is None by default
self.failIf(getattr(self.portal.doc1, 'allow_discussion', None))
# But isDiscussionAllowedFor, also checks if discussion is allowed on the # But isDiscussionAllowedFor, also checks if discussion is allowed on the
# content type. So we allow discussion on the Document content type and # content type. So we allow discussion on the Document content type and
@ -182,16 +189,19 @@ class ConversationTest(PloneTestCase):
self.portal_discussion.overrideDiscussionFor(self.portal.doc1, False) self.portal_discussion.overrideDiscussionFor(self.portal.doc1, False)
# Check if the Document discussion is disabled # Check if the Document discussion is disabled
self.assertEquals(portal_discussion.isDiscussionAllowedFor(self.portal.doc1), False) self.assertEquals(portal_discussion.isDiscussionAllowedFor(self.portal.doc1), False)
# Check that the local allow_discussion flag is now explicitly set to False
self.assertEquals(getattr(self.portal.doc1, 'allow_discussion', None), False)
# Disallow discussion on the Document content type again # Disallow discussion on the Document content type again
document_fti.manage_changeProperties(allow_discussion = False) document_fti.manage_changeProperties(allow_discussion = False)
self.failIf(portal_discussion.isDiscussionAllowedFor(self.portal.doc1)) self.assertEquals(portal_discussion.isDiscussionAllowedFor(self.portal.doc1), False)
self.failIf(self.portal.doc1.getTypeInfo().allowDiscussion()) self.assertEquals(self.portal.doc1.getTypeInfo().allowDiscussion(), False)
# Now we override allow_discussion again (True) for the Document # Now we override allow_discussion again (True) for the Document
# content object # content object
self.portal_discussion.overrideDiscussionFor(self.portal.doc1, True) self.portal_discussion.overrideDiscussionFor(self.portal.doc1, True)
self.failUnless(portal_discussion.isDiscussionAllowedFor(self.portal.doc1)) self.assertEquals(portal_discussion.isDiscussionAllowedFor(self.portal.doc1), True)
self.assertEquals(getattr(self.portal.doc1, 'allow_discussion', None), True)
def test_disable_commenting_globally(self): def test_disable_commenting_globally(self):
@ -277,20 +287,24 @@ class ConversationTest(PloneTestCase):
self.typetool.constructContent('Folder', self.portal, 'f1') self.typetool.constructContent('Folder', self.portal, 'f1')
f1 = self.portal.f1 f1 = self.portal.f1
# Create a document inside the folder with a conversation # Create a document inside the folder
self.typetool.constructContent('Document', f1, 'doc1') self.typetool.constructContent('Document', f1, 'doc1')
doc1 = self.portal.f1.doc1 doc1 = self.portal.f1.doc1
conversation = IConversation(self.portal.doc1) doc1_conversation = IConversation(doc1)
self.assertEquals(doc1_conversation.enabled(), False)
# Allow commenting for the folder # Allow commenting for the folder
self.portal_discussion.overrideDiscussionFor(f1, True) self.portal_discussion.overrideDiscussionFor(f1, True)
# Check if the content objects allows discussion # Check if the content objects allows discussion
#self.assertEquals(conversation.enabled(), True) self.assertEquals(doc1_conversation.enabled(), True)
# Turn commenting for the folder off # Turn commenting for the folder off
self.portal_discussion.overrideDiscussionFor(f1, False)
# Check if content objects do not allow discussion anymore # Check if content objects do not allow discussion anymore
self.assertEquals(doc1_conversation.enabled(), False)
def test_is_discussion_allowed_on_content_object(self): def test_is_discussion_allowed_on_content_object(self):
# Allow discussion on a single content object # Allow discussion on a single content object