2010-06-04 12:57:57 +02:00
|
|
|
# Captcha validator, see captcha.txt for design notes.
|
2009-08-05 11:01:58 +02:00
|
|
|
from persistent import Persistent
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.discussion.browser.comments import CommentForm
|
|
|
|
from plone.app.discussion.comment import Comment
|
|
|
|
from plone.app.discussion.interfaces import ICaptcha
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
from plone.z3cform.fieldsets import extensible
|
2009-08-15 13:35:54 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
2010-07-13 11:20:32 +02:00
|
|
|
from z3c.form import interfaces
|
2009-08-05 11:01:58 +02:00
|
|
|
from z3c.form.field import Fields
|
2010-07-13 11:20:32 +02:00
|
|
|
from zope import interface
|
2009-08-05 11:01:58 +02:00
|
|
|
from zope.annotation import factory
|
2017-07-28 17:58:35 +02:00
|
|
|
from zope.component import adapter
|
2016-02-05 01:39:53 +01:00
|
|
|
from zope.component import queryUtility
|
2010-07-13 11:20:32 +02:00
|
|
|
from zope.interface import Interface
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
|
2009-08-05 11:01:58 +02:00
|
|
|
|
2009-08-12 21:20:18 +02:00
|
|
|
|
2017-07-28 17:58:35 +02:00
|
|
|
@adapter(Comment)
|
2016-07-05 23:12:08 +02:00
|
|
|
@interface.implementer(ICaptcha)
|
2009-08-05 11:01:58 +02:00
|
|
|
class Captcha(Persistent):
|
2022-05-01 23:14:09 +02:00
|
|
|
"""Captcha input field."""
|
|
|
|
|
2022-05-01 23:14:41 +02:00
|
|
|
captcha = ""
|
2017-07-28 17:58:35 +02:00
|
|
|
|
2009-08-05 11:01:58 +02:00
|
|
|
|
|
|
|
Captcha = factory(Captcha)
|
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
|
2017-07-28 17:58:35 +02:00
|
|
|
# context, request, form
|
|
|
|
@adapter(Interface, IDefaultBrowserLayer, CommentForm)
|
2010-06-01 18:38:29 +02:00
|
|
|
class CaptchaExtender(extensible.FormExtender):
|
|
|
|
"""Extends the comment form with a Captcha. This Captcha extender is only
|
2010-12-16 00:52:56 +01:00
|
|
|
registered when a plugin is installed that provides the
|
2010-06-01 18:38:29 +02:00
|
|
|
"plone.app.discussion-captcha" feature.
|
|
|
|
"""
|
|
|
|
|
|
|
|
fields = Fields(ICaptcha)
|
|
|
|
|
|
|
|
def __init__(self, context, request, form):
|
|
|
|
self.context = context
|
|
|
|
self.request = request
|
|
|
|
self.form = form
|
|
|
|
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-10-29 12:43:46 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings, check=False)
|
2010-06-01 18:38:29 +02:00
|
|
|
self.captcha = settings.captcha
|
2022-05-01 23:14:09 +02:00
|
|
|
portal_membership = getToolByName(self.context, "portal_membership")
|
2010-06-01 18:38:29 +02:00
|
|
|
self.isAnon = portal_membership.isAnonymousUser()
|
|
|
|
|
|
|
|
def update(self):
|
2022-05-01 23:14:09 +02:00
|
|
|
if self.captcha != "disabled" and self.isAnon:
|
2010-06-01 18:38:29 +02:00
|
|
|
# Add a captcha field if captcha is enabled in the registry
|
2022-05-01 23:14:09 +02:00
|
|
|
self.add(ICaptcha, prefix="")
|
|
|
|
if self.captcha == "captcha":
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.formwidget.captcha import CaptchaFieldWidget
|
2022-05-01 23:14:09 +02:00
|
|
|
|
|
|
|
self.form.fields["captcha"].widgetFactory = CaptchaFieldWidget
|
|
|
|
elif self.captcha == "recaptcha":
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.formwidget.recaptcha import ReCaptchaFieldWidget
|
2022-05-01 23:14:09 +02:00
|
|
|
|
|
|
|
self.form.fields["captcha"].widgetFactory = ReCaptchaFieldWidget
|
|
|
|
elif self.captcha == "norobots":
|
2010-09-10 15:02:56 +02:00
|
|
|
from collective.z3cform.norobots import NorobotsFieldWidget
|
2022-05-01 23:14:09 +02:00
|
|
|
|
|
|
|
self.form.fields["captcha"].widgetFactory = NorobotsFieldWidget
|
2010-07-13 11:20:32 +02:00
|
|
|
else:
|
2022-05-01 23:14:09 +02:00
|
|
|
self.form.fields["captcha"].mode = interfaces.HIDDEN_MODE
|