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

Last change on this file since 1861 was 1861, checked in by liebster, 14 years ago

Changed the location of the captcha image on the forms

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"""%s%s""" % \
24            (renderElement(self.tag, **kwargs), captcha.image_tag())
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.