From e1b068dbbc680fedd4095045d8e1b4f9c1fe1ddc Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Wed, 12 Aug 2009 20:45:47 +0000 Subject: [PATCH] captcha validator added. svn path=/plone.app.discussion/trunk/; revision=28746 --- plone/app/discussion/browser/captcha.py | 18 ++++++++++-------- plone/app/discussion/browser/comments.py | 11 +++++++++++ plone/app/discussion/browser/configure.zcml | 8 +++++++- plone/app/discussion/interfaces.py | 4 ++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/plone/app/discussion/browser/captcha.py b/plone/app/discussion/browser/captcha.py index 05c20ee..3d3de3c 100644 --- a/plone/app/discussion/browser/captcha.py +++ b/plone/app/discussion/browser/captcha.py @@ -18,7 +18,7 @@ from plone.z3cform.fieldsets.interfaces import IFormExtender from plone.app.discussion.browser.comments import CommentForm from plone.app.discussion.comment import Comment -from plone.app.discussion.interfaces import IDiscussionSettings +from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha try: from plone.formwidget.captcha import CaptchaFieldWidget @@ -30,22 +30,19 @@ try: except ImportError: pass - -class ICaptcha(Interface): - captcha = schema.TextLine(title=u"Captcha", - required=True) - class Captcha(Persistent): interface.implements(ICaptcha) adapts(Comment) captcha = u"" Captcha = factory(Captcha) -provideAdapter(Captcha) class CaptchaExtender(extensible.FormExtender): adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form + fields = Fields(ICaptcha) + fields['captcha'].widgetFactory = CaptchaFieldWidget + def __init__(self, context, request, form): self.context = context self.request = request @@ -62,4 +59,9 @@ class CaptchaExtender(extensible.FormExtender): if self.captcha == 'captcha': self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget elif self.captcha == 'recaptcha': - self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget \ No newline at end of file + self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget + +from z3c.form import validator +from plone.formwidget.captcha.validator import CaptchaValidator +# Register Captcha validator for the captcha field in the ICaptchaForm +validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha['captcha']) \ No newline at end of file diff --git a/plone/app/discussion/browser/comments.py b/plone/app/discussion/browser/comments.py index d4e0d47..fedcee9 100644 --- a/plone/app/discussion/browser/comments.py +++ b/plone/app/discussion/browser/comments.py @@ -77,6 +77,17 @@ class CommentForm(extensible.ExtensibleForm, form.Form): def handleComment(self, action): data, errors = self.extractData() + if data.has_key('captcha'): + from plone.formwidget.captcha.validator import CaptchaValidator + from plone.app.discussion.interfaces import ICaptcha + # Verify the user input against the captcha + captcha = CaptchaValidator(self.context, self.request, None, ICaptcha['captcha'], None) + if data.has_key('subject') and captcha.validate(data['captcha']): + # if captcha validation passes, print the subject + print data['subject'] + else: + return + if data.has_key('title') and data.has_key('text'): title = data['title'] diff --git a/plone/app/discussion/browser/configure.zcml b/plone/app/discussion/browser/configure.zcml index 0d35e35..e83b4b7 100644 --- a/plone/app/discussion/browser/configure.zcml +++ b/plone/app/discussion/browser/configure.zcml @@ -66,12 +66,18 @@ + factory=".captcha.Captcha" + provides="plone.app.discussion.interfaces.ICaptcha" /> + + +