Skip to content

Instantly share code, notes, and snippets.

@hqman
Forked from mdellavo/recaptcha_validator.py
Created February 10, 2012 14:45
Show Gist options
  • Save hqman/1789987 to your computer and use it in GitHub Desktop.
Save hqman/1789987 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