captcha validator added.

svn path=/plone.app.discussion/trunk/; revision=28746
This commit is contained in:
Timo Stollenwerk 2009-08-12 20:45:47 +00:00
parent 0ba3bda065
commit e1b068dbbc
4 changed files with 32 additions and 9 deletions

View File

@ -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
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'])

View File

@ -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']

View File

@ -66,12 +66,18 @@
<!-- Comment form extender -->
<adapter
factory=".captcha.Captcha" />
factory=".captcha.Captcha"
provides="plone.app.discussion.interfaces.ICaptcha" />
<adapter
factory=".captcha.CaptchaExtender"
provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />
<!-- Captcha validator -->
<adapter
factory=".captcha.CaptchaValidator"
/>
<!-- Comment view -->
<browser:view
name="view"

View File

@ -163,6 +163,10 @@ class IComment(Interface):
creation_date = schema.Date(title=_(u"Creation date"))
modification_date = schema.Date(title=_(u"Modification date"))
class ICaptcha(Interface):
captcha = schema.TextLine(title=u"Captcha",
required=True)
class ICommentingTool(Interface):
"""A tool that indexes all comments for usage by the management interface.