From 92766752c4f85ff5da6d0059f73f1d2c577cfc29 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Thu, 10 Jun 2010 10:51:41 +0000 Subject: [PATCH] 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 --- CHANGES.txt | 5 +++++ plone/app/discussion/browser/comments.py | 6 ++++- plone/app/discussion/browser/validator.py | 27 +++++++++-------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d18b06d..9ddd4e5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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] diff --git a/plone/app/discussion/browser/comments.py b/plone/app/discussion/browser/comments.py index 592008f..39ecff2 100644 --- a/plone/app/discussion/browser/comments.py +++ b/plone/app/discussion/browser/comments.py @@ -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 diff --git a/plone/app/discussion/browser/validator.py b/plone/app/discussion/browser/validator.py index ca0a722..cc2a6aa 100644 --- a/plone/app/discussion/browser/validator.py +++ b/plone/app/discussion/browser/validator.py @@ -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']) \ No newline at end of file + field=ICaptcha['captcha']) + \ No newline at end of file