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

Last change on this file since 3124 was 3124, checked in by vmaksymiv, 13 years ago

pylint fixes

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