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

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

removed unused import

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    def __str__(self):
15        return repr(self.value)
16
17
18class CaptchaWidget(TextWidget):
19   
20   def getCaptcha(self):
21       try:
22           return self.form.context.getCaptcha()
23       except AttributeError:
24           raise CaptchaError('quintagroup.captcha.core not installed. '
25                              'Install quintagroup.captcha.core using Quickinstaller or Plone Control Panel')
26
27   def render(self):
28       key = self.getCaptcha()
29       portal_url = getToolByName(self.form.context, 'portal_url')()
30       image_url = "%s/getCaptchaImage/%s"%(portal_url, key)
31
32       return u"""<input type="hidden" value="%s" name="%shashkey" />
33                   %s
34                   <img src="%s" alt="Enter the word"/>""" % (key,
35                                                              self.form.prefix,
36                                                              super(CaptchaWidget, self).render(),
37                                                              image_url)
38       return super(CaptchaWidget, self).template(self) 
39
40@implementer(interfaces.IFieldWidget)
41def CaptchaWidgetFactory(field, request):
42    return widget.FieldWidget(field, CaptchaWidget(request))
Note: See TracBrowser for help on using the repository browser.