source: products/quintagroup.formlib.captcha/trunk/quintagroup/formlib/captcha/widget.py @ 2210

Last change on this file since 2210 was 2210, checked in by mylan, 14 years ago

Merged all changes from branches/captchas-refactoring into trunk.

Merged revisions 3004,3006,3094,3096-3099,3119-3120 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.formlib.captcha/branches/captchas-refactoring

........

r3004 | liebster | 2010-03-22 16:16:05 +0200 (Mon, 22 Mar 2010) | 1 line


Remove egg info

........

r3006 | liebster | 2010-03-22 16:20:03 +0200 (Mon, 22 Mar 2010) | 1 line


Add depending quintagroup.captcha.core

........

r3094 | mylan | 2010-04-01 16:53:05 +0300 (Thu, 01 Apr 2010) | 1 line


#173: Fix wrong import, update package description, add zope.formlib as dependency

........

r3096 | mylan | 2010-04-01 17:39:30 +0300 (Thu, 01 Apr 2010) | 1 line


#173: update dependencies list in quintagroup.formlib.captcha package, commented for now

........

r3097 | mylan | 2010-04-01 18:48:49 +0300 (Thu, 01 Apr 2010) | 1 line


#173: Move quintagroup.formlib.captcha.form content into the quintagroup.formlib.captcha, add zope.app.form as direct requirement

........

r3098 | mylan | 2010-04-02 13:38:30 +0300 (Fri, 02 Apr 2010) | 1 line


#186: Remove needless namespace declarations in configure.zcml

........

r3099 | mylan | 2010-04-02 13:40:52 +0300 (Fri, 02 Apr 2010) | 1 line


#186: added tests for field and widget staff registration, for captcha widget operation

........

r3119 | mylan | 2010-04-08 16:42:00 +0300 (Thu, 08 Apr 2010) | 1 line


#186: Add dependency package include in configure.zcml

........

r3120 | mylan | 2010-04-08 16:42:34 +0300 (Thu, 08 Apr 2010) | 1 line


#186: Fixed registration tests

........

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1from DateTime import DateTime
2
3from zope.component import getMultiAdapter
4from zope.app.form.browser import ASCIIWidget
5from zope.app.form.interfaces import ConversionError
6from zope.app.form.browser.textwidgets import renderElement
7from zope.i18n import MessageFactory
8
9from Acquisition import aq_inner
10
11from Products.CMFCore.utils import getToolByName
12
13from quintagroup.captcha.core.utils import decrypt, parseKey, encrypt1, getWord
14
15_ = MessageFactory('quintagroup.formlib.captcha')
16
17
18class CaptchaWidget(ASCIIWidget):
19    def __call__(self):
20        context = self.context.context
21
22        kwargs = {'type': self.type,
23                  'name': self.name,
24                  'id': self.name,
25                  'cssClass': self.cssClass,
26                  'style': self.style,
27                  'size': self.displayWidth,
28                  'extra': self.extra}
29
30        portal_url = getToolByName(context, 'portal_url')()
31        key = context.getCaptcha()
32
33        if self._prefix:
34            prefix = '%s.' % self._prefix
35        else:
36            prefix = ''
37
38        return u"""<input type="hidden" value="%s" name="%shashkey" />
39                   %s
40                   <img src="%s/getCaptchaImage/%s" alt="Enter the word"/>""" % (key,
41                                                                                 prefix,
42                                                                                 renderElement(self.tag, **kwargs),
43                                                                                 portal_url,
44                                                                                 key)
45         
46    def _toFieldValue(self, input):
47        # Verify the user input against the captcha
48        context = self.context.context
49        request = context.REQUEST
50       
51        # get captcha type (static or dynamic)
52        captcha_type = context.getCaptchaType()
53       
54        # validate captcha input
55        if input and captcha_type in ['static', 'dynamic']:
56            # make up form prefix
57            if self._prefix:
58                prefix = '%s.' % self._prefix
59            else:
60                prefix = ''
61           
62            hashkey = request.get('%shashkey' % prefix, '')
63            decrypted_key = decrypt(context.captcha_key, hashkey)
64            parsed_key = parseKey(decrypted_key)
65           
66            index = parsed_key['key']
67            date = parsed_key['date']
68           
69            if captcha_type == 'static':
70                img = getattr(context, '%s.jpg' % index)
71                solution = img.title
72                enc = encrypt1(input)
73            else:
74                enc = input
75                solution = getWord(int(index))
76           
77            captcha_tool = getToolByName(context, 'portal_captchas')
78            if (enc != solution) or (captcha_tool.has_key(decrypted_key)) or (DateTime().timeTime() - float(date) > 3600):
79                raise ConversionError(_(u'Please re-enter validation code.'))
80            else:
81                captcha_tool.addExpiredKey(decrypted_key)
82       
83        return super(CaptchaWidget, self)._toFieldValue(input)
Note: See TracBrowser for help on using the repository browser.