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
Line 
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
10
11class CaptchaError(Exception):
12    def __init__(self, value):
13        self.value = value
14
15    def __str__(self):
16        return repr(self.value)
17
18
19class CaptchaWidget(TextWidget):
20
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')
28
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" />
35                   %s
36                   <img src="%s" alt="Enter the word"/>""" % (
37            key,
38            self.form.prefix,
39            super(CaptchaWidget, self).render(),
40            image_url)
41        return super(CaptchaWidget, self).template(self)
42
43
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.