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.browser.comments import CommentForm
from plone.app.discussion.comment import Comment from plone.app.discussion.comment import Comment
from plone.app.discussion.interfaces import IDiscussionSettings from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha
try: try:
from plone.formwidget.captcha import CaptchaFieldWidget from plone.formwidget.captcha import CaptchaFieldWidget
@ -30,22 +30,19 @@ try:
except ImportError: except ImportError:
pass pass
class ICaptcha(Interface):
captcha = schema.TextLine(title=u"Captcha",
required=True)
class Captcha(Persistent): class Captcha(Persistent):
interface.implements(ICaptcha) interface.implements(ICaptcha)
adapts(Comment) adapts(Comment)
captcha = u"" captcha = u""
Captcha = factory(Captcha) Captcha = factory(Captcha)
provideAdapter(Captcha)
class CaptchaExtender(extensible.FormExtender): class CaptchaExtender(extensible.FormExtender):
adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form
fields = Fields(ICaptcha)
fields['captcha'].widgetFactory = CaptchaFieldWidget
def __init__(self, context, request, form): def __init__(self, context, request, form):
self.context = context self.context = context
self.request = request self.request = request
@ -62,4 +59,9 @@ class CaptchaExtender(extensible.FormExtender):
if self.captcha == 'captcha': if self.captcha == 'captcha':
self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget
elif self.captcha == 'recaptcha': 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): def handleComment(self, action):
data, errors = self.extractData() 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'): if data.has_key('title') and data.has_key('text'):
title = data['title'] title = data['title']

View File

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

View File

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