From 6f5e5261beb713f5ebb0cbf346889562247a2317 Mon Sep 17 00:00:00 2001 From: jordan Date: Thu, 15 Mar 2012 03:10:42 +0000 Subject: [PATCH] general improvements of the recaptcha client git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1233 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum_modules/recaptcha/formfield.py | 3 +- forum_modules/recaptcha/lib/captcha.py | 60 +++++++++----------------- 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/forum_modules/recaptcha/formfield.py b/forum_modules/recaptcha/formfield.py index 88b9497..26ece87 100644 --- a/forum_modules/recaptcha/formfield.py +++ b/forum_modules/recaptcha/formfield.py @@ -27,6 +27,7 @@ class ReCaptchaWidget(forms.Widget): return mark_safe(force_unicode(captcha.displayhtml(settings.RECAPTCHA_PUB_KEY))) def value_from_datadict(self, data, files, name): - return (data.get('recaptcha_challenge_field', None), data.get('recaptcha_response_field', None)) + + return data.get('recaptcha_challenge_field', None), data.get('recaptcha_response_field', None) diff --git a/forum_modules/recaptcha/lib/captcha.py b/forum_modules/recaptcha/lib/captcha.py index c165dc9..74ec8aa 100644 --- a/forum_modules/recaptcha/lib/captcha.py +++ b/forum_modules/recaptcha/lib/captcha.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import urllib2, urllib API_SSL_SERVER="https://www.google.com/recaptcha/api" @@ -9,35 +11,22 @@ class RecaptchaResponse(object): self.is_valid = is_valid self.error_code = error_code -def displayhtml (public_key, - use_ssl = False, - error = None): - """Gets the HTML to display for reCAPTCHA - - public_key -- The public api key - use_ssl -- Should the request be sent over ssl? - error -- An error message to display (from RecaptchaResponse.error_code)""" +def displayhtml (public_key): - error_param = '' - if error: - error_param = '&error=%s' % error + return """ +
+ - if use_ssl: - server = API_SSL_SERVER - else: - server = API_SERVER + - return """ - - """ % { - 'ApiServer' : server, 'PublicKey' : public_key, - 'ErrorParam' : error_param, } @@ -45,15 +34,6 @@ def submit (recaptcha_challenge_field, recaptcha_response_field, private_key, remoteip): - """ - Submits a reCAPTCHA request for verification. Returns RecaptchaResponse - for the request - - recaptcha_challenge_field -- The value of recaptcha_challenge_field from the form - recaptcha_response_field -- The value of recaptcha_response_field from the form - private_key -- your reCAPTCHA private key - remoteip -- the user's ip address - """ if not (recaptcha_response_field and recaptcha_challenge_field and len (recaptcha_response_field) and len (recaptcha_challenge_field)): @@ -81,14 +61,14 @@ def submit (recaptcha_challenge_field, } ) - httpresp = urllib2.urlopen (request) + httpresp = urllib2.urlopen(request) - return_values = httpresp.read ().splitlines (); - httpresp.close(); + return_values = httpresp.read().splitlines() + httpresp.close() - return_code = return_values [0] + return_code = return_values[0] - if (return_code == "true"): - return RecaptchaResponse (is_valid=True) + if return_code == "true": + return RecaptchaResponse(is_valid = True) else: - return RecaptchaResponse (is_valid=False, error_code = return_values [1]) \ No newline at end of file + return RecaptchaResponse(is_valid = False, error_code = return_values[1]) -- 2.39.5