source: products/quintagroup.z3cform.captcha/trunk/quintagroup/z3cform/captcha/validator.py @ 3671

Last change on this file since 3671 was 3671, checked in by kroman0, 11 years ago

flake8

File size: 1.8 KB
RevLine 
[1960]1from DateTime import DateTime
2
[3546]3from zope.interface import Interface
[1960]4from zope.component import adapts
5from zope.i18n import MessageFactory
6
7from Products.CMFCore.utils import getToolByName
8
[2215]9from quintagroup.captcha.core.utils import decrypt, parseKey, encrypt1, getWord
[1960]10
11from z3c.form.validator import SimpleFieldValidator
12
[3571]13from quintagroup.z3cform.captcha.interfaces import ICaptcha
[1960]14
15_ = MessageFactory('quintagroup.z3cform.captcha')
16
[3546]17
[1960]18class CaptchaValidator(SimpleFieldValidator):
19    """Captcha validator"""
20
21    adapts(Interface, Interface, Interface, ICaptcha, Interface)
22
23    def validate(self, value):
24        # Verify the user input against the captcha
[3259]25        if 'kss_z3cform_inline_validation' in self.request['URL']:
26            return
[1960]27        context = self.context
28        request = self.request
29        value = value or ''
30        captcha_type = context.getCaptchaType()
31        if captcha_type in ['static', 'dynamic']:
32            hashkey = request.get('%shashkey' % self.widget.form.prefix, '')
33            decrypted_key = decrypt(context.captcha_key, hashkey)
34            parsed_key = parseKey(decrypted_key)
[3546]35
[1960]36            index = parsed_key['key']
37            date = parsed_key['date']
[3546]38
[1960]39            if captcha_type == 'static':
40                img = getattr(context, '%s.jpg' % index)
41                solution = img.title
42                enc = encrypt1(value)
43            else:
44                enc = value
45                solution = getWord(int(index))
[3546]46
[1960]47            captcha_tool = getToolByName(context, 'portal_captchas')
[3671]48            captcha_tool_has_key = captcha_tool.has_key
49            if (enc != solution) or (captcha_tool_has_key(decrypted_key)) or \
[3570]50               (DateTime().timeTime() - float(date) > 3600):
[1960]51                raise ValueError(_(u'Please re-enter validation code.'))
52            else:
53                captcha_tool.addExpiredKey(decrypted_key)
Note: See TracBrowser for help on using the repository browser.