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:
parent
ead8813188
commit
2567483281
@ -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:
|
||||
|
@ -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
|
||||
|
49
plone/app/discussion/browser/validator.py
Normal file
49
plone/app/discussion/browser/validator.py
Normal 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
|
Loading…
Reference in New Issue
Block a user