[871] | 1 | from zope.component import getMultiAdapter |
---|
| 2 | from zope.app.form.browser import ASCIIWidget |
---|
| 3 | from zope.app.form.interfaces import ConversionError |
---|
| 4 | from zope.app.form.browser.textwidgets import renderElement |
---|
| 5 | |
---|
| 6 | from Acquisition import aq_inner |
---|
| 7 | |
---|
| 8 | from quintagroup.plonecaptchas import ProductMessageFactory as _ |
---|
| 9 | |
---|
| 10 | class 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) |
---|