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

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

PPP fixes

File size: 1.5 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"/>""" % (key,
37                                                              self.form.prefix,
38                                                              super(CaptchaWidget, self).render(),
39                                                              image_url)
40        return super(CaptchaWidget, self).template(self)
41
42
43@implementer(interfaces.IFieldWidget)
44def CaptchaWidgetFactory(field, request):
45    return widget.FieldWidget(field, CaptchaWidget(request))
Note: See TracBrowser for help on using the repository browser.