captcha registry entry and vocabulary added.

svn path=/plone.app.discussion/trunk/; revision=28679
This commit is contained in:
Timo Stollenwerk 2009-08-11 09:45:34 +00:00
parent 572b743d04
commit 7f7c4b1345
5 changed files with 88 additions and 8 deletions

View File

@ -7,21 +7,23 @@ from zope import interface, schema
from zope.annotation import factory from zope.annotation import factory
from zope.annotation.attribute import AttributeAnnotations from zope.annotation.attribute import AttributeAnnotations
from zope.component import adapts, provideAdapter from zope.component import adapts, provideAdapter, queryUtility
from zope.publisher.interfaces.browser import IDefaultBrowserLayer from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.interface import Interface, implements from zope.interface import Interface, implements
from plone.registry.interfaces import IRegistry
from plone.z3cform.fieldsets import extensible from plone.z3cform.fieldsets import extensible
from plone.z3cform.fieldsets.interfaces import IFormExtender from plone.z3cform.fieldsets.interfaces import IFormExtender
from plone.app.discussion.comment import Comment
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.interfaces import IDiscussionSettings
class ICaptcha(Interface): class ICaptcha(Interface):
captcha = schema.TextLine(title=u"Type the word 'human' in all capital letters.", captcha = schema.TextLine(title=u"Captcha",
required=False) required=True)
class Captcha(Persistent): class Captcha(Persistent):
interface.implements(ICaptcha) interface.implements(ICaptcha)
@ -40,6 +42,15 @@ class CaptchaExtender(extensible.FormExtender):
self.request = request self.request = request
self.form = form self.form = form
registry = queryUtility(IRegistry)
settings = registry.forInterface(IDiscussionSettings)
self.captcha = settings.captcha
def update(self): def update(self):
# Add all fields from the captcha interface if self.captcha != 'disabled':
self.add(ICaptcha, prefix="extra") # Add all fields from the captcha interface
self.add(ICaptcha, prefix="")
if self.captcha == 'captcha':
self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget
elif self.captcha == 'recaptcha':
self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget

View File

@ -71,6 +71,10 @@
handler=".tool.unindex_object" handler=".tool.unindex_object"
/> />
<!-- Captcha Vocabulary -->
<utility component=".vocabularies.captcha_vocabulary"
name="plone.app.discussion.vocabularies.CaptchaVocabulary" />
<!-- Conversation indexes --> <!-- Conversation indexes -->
<adapter name="total_comments" factory=".catalog.total_comments" /> <adapter name="total_comments" factory=".catalog.total_comments" />
<adapter name="last_comment_date" factory=".catalog.last_comment_date" /> <adapter name="last_comment_date" factory=".catalog.last_comment_date" />
@ -89,4 +93,4 @@
<adapter name="last_comment_date" factory=".catalog.comments_last_comment_date" /> <adapter name="last_comment_date" factory=".catalog.comments_last_comment_date" />
<adapter name="commentators" factory=".catalog.comments_commentators" /> <adapter name="commentators" factory=".catalog.comments_commentators" />
</configure> </configure>

View File

@ -25,6 +25,14 @@ class IDiscussionSettings(Interface):
description=_(u"Use this setting to enable or disable posting comments as anonymous visitor."), description=_(u"Use this setting to enable or disable posting comments as anonymous visitor."),
default=False) default=False)
captcha = schema.Choice(title=_(u"Captcha"),
description=_(u"""Use this setting to enable or disable captcha validation for comments.
If no captcha options are available, install captcha solutions like
plone.formwidget.captcha or plone.formwidget.recaptcha."""),
required=True,
default='disabled',
vocabulary='plone.app.discussion.vocabularies.CaptchaVocabulary',)
show_commenter_image = schema.Bool(title=_(u"Show commenter image"), show_commenter_image = schema.Bool(title=_(u"Show commenter image"),
description=_(u"Use this setting to enable or disable showing the commenter's image next to his/her comments."), description=_(u"Use this setting to enable or disable showing the commenter's image next to his/her comments."),
default=True) default=True)

View File

@ -1,5 +1,7 @@
import unittest import unittest
from zope.component import getMultiAdapter
from plone.registry import Registry from plone.registry import Registry
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
@ -31,6 +33,13 @@ class RegistryTest(PloneTestCase):
self.failUnless('globally_enabled' in IDiscussionSettings) self.failUnless('globally_enabled' in IDiscussionSettings)
self.assertEquals(self.registry['plone.app.discussion.interfaces.IDiscussionSettings.globally_enabled'], True) self.assertEquals(self.registry['plone.app.discussion.interfaces.IDiscussionSettings.globally_enabled'], True)
def test_captcha(self):
# Check globally_enabled record
globally_enabled_record = self.registry.records['plone.app.discussion.interfaces.IDiscussionSettings.captcha']
self.failUnless('captcha' in IDiscussionSettings)
self.assertEquals(self.registry['plone.app.discussion.interfaces.IDiscussionSettings.captcha'], 'disabled')
def test_anonymous_comments(self): def test_anonymous_comments(self):
# Check anonymous_comments record # Check anonymous_comments record
anonymous_comments_record = self.registry.records['plone.app.discussion.interfaces.IDiscussionSettings.anonymous_comments'] anonymous_comments_record = self.registry.records['plone.app.discussion.interfaces.IDiscussionSettings.anonymous_comments']

View File

@ -0,0 +1,48 @@
from zope import interface
from zope import component
import zope.schema.interfaces
import zope.schema.vocabulary
# XXX: REPLACE THIS!!!
HAS_CAPTCHA=False
HAS_RECAPTCHA=False
try:
from plone.formwidget.captcha.widget import CaptchaFieldWidget
HAS_CAPTCHA = True
except ImportError:
pass
try:
from plone.formwidget.captcha.widget import ReCaptchaFieldWidget
HAS_RECAPTCHA = True
except ImportError:
pass
# XXX: REPLACE THIS!!!
def captcha_vocabulary(context):
"""Vocabulary with all available captcha implementations.
"""
terms = []
terms.append(
zope.schema.vocabulary.SimpleTerm(
value='disabled',
token='disabled',
title='Disabled'))
if HAS_CAPTCHA:
terms.append(
zope.schema.vocabulary.SimpleTerm(
value='captcha',
token='captcha',
title='Captcha'))
if HAS_RECAPTCHA:
terms.append(
zope.schema.vocabulary.SimpleTerm(
value='recaptcha',
token='recaptcha',
title='ReCaptcha'))
return zope.schema.vocabulary.SimpleVocabulary(terms)
interface.alsoProvides(captcha_vocabulary,
zope.schema.interfaces.IVocabularyFactory)