From 327319f9a6fd83c711dac25f54020a68deb940e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 18 Aug 2015 11:08:41 +0200 Subject: [PATCH 1/3] form: improve rendering of CAPTCHA widget (#7859) --- wcs/qommon/form.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 96276bd..754b044 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -250,9 +250,9 @@ class Form(QuixoteForm): if self.get_widget('__keep_referer'): return self.get_widget('__keep_referer').parse() - def add_captcha(self): + def add_captcha(self, hint=None): if not self.captcha and not (get_session().won_captcha or get_session().user): - self.captcha = CaptchaWidget('captcha') + self.captcha = CaptchaWidget('captcha', hint=hint) def add(self, widget_class, name, *args, **kwargs): if kwargs and not kwargs.has_key('render_br'): @@ -1079,6 +1079,7 @@ class FileSizeWidget(ValidatedStringWidget): class CaptchaWidget(CompositeWidget): def __init__(self, name, value = None, mode = 'arithmetic-simple', *args, **kwargs): CompositeWidget.__init__(self, name, value, **kwargs) + self.render_br = False if value: token = value else: @@ -1104,10 +1105,12 @@ class CaptchaWidget(CompositeWidget): if b > a: a, b = b, a answer = a - b - question = _('What is the result of %(a)d %(op)s %(b)d?') % { + self.question = _('What is the result of %(a)d %(op)s %(b)d?') % { 'a': a, 'b': b, 'op': _(operator)} - hint = _('Please answer this simple mathematical question as proof you are not a bot.') - self.add(StringWidget, 'q', title = question, hint = hint, required=True) + self.hint = kwargs.get('hint') + if self.hint is None: + self.hint = _('Please answer this simple mathematical question as proof you are not a bot.') + self.add(StringWidget, 'q', required=True) token['answer'] = str(answer) def _parse(self, request): @@ -1120,6 +1123,14 @@ class CaptchaWidget(CompositeWidget): else: self.error = _('wrong answer') + def get_title(self): + return self.question + + def render_content(self): + r = TemplateIO(html=True) + for widget in self.get_widgets(): + r += widget.render_content() + return r.getvalue() class WidgetList(quixote.form.widget.WidgetList): def render(self): -- 2.5.0