def render(self, name, value, attrs=None): if isinstance(settings.RECAPTCHA_PUBLIC_KEY, dict): request = obtain_request() public_key = settings.RECAPTCHA_PUBLIC_KEY[request.get_host().lower()] else: public_key = settings.RECAPTCHA_PUBLIC_KEY html = displayhtml(public_key, theme=RECAPTCHA_THEME, lang=RECAPTCHA_LANG) return mark_safe(html)
def clean(self, value): challenge, response = super(RecaptchaField, self).clean(value) if not challenge: raise forms.ValidationError(_('An error occured with the CAPTCHA service. Please try again.')) if not response: raise forms.ValidationError(_('Please enter the CAPTCHA solution.')) remote_ip = obtain_request().META['REMOTE_ADDR']; if isinstance(settings.RECAPTCHA_PRIVATE_KEY, dict): request = obtain_request() private_key = settings.RECAPTCHA_PRIVATE_KEY[request.get_host().lower()] else: private_key = settings.RECAPTCHA_PRIVATE_KEY rc = submit(challenge, response, private_key, remote_ip) if not rc.is_valid: msg = ERROR_CODES.get(rc.error_code, ERROR_CODES['unknown']) raise forms.ValidationError(msg) return True