From f4f0e29e968467bb8ec9367bb9f2f3a34c2855a8 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Thu, 13 Aug 2009 10:01:40 +0000 Subject: [PATCH] fix errors that are thrown when plone.formwidget.captcha is not installed. svn path=/plone.app.discussion/trunk/; revision=28755 --- plone/app/discussion/browser/captcha.py | 81 +++++++++++++++++-------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/plone/app/discussion/browser/captcha.py b/plone/app/discussion/browser/captcha.py index 3d3de3c..9a3c4f6 100644 --- a/plone/app/discussion/browser/captcha.py +++ b/plone/app/discussion/browser/captcha.py @@ -1,5 +1,6 @@ from persistent import Persistent +from z3c.form import validator from z3c.form.field import Fields from zope import interface, schema @@ -20,15 +21,27 @@ from plone.app.discussion.browser.comments import CommentForm from plone.app.discussion.comment import Comment from plone.app.discussion.interfaces import IDiscussionSettings, ICaptcha +HAS_CAPTCHA = False try: from plone.formwidget.captcha import CaptchaFieldWidget + from plone.formwidget.captcha.validator import CaptchaValidator + HAS_CAPTCHA = True except ImportError: - pass + # Fall back to SimpleFieldValidator, when Captcha is not installed, + # since otherwise the registration of the Captcha validator adapter + # would fail. + from z3c.form.validator import SimpleFieldValidator as CaptchaValidator +HAS_RECAPTCHA = False try: from plone.formwidget.recaptcha import ReCaptchaFieldWidget + HAS_RECAPTCHA = True except ImportError: - pass + # Fall back to SimpleFieldValidator, when ReCaptcha is not installed, + # since otherwise the registration of the ReCaptcha validator adapter + # would fail. + from z3c.form.validator import SimpleFieldValidator as CaptchaValidator + class Captcha(Persistent): interface.implements(ICaptcha) @@ -37,31 +50,49 @@ class Captcha(Persistent): Captcha = factory(Captcha) -class CaptchaExtender(extensible.FormExtender): - adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form +if HAS_CAPTCHA or HAS_RECAPTCHA: + # Extend the comment form with captcha, only if a captcha solution is installed. + class CaptchaExtender(extensible.FormExtender): + adapts(Interface, IDefaultBrowserLayer, CommentForm) # context, request, form - fields = Fields(ICaptcha) - fields['captcha'].widgetFactory = CaptchaFieldWidget + fields = Fields(ICaptcha) - def __init__(self, context, request, form): - self.context = context - self.request = request - self.form = form + def __init__(self, context, request, form): + self.context = context + self.request = request + self.form = form - registry = queryUtility(IRegistry) - settings = registry.forInterface(IDiscussionSettings) - self.captcha = settings.captcha + registry = queryUtility(IRegistry) + settings = registry.forInterface(IDiscussionSettings) + self.captcha = settings.captcha - def update(self): - if self.captcha != 'disabled': - # 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 + def update(self): + if self.captcha != 'disabled': + # Add captcha field if captcha is enabled in the registry + self.add(ICaptcha, prefix="") + if HAS_CAPTCHA and self.captcha == 'captcha': + # If Captcha is installed and Captcha is enabled, + # use the CaptchaFieldWidget + self.form.fields['captcha'].widgetFactory = CaptchaFieldWidget + elif HAS_RECAPTCHA and self.captcha == 'recaptcha': + # If ReCaptcha is installed and ReCaptcha is enabled, + # use the ReCaptchaFieldWidget + self.form.fields['captcha'].widgetFactory = ReCaptchaFieldWidget + else: + # + pass -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 + # Register Captcha validator for the captcha field in the ICaptchaForm + validator.WidgetValidatorDiscriminators(CaptchaValidator, field=ICaptcha['captcha']) + +else: + # This is necessary, otherwise the zcml registration of the CaptchaExtender + # would fail if no captcha solution is installed. + class CaptchaExtender(extensible.FormExtender): + adapts(Interface, IDefaultBrowserLayer, CommentForm) + + def __init__(self, context, request, form): + pass + + def update(self): + pass \ No newline at end of file