28 from django.conf
import settings
29 from django.forms
import Widget, Field, ValidationError
30 from django.utils.html
import conditional_escape
31 from django.utils.safestring
import mark_safe
32 from django.utils.translation
import get_language, ugettext_lazy
as _
33 from recaptcha.client
import captcha
37 'unknown': _(
u'Unknown error.'),
38 'invalid-site-public-key': _(
u'ReCAPTCHA is wrongly configured.'),
39 'invalid-site-private-key': _(
u'ReCAPTCHA is wrongly configured.'),
40 'invalid-request-cookie': _(
u'Bad reCAPTCHA challenge parameter.'),
41 'incorrect-captcha-sol': _(
u'The CAPTCHA solution was incorrect.'),
42 'verify-params-incorrect': _(
u'Bad reCAPTCHA verification parameters.'),
43 'invalid-referrer': _(
u'Provided reCAPTCHA API keys are not valid for this domain.'),
44 'recaptcha-not-reachable': _(
u'ReCAPTCHA could not be reached.')
54 def render(self, name, value, attrs=None):
55 final_attrs = self.build_attrs(attrs)
56 error = final_attrs.get(
'error',
None)
57 html = captcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY, error=error, use_ssl=
True)
59 return mark_safe(
u"""<script type="text/javascript">
60 var RecaptchaOptions = {
61 custom_translations : {
62 instructions_visual : "%s",
63 instructions_audio : "%s",
65 cant_hear_this : "%s",
66 visual_challenge : "%s",
67 audio_challenge : "%s",
70 incorrect_try_again : "%s",
76 """ % (_(
'Type the two words:'),
77 _(
'Type what you hear:'),
78 _(
'Play sound again'),
79 _(
'Download sound as MP3'),
80 _(
'Get a visual challenge'),
81 _(
'Get an audio challenge'),
82 _(
'Get a new challenge'),
84 _(
'Incorrect. Try again.'),
93 return {
'challenge': data[
'recaptcha_challenge_field'],
94 'response': data[
'recaptcha_response_field'],
95 'ip': data[
'recaptcha_ip_field']}
105 widget = ReCaptchaWidget
109 raise ValidationError(_(
'Invalid request'))
110 resp = captcha.submit(value.get(
'challenge',
None),
111 value.get(
'response',
None),
112 settings.RECAPTCHA_PRIVATE_KEY,
113 value.get(
'ip',
None))
114 if not resp.is_valid:
115 self.widget.attrs[
'error'] = resp.error_code
116 raise ValidationError(HUMAN_ERRORS.get(resp.error_code, _(
u'Unknown error.')))