web/captcha.py
brancholdstable
changeset 5422 0865e1e90674
parent 5421 8167de96c523
child 5423 e15abfdcce38
child 5424 8ecbcbff9777
equal deleted inserted replaced
4985:02b52bf9f5f8 5422:0865e1e90674
       
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
     1 """Simple captcha library, based on PIL. Monkey patch functions in this module
    18 """Simple captcha library, based on PIL. Monkey patch functions in this module
     2 if you want something better...
    19 if you want something better...
     3 
    20 
     4 :organization: Logilab
       
     5 :copyright: 2009-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     8 """
    21 """
     9 __docformat__ = "restructuredtext en"
    22 __docformat__ = "restructuredtext en"
    10 
    23 
    11 from random import randint, choice
    24 from random import randint, choice
    12 from cStringIO import StringIO
    25 from cStringIO import StringIO
    15 
    28 
    16 
    29 
    17 from time import time
    30 from time import time
    18 
    31 
    19 from cubicweb import tags
    32 from cubicweb import tags
    20 from cubicweb.web import formwidgets as fw
    33 from cubicweb.web import ProcessFormError, formwidgets as fw
    21 
    34 
    22 
    35 
    23 def pil_captcha(text, fontfile, fontsize):
    36 def pil_captcha(text, fontfile, fontsize):
    24     """Generate a captcha image. Return a PIL image object.
    37     """Generate a captcha image. Return a PIL image object.
    25 
    38 
    61 
    74 
    62 
    75 
    63 class CaptchaWidget(fw.TextInput):
    76 class CaptchaWidget(fw.TextInput):
    64     def render(self, form, field, renderer=None):
    77     def render(self, form, field, renderer=None):
    65         # t=int(time()*100) to make sure img is not cached
    78         # t=int(time()*100) to make sure img is not cached
    66         src = form._cw.build_url('view', vid='captcha', t=int(time()*100))
    79         src = form._cw.build_url('view', vid='captcha', t=int(time()*100),
       
    80                                  captchakey=field.input_name(form))
    67         img = tags.img(src=src, alt=u'captcha')
    81         img = tags.img(src=src, alt=u'captcha')
    68         img = u'<div class="captcha">%s</div>' % img
    82         img = u'<div class="captcha">%s</div>' % img
    69         return img + super(CaptchaWidget, self).render(form, field, renderer)
    83         return img + super(CaptchaWidget, self).render(form, field, renderer)
       
    84 
       
    85     def process_field_data(self, form, field):
       
    86         captcha = form._cw.get_session_data(field.input_name(form), None,
       
    87                                             pop=True)
       
    88         val = super(CaptchaWidget, self).process_field_data(form, field)
       
    89         if val is None:
       
    90             return val # required will be checked by field
       
    91         if captcha is None:
       
    92             msg = form._cw._('unable to check captcha, please try again')
       
    93             raise ProcessFormError(msg)
       
    94         elif val.lower() != captcha.lower():
       
    95             msg = form._cw._('incorrect captcha value')
       
    96             raise ProcessFormError(msg)
       
    97         return val