[1960] | 1 | from zope.interface import implementer |
---|
| 2 | from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile |
---|
| 3 | |
---|
| 4 | from z3c.form import interfaces |
---|
| 5 | from z3c.form import widget |
---|
| 6 | |
---|
| 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | |
---|
| 9 | from z3c.form.browser.text import TextWidget |
---|
| 10 | |
---|
[3257] | 11 | |
---|
| 12 | class CaptchaError(BaseException): |
---|
| 13 | def __init__(self, value): |
---|
| 14 | self.value = value |
---|
| 15 | def __str__(self): |
---|
| 16 | return repr(self.value) |
---|
| 17 | |
---|
| 18 | |
---|
[1960] | 19 | class CaptchaWidget(TextWidget): |
---|
| 20 | |
---|
| 21 | def getCaptcha(self): |
---|
[3257] | 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') |
---|
[1960] | 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) |
---|
| 42 | def CaptchaWidgetFactory(field, request): |
---|
| 43 | return widget.FieldWidget(field, CaptchaWidget(request)) |
---|