Tuesday, February 17, 2009

Trac RecaptchaRegisterPlugin problems

The RecaptchaRegisterPlugin Trac extension has a few minor defects I noticed while testing it out for production use. The first problem, the captcha input would disappear if any other fields were invalid. The second problem, if the captcha field was invalid, any existing for data was lost. Here is my updated version of process_request() that addresses both issues.
#RecaptchaRegister trac plugin fix.

# IRequestHandler methods
def process_request(self, req):
self.check_config()
action = req.args.get('action')

if req.method == 'POST' and action == 'create':
response = captcha.submit(
req.args['recaptcha_challenge_field'],
req.args['recaptcha_response_field'],
self.private_key,
req.remote_addr,
)
if not response.is_valid:
data = {'acctmgr' : { 'username' : req.args['user'],
'name' : req.args['name'],
'email' : req.args['email'],
},
}
data['registration_error'] = 'Captcha incorrect. Please try again.'
data['recaptcha_javascript'] = captcha.displayhtml(self.public_key)
data['recaptcha_theme'] = self.theme
return "recaptcharegister.html", data, None
else:
ret = super(RecaptchaRegistrationModule, self).process_request(req)
h, data, n = ret
data['recaptcha_javascript'] = captcha.displayhtml(self.public_key)
data['recaptcha_theme'] = self.theme
return "recaptcharegister.html", data, n
else:
ret = super(RecaptchaRegistrationModule, self).process_request(req)
h, data, n = ret
data['recaptcha_javascript'] = captcha.displayhtml(self.public_key)
data['recaptcha_theme'] = self.theme
return "recaptcharegister.html", data, n