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)
|
||||
------------------
|
||||
|
||||
* 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]
|
||||
|
||||
|
@ -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
|
||||
|
@ -40,34 +40,27 @@ from zope.component import adapts
|
||||
|
||||
class CaptchaValidator(validator.SimpleFieldValidator):
|
||||
implements(IValidator)
|
||||
adapts(Interface,IDiscussionLayer,Interface,IField,Interface)
|
||||
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
|
||||
|
||||
|
||||
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
||||
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
||||
field=ICaptcha['captcha'])
|
||||
field=ICaptcha['captcha'])
|
||||
|
Loading…
Reference in New Issue
Block a user