source: products/quintagroup.z3cform.captcha/trunk/quintagroup/z3cform/captcha/widget.py

Last change on this file was 3571, checked in by vmaksymiv, 12 years ago

PPP fixes

File size: 1.4 KB
RevLine 
[1960]1from zope.interface import implementer
2
3from z3c.form import interfaces
4from z3c.form import widget
5
6from Products.CMFCore.utils import getToolByName
7
8from z3c.form.browser.text import TextWidget
9
[3257]10
[3468]11class CaptchaError(Exception):
[3257]12    def __init__(self, value):
13        self.value = value
[3545]14
[3257]15    def __str__(self):
16        return repr(self.value)
17
18
[1960]19class CaptchaWidget(TextWidget):
20
[3545]21    def getCaptcha(self):
22        try:
23            return self.form.context.getCaptcha()
24        except AttributeError:
25            raise CaptchaError('quintagroup.captcha.core not installed. '
26                               'Install quintagroup.captcha.core using '
27                               'Quickinstaller or Plone Control Panel')
[1960]28
[3545]29    def render(self):
30        key = self.getCaptcha()
31        portal_url = getToolByName(self.form.context, 'portal_url')()
32        image_url = "%s/getCaptchaImage/%s" % (portal_url, key)
33
34        return u"""<input type="hidden" value="%s" name="%shashkey" />
[1960]35                   %s
[3571]36                   <img src="%s" alt="Enter the word"/>""" % (
37            key,
38            self.form.prefix,
39            super(CaptchaWidget, self).render(),
40            image_url)
[3545]41        return super(CaptchaWidget, self).template(self)
[1960]42
[3545]43
[1960]44@implementer(interfaces.IFieldWidget)
45def CaptchaWidgetFactory(field, request):
46    return widget.FieldWidget(field, CaptchaWidget(request))
Note: See TracBrowser for help on using the repository browser.