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

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

fixed ViewPageTemplateFile? import

File size: 1.5 KB
Line 
1from zope.interface import implementer
2from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
3
4from z3c.form import interfaces
5from z3c.form import widget
6
7from Products.CMFCore.utils import getToolByName
8
9from z3c.form.browser.text import TextWidget
10
11
12class CaptchaError(Exception):
13    def __init__(self, value):
14        self.value = value
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 Quickinstaller or Plone Control Panel')
27
28   def render(self):
29       key = self.getCaptcha()
30       portal_url = getToolByName(self.form.context, 'portal_url')()
31       image_url = "%s/getCaptchaImage/%s"%(portal_url, key)
32
33       return u"""<input type="hidden" value="%s" name="%shashkey" />
34                   %s
35                   <img src="%s" alt="Enter the word"/>""" % (key,
36                                                              self.form.prefix,
37                                                              super(CaptchaWidget, self).render(),
38                                                              image_url)
39       return super(CaptchaWidget, self).template(self) 
40
41@implementer(interfaces.IFieldWidget)
42def CaptchaWidgetFactory(field, request):
43    return widget.FieldWidget(field, CaptchaWidget(request))
Note: See TracBrowser for help on using the repository browser.