Simplify the CaptchaValidator class by dynamically adapting a view with the name of the captcha plugin (e.g. recaptcha, captcha, akismet) for the validator.
svn path=/plone.app.discussion/trunk/; revision=37131
This commit is contained in:
parent
30ffc4a920
commit
92766752c4
@ -4,6 +4,11 @@ Changelog
|
|||||||
1.0b5 (unreleased)
|
1.0b5 (unreleased)
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
* Simplify the CaptchaValidator class by dynamically adapting a view with the
|
||||||
|
name of the captcha plugin (e.g. recaptcha, captcha, akismet) for the
|
||||||
|
validator.
|
||||||
|
[timo]
|
||||||
|
|
||||||
* Dutch translation added.
|
* Dutch translation added.
|
||||||
[kcleong]
|
[kcleong]
|
||||||
|
|
||||||
|
@ -119,7 +119,11 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
|
|||||||
if 'captcha' in data:
|
if 'captcha' in data:
|
||||||
# Check Captcha only if there is a value, otherwise
|
# Check Captcha only if there is a value, otherwise
|
||||||
# the default "required" validator is sufficient.
|
# the default "required" validator is sufficient.
|
||||||
captcha = CaptchaValidator(self.context, self.request, None, ICaptcha['captcha'], None)
|
captcha = CaptchaValidator(self.context,
|
||||||
|
self.request,
|
||||||
|
None,
|
||||||
|
ICaptcha['captcha'],
|
||||||
|
None)
|
||||||
captcha.validate(data['captcha'])
|
captcha.validate(data['captcha'])
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
@ -40,34 +40,27 @@ from zope.component import adapts
|
|||||||
|
|
||||||
class CaptchaValidator(validator.SimpleFieldValidator):
|
class CaptchaValidator(validator.SimpleFieldValidator):
|
||||||
implements(IValidator)
|
implements(IValidator)
|
||||||
adapts(Interface,IDiscussionLayer,Interface,IField,Interface)
|
adapts(Interface, IDiscussionLayer, Interface, IField, Interface)
|
||||||
# Object, Request, Form, Field, Widget,
|
# Object, Request, Form, Field, Widget,
|
||||||
|
# We adapt the CaptchaValidator class to all form fields (IField)
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
super(CaptchaValidator, self).validate(value)
|
super(CaptchaValidator, self).validate(value)
|
||||||
|
|
||||||
|
data = self.request.form
|
||||||
|
|
||||||
registry = queryUtility(IRegistry)
|
registry = queryUtility(IRegistry)
|
||||||
settings = registry.forInterface(IDiscussionSettings)
|
settings = registry.forInterface(IDiscussionSettings)
|
||||||
|
|
||||||
if settings.captcha == 'captcha':
|
if settings.captcha != 'disabled':
|
||||||
# Fetch captcha view
|
|
||||||
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
||||||
name='captcha')
|
name=settings.captcha)
|
||||||
if value:
|
if not captcha.verify(input=value):
|
||||||
if not captcha.verify(value):
|
|
||||||
raise WrongCaptchaCode
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
raise WrongCaptchaCode
|
|
||||||
elif settings.captcha == 'recaptcha':
|
|
||||||
# Fetch recaptcha view
|
|
||||||
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
|
||||||
name='recaptcha')
|
|
||||||
if not captcha.verify():
|
|
||||||
raise WrongCaptchaCode
|
raise WrongCaptchaCode
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
||||||
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
||||||
field=ICaptcha['captcha'])
|
field=ICaptcha['captcha'])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user