Random ...
 
December 2017
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags ...
Links



python+zope++:: recaptcha.net and zope
Posted at 28.Feb,2008 09:33  Comments 2 / Trackbacks 0 / Like this post!
Technorati tag(s):

Here's how to use recaptcha.net with zope.

Get your private and public recaptcha.net key.

Download the python recaptcha-client

Untar the client, and get captcha.py somewhere in the subdirectories. We're not installing this since we're using it exclusively with zope.

Edit captcha.py. We're not going to use RecaptchaResponse class since we're returning directly to zope objects.

Here's the diff:

  7,10c7,10
  < class RecaptchaResponse(object):
  <     def __init__(self, is_valid, error_code=None):
  <         self.is_valid = is_valid
  <         self.error_code = error_code
  ---
  > #class RecaptchaResponse(object):
  > #    def __init__(self, is_valid, error_code=None):
  > #        self.is_valid = is_valid
  > #        self.error_code = error_code
  60c60
  <         return RecaptchaResponse (is_valid = False, error_code   = 'incorrect-captcha-sol')
  ---
  >         return  (False, 'incorrect-captcha-sol')
  88c88
  <         return RecaptchaResponse (is_valid=True)
  ---
  >         return (True,)
  90c90
  <         return RecaptchaResponse (is_valid=False, error_code =   return_values [1])
  ---
  >         return (False, return_values [1])

Once edited, put captcha.py in INSTANCE_HOME/Extensions.

Now we need to create External Methods for displayhtml and submit.

Go to ZMI, add External Method, give an id (displayhtml), module (captcha), and function (displayhtml).

Create another External Method, give an id (submitcaptcha), module (captcha) and function (submit).

Once done, we can call displayhtml to display the recaptcha.net's html and use submitcaptcha to verify whether the input is correct.

For example, say we have input form, insertData that send action to doInsert, we can do:

  #insertData
  #<form action="doInsert">
  #.. other form fields
  <dtml-var expr="displayhtml(public_key='mmyyppuubblliicckkeeyy')"> or 
  <x tal:replace="python: here.displayhtml(public_key='mmyyppuubblliicckkeeyy')" />
  #<input type="submit">
  #</form>

and in doInsert:

  #Script (Python)
  #
  r=context.REQUEST

  PVT_KEY='yyoouurrpprriivvaatteekkeeyyhheerree'

  if r.environ.has_key('HTTP_X_FORWARDED_FOR'):
    myip = r.environ['HTTP_X_FORWARDED_FOR']
  elif r.environ.has_key('REMOTE_ADDR'):
    myip = r.environ['REMOTE_ADDR']

  cres = context.submitcaptcha(r.form['recaptcha_challenge_field'],r.form['recaptcha_response_field'],
        PVT_KEY, myip)

  if cres[0] is True:
    #do whatever that comes naturally
  else:
    #bzt! no go

I hope to make a wrapper that will make this dance easier. But don't bet your life on it :P


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/181/tbping 

Comments
Just what I needed, thanks!

This is a great how-to. Just what I needed to save hours of hacking trying to figure this out and get reCAPTCHA working on a zope site.

Thank you!
John S.

John Schinnerer  at 08.Nov,2008 09:04
good to hear

i still haven't yet done the zope wrapper. maybe some time this yer? :P

kedai  at 08.Nov,2008 19:35
Post a comment