CaptchaValidator for captcha/recaptcha/no captcha added. This is necessary because the zcml registration of the CaptchaValidator has to work with all three options.

svn path=/plone.app.discussion/trunk/; revision=28894
This commit is contained in:
Timo Stollenwerk 2009-08-15 10:21:26 +00:00
parent ead8813188
commit 2567483281
3 changed files with 54 additions and 5 deletions

View File

@ -21,10 +21,11 @@ from plone.app.discussion.browser.comments import CommentForm
from plone.app.discussion.comment import Comment
from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha
from plone.app.discussion.browser.validator import CaptchaValidator
HAS_CAPTCHA = False
try:
from plone.formwidget.captcha import CaptchaFieldWidget
from plone.formwidget.captcha.validator import CaptchaValidator
HAS_CAPTCHA = True
except ImportError:
pass
@ -44,8 +45,7 @@ class Captcha(Persistent):
Captcha = factory(Captcha)
if HAS_CAPTCHA or HAS_RECAPTCHA:
# Extend the comment form with captcha, only if a captcha solution is installed.
# Extend the comment form with captcha, if a captcha solution is installed.
class CaptchaExtender(extensible.FormExtender):
adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form
@ -73,7 +73,7 @@ if HAS_CAPTCHA or HAS_RECAPTCHA:
# use the ReCaptchaFieldWidget
self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget
# Register Captcha validator for the captcha field in the ICaptchaForm
# Register Captcha validator for the Captcha field in the ICaptcha Form
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha['captcha'])
else:

View File

@ -34,7 +34,7 @@ from plone.app.layout.viewlets.common import ViewletBase
from plone.app.discussion.comment import Comment, CommentFactory
from plone.app.discussion.interfaces import IConversation, IComment, IReplies, IDiscussionSettings, ICaptcha
from plone.formwidget.captcha.validator import CaptchaValidator
from plone.app.discussion.browser.validator import CaptchaValidator
from plone.z3cform import layout, z2
from plone.z3cform.fieldsets import extensible

View File

@ -0,0 +1,49 @@
# Captcha/ReCaptcha validators. We override the standard validators from
# plone.formwidget.captcha and plone.formwidget.recaptcha, in order to
# switch between the two. This is necessary, because the zcml registration
# of the CaptchaValidator has to be there, no matter which captcha solution
# is installed, or even when no captcha solution is installed.
from Acquisition import aq_inner
from z3c.form import validator
from z3c.form.interfaces import IValidator
from zope.component import getMultiAdapter, provideAdapter, queryUtility
from zope.schema import ValidationError
from plone.registry.interfaces import IRegistry
from plone.app.discussion.interfaces import IDiscussionSettings
from plone.formwidget.captcha import CaptchaMessageFactory as _
class WrongCaptchaCode(ValidationError):
__doc__ = _("""The code you entered was wrong, please enter the new one.""")
class CaptchaValidator(validator.SimpleFieldValidator):
def validate(self, value):
super(CaptchaValidator, self).validate(value)
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
if settings.captcha == 'captcha':
# Fetch captcha view
captcha = getMultiAdapter((aq_inner(self.context), self.request), name='captcha')
if value:
if not captcha.verify(value):
raise WrongCaptchaCode
else:
return True
raise WrongCaptchaCode
elif settings.captcha == 'recaptcha':
# Fetch recatpcha view
captcha = getMultiAdapter((aq_inner(self.context), self.request), name='recaptcha')
if not captcha.verify():
raise WrongCaptchaCode
else:
return True