2016-02-05 01:39:53 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-04-15 06:29:46 +02:00
|
|
|
from Acquisition import aq_base
|
2011-04-15 18:47:57 +02:00
|
|
|
from Acquisition import aq_chain
|
|
|
|
from Acquisition import aq_inner
|
2016-02-05 01:39:53 +01:00
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
|
|
|
from plone.registry.interfaces import IRegistry
|
2011-04-15 06:29:46 +02:00
|
|
|
from Products.CMFCore.interfaces import IFolderish
|
2015-05-03 08:16:39 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
2011-04-15 06:29:46 +02:00
|
|
|
from Products.CMFPlone.interfaces import INonStructuralFolder
|
2015-05-03 08:16:39 +02:00
|
|
|
from Products.CMFPlone.interfaces import IPloneSiteRoot
|
2016-06-10 02:14:45 +02:00
|
|
|
from Products.CMFPlone.utils import safe_hasattr
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.component import queryUtility
|
2011-04-15 06:29:46 +02:00
|
|
|
|
2016-02-05 01:39:53 +01:00
|
|
|
|
2012-06-30 10:59:49 +02:00
|
|
|
try:
|
|
|
|
from plone.dexterity.interfaces import IDexterityContent
|
|
|
|
DEXTERITY_INSTALLED = True
|
2016-02-05 01:39:53 +01:00
|
|
|
except ImportError:
|
2012-06-30 10:59:49 +02:00
|
|
|
DEXTERITY_INSTALLED = False
|
|
|
|
|
2011-04-15 06:29:46 +02:00
|
|
|
|
2016-06-10 03:40:07 +02:00
|
|
|
def traverse_parents(context):
|
|
|
|
# Run through the aq_chain of obj and check if discussion is
|
|
|
|
# enabled in a parent folder.
|
|
|
|
for obj in aq_chain(context):
|
|
|
|
if not IPloneSiteRoot.providedBy(obj):
|
|
|
|
obj_is_folderish = IFolderish.providedBy(obj)
|
|
|
|
obj_is_stuctural = not INonStructuralFolder.providedBy(obj)
|
|
|
|
if (obj_is_folderish and obj_is_stuctural):
|
|
|
|
flag = getattr(obj, 'allow_discussion', None)
|
|
|
|
if flag is not None:
|
|
|
|
return flag
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2011-04-15 06:29:46 +02:00
|
|
|
class ConversationView(object):
|
|
|
|
|
|
|
|
def enabled(self):
|
2012-06-30 10:59:49 +02:00
|
|
|
if DEXTERITY_INSTALLED and IDexterityContent.providedBy(self.context):
|
|
|
|
return self._enabled_for_dexterity_types()
|
|
|
|
else:
|
|
|
|
return self._enabled_for_archetypes()
|
|
|
|
|
|
|
|
def _enabled_for_archetypes(self):
|
2011-04-15 06:29:46 +02:00
|
|
|
""" Returns True if discussion is enabled for this conversation.
|
|
|
|
|
|
|
|
This method checks five different settings in order to figure out if
|
2016-06-10 03:40:07 +02:00
|
|
|
discussion is enabled on a specific content object:
|
2011-04-15 06:29:46 +02:00
|
|
|
|
|
|
|
1) Check if discussion is enabled globally in the plone.app.discussion
|
|
|
|
registry/control panel.
|
|
|
|
|
|
|
|
2) If the current content object is a folder, always return
|
|
|
|
False, since we don't allow comments on a folder. This
|
2016-06-10 03:40:07 +02:00
|
|
|
setting is used to allow / disallow comments for all content
|
2011-04-15 06:29:46 +02:00
|
|
|
objects inside a folder, not for the folder itself.
|
|
|
|
|
|
|
|
3) Check if the allow_discussion boolean flag on the content object is
|
|
|
|
set. If it is set to True or False, return the value. If it set to
|
|
|
|
None, try further.
|
|
|
|
|
|
|
|
4) Traverse to a folder with allow_discussion set to either True or
|
|
|
|
False. If allow_discussion is not set (None), traverse further until
|
|
|
|
we reach the PloneSiteRoot.
|
|
|
|
|
|
|
|
5) Check if discussion is allowed for the content type.
|
|
|
|
"""
|
|
|
|
context = aq_inner(self.context)
|
|
|
|
|
|
|
|
# Fetch discussion registry
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
|
|
|
|
|
|
|
# Check if discussion is allowed globally
|
|
|
|
if not settings.globally_enabled:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Always return False if object is a folder
|
2013-04-18 15:31:40 +02:00
|
|
|
context_is_folderish = IFolderish.providedBy(context)
|
2016-06-10 03:40:07 +02:00
|
|
|
if context_is_folderish:
|
|
|
|
if not INonStructuralFolder.providedBy(context):
|
|
|
|
return False
|
2011-04-15 06:29:46 +02:00
|
|
|
|
|
|
|
# If discussion is disabled for the object, bail out
|
|
|
|
obj_flag = getattr(aq_base(context), 'allow_discussion', None)
|
|
|
|
if obj_flag is False:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check if traversal returned a folder with discussion_allowed set
|
|
|
|
# to True or False.
|
|
|
|
folder_allow_discussion = traverse_parents(context)
|
|
|
|
|
2011-04-15 18:47:57 +02:00
|
|
|
if folder_allow_discussion:
|
2011-04-15 06:29:46 +02:00
|
|
|
if not getattr(self, 'allow_discussion', None):
|
|
|
|
return True
|
2011-04-15 18:56:17 +02:00
|
|
|
else:
|
2011-04-15 06:29:46 +02:00
|
|
|
if obj_flag:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Check if discussion is allowed on the content type
|
|
|
|
portal_types = getToolByName(self, 'portal_types')
|
|
|
|
document_fti = getattr(portal_types, context.portal_type)
|
|
|
|
if not document_fti.getProperty('allow_discussion'):
|
|
|
|
# If discussion is not allowed on the content type,
|
|
|
|
# check if 'allow discussion' is overridden on the content object.
|
|
|
|
if not obj_flag:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2012-06-30 10:59:49 +02:00
|
|
|
|
|
|
|
def _enabled_for_dexterity_types(self):
|
|
|
|
""" Returns True if discussion is enabled for this conversation.
|
|
|
|
|
|
|
|
This method checks five different settings in order to figure out if
|
|
|
|
discussion is enable on a specific content object:
|
|
|
|
|
|
|
|
1) Check if discussion is enabled globally in the plone.app.discussion
|
|
|
|
registry/control panel.
|
|
|
|
|
|
|
|
2) Check if the allow_discussion boolean flag on the content object is
|
|
|
|
set. If it is set to True or False, return the value. If it set to
|
|
|
|
None, try further.
|
|
|
|
|
|
|
|
3) Check if discussion is allowed for the content type.
|
|
|
|
"""
|
|
|
|
context = aq_inner(self.context)
|
|
|
|
|
|
|
|
# Fetch discussion registry
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
|
|
|
|
|
|
|
# Check if discussion is allowed globally
|
|
|
|
if not settings.globally_enabled:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check if discussion is allowed on the content object
|
2016-06-10 02:14:45 +02:00
|
|
|
if safe_hasattr(context, 'allow_discussion'):
|
2012-06-30 10:59:49 +02:00
|
|
|
if context.allow_discussion is not None:
|
|
|
|
return context.allow_discussion
|
|
|
|
|
|
|
|
# Check if discussion is allowed on the content type
|
|
|
|
portal_types = getToolByName(self, 'portal_types')
|
|
|
|
document_fti = getattr(portal_types, context.portal_type)
|
|
|
|
return document_fti.getProperty('allow_discussion')
|