Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created August 7, 2010 18:03
Show Gist options
  • Save mdellavo/513035 to your computer and use it in GitHub Desktop.
Save mdellavo/513035 to your computer and use it in GitHub Desktop.
try:
from recaptcha.client import captcha
from pylons import request, config
class RecaptchaValidator(Schema):
'''
Validates ReCAPTCHA against their web service
'''
challenge = validators.String(not_empty=True, strip=True)
response = validators.String(not_empty=True, strip=True)
def to_python(self, value, state):
challenge = value.get('recaptcha_challenge_field', '')
response = value.get('recaptcha_response_field', '')
key = config['recaptcha_private_key']
remoteip = request.remote_addr
recaptcha_response = captcha.submit(challenge, response, key, remoteip)
if not recaptcha_response.is_valid:
raise Invalid('Please enter the words shown', value, state)
return value
except ImportError, e:
RecaptchaValidator = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment