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:
Timo Stollenwerk 2010-06-10 10:51:41 +00:00
parent 30ffc4a920
commit 92766752c4
3 changed files with 20 additions and 18 deletions

View File

@ -4,6 +4,11 @@ Changelog
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.
[kcleong]

View File

@ -119,7 +119,11 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
if 'captcha' in data:
# Check Captcha only if there is a value, otherwise
# 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'])
else:
return

View File

@ -42,28 +42,20 @@ class CaptchaValidator(validator.SimpleFieldValidator):
implements(IValidator)
adapts(Interface, IDiscussionLayer, Interface, IField, Interface)
# Object, Request, Form, Field, Widget,
# We adapt the CaptchaValidator class to all form fields (IField)
def validate(self, value):
super(CaptchaValidator, self).validate(value)
data = self.request.form
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
if settings.captcha == 'captcha':
# Fetch captcha view
if settings.captcha != 'disabled':
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 recaptcha view
captcha = getMultiAdapter((aq_inner(self.context), self.request),
name='recaptcha')
if not captcha.verify():
name=settings.captcha)
if not captcha.verify(input=value):
raise WrongCaptchaCode
else:
return True
@ -71,3 +63,4 @@ class CaptchaValidator(validator.SimpleFieldValidator):
# Register Captcha validator for the Captcha field in the ICaptcha Form
validator.WidgetValidatorDiscriminators(CaptchaValidator,
field=ICaptcha['captcha'])