form: improve rendering of CAPTCHA widget (#7859)

This commit is contained in:
Frédéric Péters 2015-08-18 11:08:41 +02:00
parent 23a1308f72
commit e264d59ff4
1 changed files with 16 additions and 5 deletions

View File

@ -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):