source: products/quintagroup.plonecaptchas/trunk/quintagroup/plonecaptchas/browser/widget.py @ 1542

Last change on this file since 1542 was 871, checked in by crchemist, 17 years ago

Fixed http://projects.quintagroup.com/products/ticket/77 ticket

File size: 1.3 KB
Line 
1from zope.component import getMultiAdapter
2from zope.app.form.browser import ASCIIWidget
3from zope.app.form.interfaces import ConversionError
4from zope.app.form.browser.textwidgets import renderElement
5
6from Acquisition import aq_inner
7
8from quintagroup.plonecaptchas import ProductMessageFactory as _
9
10class CaptchaWidget(ASCIIWidget):
11    def __call__(self):
12        captcha = getMultiAdapter((aq_inner(self.context.context), self.request), name='captcha')
13        kwargs = {'type': self.type,
14                  'name': self.name,
15                  'id': self.name,
16                  'cssClass': self.cssClass,
17                  'style': self.style,
18                  'size': self.displayWidth,
19                  'extra': self.extra}
20        if self.displayMaxWidth:
21            kwargs['maxlength'] = self.displayMaxWidth # TODO This is untested.
22
23        return u"""<div class="captchaImage">%s</div>%s""" % \
24            (captcha.image_tag(), renderElement(self.tag, **kwargs))
25
26    def _toFieldValue(self, input):
27        # Verify the user input against the captcha
28        captcha = getMultiAdapter((aq_inner(self.context.context), self.request), name='captcha')
29        if not captcha.verify(input):
30            raise ConversionError(_(u"Please re-enter validation code."))
31        return super(CaptchaWidget, self)._toFieldValue(input)
Note: See TracBrowser for help on using the repository browser.