2009-08-05 11:01:58 +02:00
|
|
|
from persistent import Persistent
|
|
|
|
|
2009-08-15 13:35:54 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
|
2009-08-13 12:01:40 +02:00
|
|
|
from z3c.form import validator
|
2009-08-05 11:01:58 +02:00
|
|
|
from z3c.form.field import Fields
|
|
|
|
|
|
|
|
from zope import interface, schema
|
|
|
|
|
|
|
|
from zope.annotation import factory
|
|
|
|
|
2009-08-11 11:45:34 +02:00
|
|
|
from zope.component import adapts, provideAdapter, queryUtility
|
2009-08-05 11:01:58 +02:00
|
|
|
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
|
|
|
|
|
|
|
|
from zope.interface import Interface, implements
|
|
|
|
|
2009-08-11 11:45:34 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
2009-08-05 11:01:58 +02:00
|
|
|
from plone.z3cform.fieldsets import extensible
|
|
|
|
from plone.z3cform.fieldsets.interfaces import IFormExtender
|
|
|
|
|
2009-08-10 18:38:41 +02:00
|
|
|
from plone.app.discussion.browser.comments import CommentForm
|
2009-08-11 11:45:34 +02:00
|
|
|
from plone.app.discussion.comment import Comment
|
2009-08-12 22:45:47 +02:00
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha
|
2009-08-05 11:01:58 +02:00
|
|
|
|
2009-08-15 12:21:26 +02:00
|
|
|
from plone.app.discussion.browser.validator import CaptchaValidator
|
|
|
|
|
2009-08-13 12:01:40 +02:00
|
|
|
HAS_CAPTCHA = False
|
2009-08-12 21:20:18 +02:00
|
|
|
try:
|
|
|
|
from plone.formwidget.captcha import CaptchaFieldWidget
|
2009-08-13 12:01:40 +02:00
|
|
|
HAS_CAPTCHA = True
|
2009-08-12 21:20:18 +02:00
|
|
|
except ImportError:
|
2009-08-13 20:53:27 +02:00
|
|
|
pass
|
2009-08-12 21:20:18 +02:00
|
|
|
|
2009-08-13 12:01:40 +02:00
|
|
|
HAS_RECAPTCHA = False
|
2009-08-12 21:20:18 +02:00
|
|
|
try:
|
|
|
|
from plone.formwidget.recaptcha import ReCaptchaFieldWidget
|
2009-08-13 12:01:40 +02:00
|
|
|
HAS_RECAPTCHA = True
|
2009-08-12 21:20:18 +02:00
|
|
|
except ImportError:
|
2009-08-13 20:53:27 +02:00
|
|
|
pass
|
2009-08-12 21:20:18 +02:00
|
|
|
|
2009-08-05 11:01:58 +02:00
|
|
|
class Captcha(Persistent):
|
|
|
|
interface.implements(ICaptcha)
|
|
|
|
adapts(Comment)
|
|
|
|
captcha = u""
|
|
|
|
|
|
|
|
Captcha = factory(Captcha)
|
|
|
|
|
2009-08-13 12:01:40 +02:00
|
|
|
if HAS_CAPTCHA or HAS_RECAPTCHA:
|
2009-08-15 12:21:26 +02:00
|
|
|
# Extend the comment form with captcha, if a captcha solution is installed.
|
2009-08-13 12:01:40 +02:00
|
|
|
class CaptchaExtender(extensible.FormExtender):
|
|
|
|
adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form
|
|
|
|
|
|
|
|
fields = Fields(ICaptcha)
|
|
|
|
|
|
|
|
def __init__(self, context, request, form):
|
|
|
|
self.context = context
|
|
|
|
self.request = request
|
|
|
|
self.form = form
|
|
|
|
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
self.captcha = settings.captcha
|
2009-08-15 13:35:54 +02:00
|
|
|
portal_membership = getToolByName(self.context, 'portal_membership')
|
|
|
|
self.isAnon = portal_membership.isAnonymousUser()
|
2009-08-13 12:01:40 +02:00
|
|
|
|
|
|
|
def update(self):
|
2009-08-15 13:35:54 +02:00
|
|
|
if self.captcha != 'disabled' and self.isAnon:
|
2009-08-13 12:01:40 +02:00
|
|
|
# Add captcha field if captcha is enabled in the registry
|
|
|
|
self.add(ICaptcha, prefix="")
|
|
|
|
if HAS_CAPTCHA and self.captcha == 'captcha':
|
|
|
|
# If Captcha is installed and Captcha is enabled,
|
|
|
|
# use the CaptchaFieldWidget
|
|
|
|
self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget
|
|
|
|
elif HAS_RECAPTCHA and self.captcha == 'recaptcha':
|
|
|
|
# If ReCaptcha is installed and ReCaptcha is enabled,
|
|
|
|
# use the ReCaptchaFieldWidget
|
|
|
|
self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget
|
|
|
|
|
2009-08-15 12:21:26 +02:00
|
|
|
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
2009-08-13 12:01:40 +02:00
|
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha['captcha'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
# This is necessary, otherwise the zcml registration of the CaptchaExtender
|
|
|
|
# would fail if no captcha solution is installed.
|
|
|
|
class CaptchaExtender(extensible.FormExtender):
|
|
|
|
adapts(Interface, IDefaultBrowserLayer, CommentForm)
|
|
|
|
|
|
|
|
def __init__(self, context, request, form):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
pass
|