2010-06-04 12:57:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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):
|
2010-06-01 18:38:29 +02:00
|
|
|
"""Captcha input field.
|
|
|
|
"""
|
2017-07-28 17:58:35 +02:00
|
|
|
captcha = u''
|
|
|
|
|
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
|
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
|
|
|
self.isAnon = portal_membership.isAnonymousUser()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
if self.captcha != 'disabled' and self.isAnon:
|
|
|
|
# Add a captcha field if captcha is enabled in the registry
|
2016-02-05 01:39:53 +01:00
|
|
|
self.add(ICaptcha, prefix='')
|
2010-06-01 18:38:29 +02:00
|
|
|
if self.captcha == 'captcha':
|
|
|
|
from plone.formwidget.captcha import CaptchaFieldWidget
|
|
|
|
self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget
|
|
|
|
elif self.captcha == 'recaptcha':
|
|
|
|
from plone.formwidget.recaptcha import ReCaptchaFieldWidget
|
2012-01-14 07:13:39 +01:00
|
|
|
self.form.fields['captcha'].widgetFactory = \
|
|
|
|
ReCaptchaFieldWidget
|
2010-09-10 15:02:56 +02:00
|
|
|
elif self.captcha == 'norobots':
|
|
|
|
from collective.z3cform.norobots import NorobotsFieldWidget
|
|
|
|
self.form.fields['captcha'].widgetFactory = NorobotsFieldWidget
|
2010-07-13 11:20:32 +02:00
|
|
|
else:
|
2010-06-10 15:11:33 +02:00
|
|
|
self.form.fields['captcha'].mode = interfaces.HIDDEN_MODE
|