From 987fcd65af22e1786a0234b9333125d7b3a96a33 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:09:15 +0200 Subject: [PATCH 2/3] fields: add a new "CAPTCHA" field (#7859) --- wcs/fields.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/wcs/fields.py b/wcs/fields.py index 8faf0b7..4b676bc 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -1707,6 +1707,47 @@ class PasswordField(WidgetField): register_field_class(PasswordField) +class CaptchaField(Field): + key = 'captcha' + description = N_('CAPTCHA') + in_listing = False + + hint = None + mode = 'arithmetic-simple' + extra_attributes = ['mode'] + + widget_class = CaptchaWidget + + def add_to_form(self, form, value=None): + if get_session().won_captcha: + form.add(HiddenWidget, 'f%s' % self.id) + return + if self.hint and self.hint.startswith('<'): + hint = htmltext(self.hint) + else: + hint = self.hint + form.add(self.widget_class, 'f%s' % self.id, title=self.label, hint=hint) + + def add_to_view_form(self, form, value=None): + form.add(HiddenWidget, 'f%s' % self.id) + + def get_admin_attributes(self): + return Field.get_admin_attributes(self) + self.extra_attributes + + def fill_admin_form(self, form): + form.add(StringWidget, 'label', title=_('Label'), value=self.label, + required=True, size=50) + form.add(TextWidget, 'hint', title=_('Hint'), value=self.hint, + cols=60, rows=3) + modes = [('arithmetic-simple', _('Simple Arithmetic (plus and minus)')), + ('arithmetic', _('Arithmetic (includes multiplication)')), + ] + form.add(SingleSelectWidget, 'mode', title=_('Mode'), + value=self.mode, options=modes) + +register_field_class(CaptchaField) + + def get_field_class_by_type(type): for k in field_classes: if k.key == type: -- 2.5.0