2010-07-13 12:41:26 +02:00
# -*- coding: utf-8 -*-
2015-05-03 08:16:39 +02:00
from plone . app . discussion . interfaces import _
from plone . app . discussion . interfaces import IDiscussionSettings
from plone . app . discussion . upgrades import update_registry
2009-06-02 23:20:53 +02:00
from plone . app . registry . browser import controlpanel
2010-12-11 22:30:18 +01:00
from plone . registry . interfaces import IRecordModifiedEvent
2015-05-03 08:16:39 +02:00
from plone . registry . interfaces import IRegistry
2016-02-05 01:39:53 +01:00
from Products . CMFCore . utils import getToolByName
from Products . CMFPlone . interfaces . controlpanel import IMailSchema
from Products . Five . browser . pagetemplatefile import ViewPageTemplateFile
from Products . statusmessages . interfaces import IStatusMessage
2010-11-30 10:06:46 +01:00
from z3c . form import button
2009-11-19 22:40:08 +01:00
from z3c . form . browser . checkbox import SingleCheckBoxFieldWidget
2015-05-03 08:16:39 +02:00
from zope . component import getMultiAdapter
from zope . component import getUtility
from zope . component import queryUtility
from zope . component . hooks import getSite
2010-12-02 19:15:47 +01:00
2018-10-24 16:46:50 +02:00
# try/except was added because Configuration Changed Event was moved inside the
# controlpanel file in the PR #2495 on Products.CMFPlone
2018-09-28 16:51:52 +02:00
try :
from Products . CMFPlone . interfaces . controlpanel import IConfigurationChangedEvent # noqa: E501
except ImportError :
2018-10-24 16:46:50 +02:00
from Products . CMFPlone . interfaces import IConfigurationChangedEvent
2018-09-28 16:51:52 +02:00
2009-06-02 23:20:53 +02:00
class DiscussionSettingsEditForm ( controlpanel . RegistryEditForm ) :
2010-12-09 09:07:15 +01:00
""" Discussion settings form.
"""
2009-06-02 23:20:53 +02:00
schema = IDiscussionSettings
2016-02-05 01:39:53 +01:00
id = ' DiscussionSettingsEditForm '
label = _ ( u ' Discussion settings ' )
2013-04-18 15:38:17 +02:00
description = _ (
2016-02-05 01:39:53 +01:00
u ' help_discussion_settings_editform ' ,
default = u ' Some discussion related settings are not '
u ' located in the Discussion Control Panel. \n '
u ' To enable comments for a specific content type, '
u ' go to the Types Control Panel of this type and '
u ' choose " Allow comments " . \n '
u ' To enable the moderation workflow for comments, '
u ' go to the Types Control Panel, choose '
u ' " Comment " and set workflow to '
2018-06-18 17:04:41 +02:00
u ' " Comment Review Workflow " . ' ,
2013-04-18 15:38:17 +02:00
)
2009-06-02 23:20:53 +02:00
def updateFields ( self ) :
super ( DiscussionSettingsEditForm , self ) . updateFields ( )
2010-08-31 19:31:27 +02:00
self . fields [ ' globally_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2010-12-11 18:18:14 +01:00
self . fields [ ' moderation_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2013-09-17 14:03:46 +02:00
self . fields [ ' edit_comment_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2014-09-20 16:02:48 +02:00
self . fields [ ' delete_own_comment_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2010-08-31 19:31:27 +02:00
self . fields [ ' anonymous_comments ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
self . fields [ ' show_commenter_image ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
self . fields [ ' moderator_notification_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2010-10-30 17:02:05 +02:00
self . fields [ ' user_notification_enabled ' ] . widgetFactory = \
SingleCheckBoxFieldWidget
2009-06-02 23:20:53 +02:00
def updateWidgets ( self ) :
2013-09-17 14:03:46 +02:00
try :
super ( DiscussionSettingsEditForm , self ) . updateWidgets ( )
except KeyError :
# upgrade profile not visible in prefs_install_products_form
# provide auto-upgrade
update_registry ( self . context )
super ( DiscussionSettingsEditForm , self ) . updateWidgets ( )
2016-02-05 01:39:53 +01:00
self . widgets [ ' globally_enabled ' ] . label = _ ( u ' Enable Comments ' )
self . widgets [ ' anonymous_comments ' ] . label = _ ( u ' Anonymous Comments ' )
self . widgets [ ' show_commenter_image ' ] . label = _ ( u ' Commenter Image ' )
self . widgets [ ' moderator_notification_enabled ' ] . label = _ (
2018-06-18 17:04:41 +02:00
u ' Moderator Email Notification ' ,
2016-02-05 01:39:53 +01:00
)
self . widgets [ ' user_notification_enabled ' ] . label = _ (
2018-06-18 17:04:41 +02:00
u ' User Email Notification ' ,
2016-02-05 01:39:53 +01:00
)
2012-01-14 07:13:39 +01:00
2011-07-15 10:23:08 +02:00
@button.buttonAndHandler ( _ ( ' Save ' ) , name = None )
2010-11-30 10:06:46 +01:00
def handleSave ( self , action ) :
data , errors = self . extractData ( )
if errors :
self . status = self . formErrorsMessage
return
2012-01-14 07:13:39 +01:00
self . applyChanges ( data )
2016-02-05 01:39:53 +01:00
IStatusMessage ( self . request ) . addStatusMessage ( _ ( u ' Changes saved ' ) ,
' info ' )
self . context . REQUEST . RESPONSE . redirect ( ' @@discussion-controlpanel ' )
2010-11-30 10:06:46 +01:00
@button.buttonAndHandler ( _ ( ' Cancel ' ) , name = ' cancel ' )
def handleCancel ( self , action ) :
2016-02-05 01:39:53 +01:00
IStatusMessage ( self . request ) . addStatusMessage ( _ ( u ' Edit cancelled ' ) ,
' info ' )
self . request . response . redirect (
' {0} / {1} ' . format (
self . context . absolute_url ( ) ,
2018-06-18 17:04:41 +02:00
self . control_panel_view ,
) ,
2016-02-05 01:39:53 +01:00
)
2010-12-02 22:07:58 +01:00
2010-12-16 00:52:56 +01:00
2010-12-02 22:07:58 +01:00
class DiscussionSettingsControlPanel ( controlpanel . ControlPanelFormWrapper ) :
2010-12-09 09:07:15 +01:00
""" Discussion settings control panel.
"""
2010-12-02 22:07:58 +01:00
form = DiscussionSettingsEditForm
index = ViewPageTemplateFile ( ' controlpanel.pt ' )
2021-02-17 15:46:11 +01:00
def __call__ ( self ) :
self . mailhost_warning ( )
self . custom_comment_workflow_warning ( )
return super ( DiscussionSettingsControlPanel , self ) . __call__ ( )
2016-03-11 14:12:03 +01:00
@property
def site_url ( self ) :
""" Return the absolute URL to the current site, which is likely not
necessarily the portal root .
"""
return getSite ( ) . absolute_url ( )
2010-12-03 00:02:00 +01:00
def settings ( self ) :
""" Compose a string that contains all registry settings that are
needed for the discussion control panel .
2010-12-02 22:07:58 +01:00
"""
2010-12-02 19:15:47 +01:00
registry = queryUtility ( IRegistry )
settings = registry . forInterface ( IDiscussionSettings , check = False )
2016-02-05 01:39:53 +01:00
wftool = getToolByName ( self . context , ' portal_workflow ' , None )
2012-03-15 13:06:00 +01:00
workflow_chain = wftool . getChainForPortalType ( ' Discussion Item ' )
2010-12-03 00:02:00 +01:00
output = [ ]
2010-12-16 00:52:56 +01:00
2010-12-03 00:20:10 +01:00
# Globally enabled
2010-12-03 00:02:00 +01:00
if settings . globally_enabled :
2016-02-05 01:39:53 +01:00
output . append ( ' globally_enabled ' )
2010-12-16 00:52:56 +01:00
2010-12-11 18:18:14 +01:00
# Comment moderation
2017-01-24 11:58:39 +01:00
one_state_worklow_disabled = \
' comment_one_state_workflow ' not in workflow_chain
2013-04-18 15:38:17 +02:00
comment_review_workflow_disabled = \
' comment_review_workflow ' not in workflow_chain
if one_state_worklow_disabled and comment_review_workflow_disabled :
2016-02-05 01:39:53 +01:00
output . append ( ' moderation_custom ' )
2010-12-11 22:30:18 +01:00
elif settings . moderation_enabled :
2016-02-05 01:39:53 +01:00
output . append ( ' moderation_enabled ' )
2010-12-16 00:52:56 +01:00
2013-09-17 14:03:46 +02:00
if settings . edit_comment_enabled :
2016-02-05 01:39:53 +01:00
output . append ( ' edit_comment_enabled ' )
2013-09-17 14:03:46 +02:00
2014-09-20 16:02:48 +02:00
if settings . delete_own_comment_enabled :
2016-02-05 01:39:53 +01:00
output . append ( ' delete_own_comment_enabled ' )
2014-09-20 16:02:48 +02:00
2010-12-03 00:20:10 +01:00
# Anonymous comments
2010-12-03 00:02:00 +01:00
if settings . anonymous_comments :
2016-02-05 01:39:53 +01:00
output . append ( ' anonymous_comments ' )
2010-12-16 00:52:56 +01:00
2010-12-03 00:20:10 +01:00
# Invalid mail setting
2010-12-02 22:07:58 +01:00
ctrlOverview = getMultiAdapter ( ( self . context , self . request ) ,
name = ' overview-controlpanel ' )
2010-12-03 00:02:00 +01:00
if ctrlOverview . mailhost_warning ( ) :
2016-02-05 01:39:53 +01:00
output . append ( ' invalid_mail_setup ' )
2010-12-03 00:02:00 +01:00
2010-12-03 00:20:10 +01:00
# Workflow
2012-03-15 13:06:00 +01:00
if workflow_chain :
discussion_workflow = workflow_chain [ 0 ]
2010-12-03 00:20:10 +01:00
output . append ( discussion_workflow )
2010-12-16 00:52:56 +01:00
2010-12-03 00:20:10 +01:00
# Merge all settings into one string
2010-12-03 00:02:00 +01:00
return ' ' . join ( output )
2010-12-09 09:07:15 +01:00
def mailhost_warning ( self ) :
""" Returns true if mailhost is not configured properly.
"""
2014-12-13 16:20:08 +01:00
# Copied from Products.CMFPlone/controlpanel/browser/overview.py
registry = getUtility ( IRegistry )
mail_settings = registry . forInterface ( IMailSchema , prefix = ' plone ' )
mailhost = mail_settings . smtp_host
email = mail_settings . email_from_address
2010-12-09 09:07:15 +01:00
if mailhost and email :
2021-02-17 15:46:11 +01:00
pass
else :
2021-02-17 16:02:26 +01:00
message = _ ( u ' discussion_text_no_mailhost_configured ' ,
default = u ' You have not configured a mail host or a site \' From \' address, various features including contact forms, email notification and password reset will not work. Go to the E-Mail Settings to fix this. ' ) # noqa: E501
2021-02-17 15:46:11 +01:00
IStatusMessage ( self . request ) . addStatusMessage ( message , ' warning ' )
2010-12-11 18:18:14 +01:00
2010-12-11 22:30:18 +01:00
def custom_comment_workflow_warning ( self ) :
2019-12-05 21:55:23 +01:00
""" Return True if a custom comment workflow is enabled. """
2016-02-05 01:39:53 +01:00
wftool = getToolByName ( self . context , ' portal_workflow ' , None )
2012-03-15 13:06:00 +01:00
workflow_chain = wftool . getChainForPortalType ( ' Discussion Item ' )
2017-01-24 11:58:39 +01:00
one_state_workflow_enabled = \
' comment_one_state_workflow ' in workflow_chain
2013-04-18 15:38:17 +02:00
comment_review_workflow_enabled = \
' comment_review_workflow ' in workflow_chain
2021-02-17 15:46:11 +01:00
if one_state_workflow_enabled or comment_review_workflow_enabled :
pass
else :
2021-02-17 16:02:26 +01:00
message = _ ( u ' discussion_text_custom_comment_workflow ' ,
default = u ' You have configured a custom workflow for the \' Discussion Item \' content type. You can enable/disable the comment moderation in this control panel only if you use one of the default \' Discussion Item \' workflows. Go to the Types control panel to choose a workflow for the \' Discussion Item \' type. ' ) # noqa: E501
2021-02-17 15:46:11 +01:00
IStatusMessage ( self . request ) . addStatusMessage ( message , ' warning ' )
2010-12-16 00:52:56 +01:00
2010-12-11 18:18:14 +01:00
def notify_configuration_changed ( event ) :
""" Event subscriber that is called every time the configuration changed.
"""
portal = getSite ( )
wftool = getToolByName ( portal , ' portal_workflow ' , None )
2010-12-16 00:52:56 +01:00
2010-12-11 18:18:14 +01:00
if IRecordModifiedEvent . providedBy ( event ) :
# Discussion control panel setting changed
if event . record . fieldName == ' moderation_enabled ' :
# Moderation enabled has changed
2013-04-18 15:38:17 +02:00
if event . record . value is True :
2010-12-11 18:18:14 +01:00
# Enable moderation workflow
2010-12-16 00:52:56 +01:00
wftool . setChainForPortalTypes ( ( ' Discussion Item ' , ) ,
' comment_review_workflow ' )
2010-12-11 18:18:14 +01:00
else :
# Disable moderation workflow
2010-12-16 00:52:56 +01:00
wftool . setChainForPortalTypes ( ( ' Discussion Item ' , ) ,
2017-01-10 18:09:01 +01:00
' comment_one_state_workflow ' )
2010-12-16 00:52:56 +01:00
2010-12-11 18:18:14 +01:00
if IConfigurationChangedEvent . providedBy ( event ) :
# Types control panel setting changed
2010-12-11 22:30:18 +01:00
if ' workflow ' in event . data :
registry = queryUtility ( IRegistry )
settings = registry . forInterface ( IDiscussionSettings , check = False )
2012-03-15 13:06:00 +01:00
workflow_chain = wftool . getChainForPortalType ( ' Discussion Item ' )
if workflow_chain :
workflow = workflow_chain [ 0 ]
2017-01-10 18:09:01 +01:00
if workflow == ' comment_one_state_workflow ' :
2012-03-15 13:06:00 +01:00
settings . moderation_enabled = False
elif workflow == ' comment_review_workflow ' :
settings . moderation_enabled = True
else :
# Custom workflow
pass