plone.app.discussion/plone/app/discussion/browser/controlpanel.py

229 lines
9.5 KiB
Python
Raw Permalink Normal View History

from ..interfaces import _
from ..interfaces import IDiscussionSettings
from ..upgrades import update_registry
from plone.app.registry.browser import controlpanel
from plone.base.interfaces.controlpanel import IConfigurationChangedEvent
from plone.base.interfaces.controlpanel import IMailSchema
from plone.registry.interfaces import IRecordModifiedEvent
from plone.registry.interfaces import IRegistry
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.statusmessages.interfaces import IStatusMessage
from z3c.form import button
from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.component import queryUtility
from zope.component.hooks import getSite
2022-05-01 23:14:00 +02:00
class DiscussionSettingsEditForm(controlpanel.RegistryEditForm):
2022-05-01 23:14:09 +02:00
"""Discussion settings form."""
schema = IDiscussionSettings
2022-05-01 23:14:09 +02:00
id = "DiscussionSettingsEditForm"
label = _("Discussion settings")
2013-04-18 15:38:17 +02:00
description = _(
"help_discussion_settings_editform",
default="Some discussion related settings are not "
"located in the Discussion Control Panel.\n"
"To enable comments for a specific content type, "
"go to the Types Control Panel of this type and "
'choose "Allow comments".\n'
"To enable the moderation workflow for comments, "
"go to the Types Control Panel, choose "
'"Comment" and set workflow to '
'"Comment Review Workflow".',
2013-04-18 15:38:17 +02:00
)
def updateFields(self):
super().updateFields()
2022-05-01 23:14:09 +02:00
self.fields["globally_enabled"].widgetFactory = SingleCheckBoxFieldWidget
self.fields["moderation_enabled"].widgetFactory = SingleCheckBoxFieldWidget
self.fields["edit_comment_enabled"].widgetFactory = SingleCheckBoxFieldWidget
self.fields[
"delete_own_comment_enabled"
].widgetFactory = SingleCheckBoxFieldWidget
self.fields["anonymous_comments"].widgetFactory = SingleCheckBoxFieldWidget
self.fields["show_commenter_image"].widgetFactory = SingleCheckBoxFieldWidget
self.fields[
"moderator_notification_enabled"
].widgetFactory = SingleCheckBoxFieldWidget
self.fields[
"user_notification_enabled"
].widgetFactory = SingleCheckBoxFieldWidget
def updateWidgets(self):
2013-09-17 14:03:46 +02:00
try:
super().updateWidgets()
2013-09-17 14:03:46 +02:00
except KeyError:
# upgrade profile not visible in prefs_install_products_form
# provide auto-upgrade
update_registry(self.context)
super().updateWidgets()
self.widgets["globally_enabled"].label = _("Enable Comments")
self.widgets["anonymous_comments"].label = _("Anonymous Comments")
self.widgets["show_commenter_image"].label = _("Commenter Image")
2022-05-01 23:14:09 +02:00
self.widgets["moderator_notification_enabled"].label = _(
"Moderator Email Notification",
)
2022-05-01 23:14:09 +02:00
self.widgets["user_notification_enabled"].label = _(
"User Email Notification",
)
2012-01-14 07:13:39 +01:00
2022-05-01 23:14:09 +02:00
@button.buttonAndHandler(_("Save"), name=None)
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)
IStatusMessage(self.request).addStatusMessage(_("Changes saved"), "info")
2022-05-01 23:14:09 +02:00
self.context.REQUEST.RESPONSE.redirect("@@discussion-controlpanel")
2022-05-01 23:14:09 +02:00
@button.buttonAndHandler(_("Cancel"), name="cancel")
def handleCancel(self, action):
IStatusMessage(self.request).addStatusMessage(_("Edit cancelled"), "info")
self.request.response.redirect(
"{}/{}".format(
self.context.absolute_url(),
self.control_panel_view,
),
)
class DiscussionSettingsControlPanel(controlpanel.ControlPanelFormWrapper):
2022-05-01 23:14:09 +02:00
"""Discussion settings control panel."""
form = DiscussionSettingsEditForm
2022-05-01 23:14:09 +02:00
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().__call__()
2021-02-17 15:46:11 +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()
def settings(self):
"""Compose a string that contains all registry settings that are
2022-05-01 23:14:09 +02:00
needed for the discussion control panel.
"""
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
2022-05-01 23:14:09 +02:00
wftool = getToolByName(self.context, "portal_workflow", None)
workflow_chain = wftool.getChainForPortalType("Discussion Item")
output = []
# Globally enabled
if settings.globally_enabled:
2022-05-01 23:14:09 +02:00
output.append("globally_enabled")
# Comment moderation
2022-05-01 23:14:09 +02:00
one_state_worklow_disabled = "comment_one_state_workflow" not in workflow_chain
comment_review_workflow_disabled = (
"comment_review_workflow" not in workflow_chain
)
2013-04-18 15:38:17 +02:00
if one_state_worklow_disabled and comment_review_workflow_disabled:
2022-05-01 23:14:09 +02:00
output.append("moderation_custom")
elif settings.moderation_enabled:
2022-05-01 23:14:09 +02:00
output.append("moderation_enabled")
2013-09-17 14:03:46 +02:00
if settings.edit_comment_enabled:
2022-05-01 23:14:09 +02:00
output.append("edit_comment_enabled")
2013-09-17 14:03:46 +02:00
if settings.delete_own_comment_enabled:
2022-05-01 23:14:09 +02:00
output.append("delete_own_comment_enabled")
# Anonymous comments
if settings.anonymous_comments:
2022-05-01 23:14:09 +02:00
output.append("anonymous_comments")
# Invalid mail setting
2022-05-01 23:14:09 +02:00
ctrlOverview = getMultiAdapter(
(self.context, self.request), name="overview-controlpanel"
)
if ctrlOverview.mailhost_warning():
2022-05-01 23:14:09 +02:00
output.append("invalid_mail_setup")
# Workflow
if workflow_chain:
discussion_workflow = workflow_chain[0]
output.append(discussion_workflow)
# Merge all settings into one string
2022-05-01 23:14:09 +02:00
return " ".join(output)
def mailhost_warning(self):
2022-05-01 23:14:09 +02:00
"""Returns true if mailhost is not configured properly."""
# Copied from Products.CMFPlone/controlpanel/browser/overview.py
registry = getUtility(IRegistry)
2022-05-01 23:14:09 +02:00
mail_settings = registry.forInterface(IMailSchema, prefix="plone")
mailhost = mail_settings.smtp_host
email = mail_settings.email_from_address
if mailhost and email:
2021-02-17 15:46:11 +01:00
pass
else:
2022-05-01 23:14:09 +02:00
message = _(
"discussion_text_no_mailhost_configured",
default="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.",
2022-05-01 23:14:09 +02:00
) # noqa: E501
IStatusMessage(self.request).addStatusMessage(message, "warning")
def custom_comment_workflow_warning(self):
"""Return True if a custom comment workflow is enabled."""
2022-05-01 23:14:09 +02:00
wftool = getToolByName(self.context, "portal_workflow", None)
workflow_chain = wftool.getChainForPortalType("Discussion Item")
one_state_workflow_enabled = "comment_one_state_workflow" in workflow_chain
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:
2022-05-01 23:14:09 +02:00
message = _(
"discussion_text_custom_comment_workflow",
default="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.",
2022-05-01 23:14:09 +02:00
) # noqa: E501
IStatusMessage(self.request).addStatusMessage(message, "warning")
def notify_configuration_changed(event):
2022-05-01 23:14:09 +02:00
"""Event subscriber that is called every time the configuration changed."""
portal = getSite()
2022-05-01 23:14:09 +02:00
wftool = getToolByName(portal, "portal_workflow", None)
if IRecordModifiedEvent.providedBy(event):
# Discussion control panel setting changed
2022-05-01 23:14:09 +02:00
if event.record.fieldName == "moderation_enabled":
# Moderation enabled has changed
2013-04-18 15:38:17 +02:00
if event.record.value is True:
# Enable moderation workflow
2022-05-01 23:14:09 +02:00
wftool.setChainForPortalTypes(
("Discussion Item",), "comment_review_workflow"
)
else:
# Disable moderation workflow
2022-05-01 23:14:09 +02:00
wftool.setChainForPortalTypes(
("Discussion Item",), "comment_one_state_workflow"
)
if IConfigurationChangedEvent.providedBy(event):
# Types control panel setting changed
2022-05-01 23:14:09 +02:00
if "workflow" in event.data:
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings, check=False)
2022-05-01 23:14:09 +02:00
workflow_chain = wftool.getChainForPortalType("Discussion Item")
if workflow_chain:
workflow = workflow_chain[0]
2022-05-01 23:14:09 +02:00
if workflow == "comment_one_state_workflow":
settings.moderation_enabled = False
2022-05-01 23:14:09 +02:00
elif workflow == "comment_review_workflow":
settings.moderation_enabled = True
else:
# Custom workflow
pass