cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
__init__.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.recaptcha_django
22 #
23 # \c ReCAPTCHA (Completely Automated Public Turing test to tell Computers and
24 # Humans Apart - while helping digitize books, newspapers, and old time radio
25 # shows) module for django
26 #
27 
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
34 
35 
36 HUMAN_ERRORS = {
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.')
45 }
46 
47 
48 ##
49 #
50 # A Widget that renders a \c ReCAPTCHA form.
51 #
52 class ReCaptchaWidget(Widget):
53 
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)
58 
59  return mark_safe(u"""<script type="text/javascript">
60  var RecaptchaOptions = {
61  custom_translations : {
62  instructions_visual : "%s",
63  instructions_audio : "%s",
64  play_again : "%s",
65  cant_hear_this : "%s",
66  visual_challenge : "%s",
67  audio_challenge : "%s",
68  refresh_btn : "%s",
69  help_btn : "%s",
70  incorrect_try_again : "%s",
71  },
72  theme : 'clean'
73  };
74  </script>
75  %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'),
83  _('Help'),
84  _('Incorrect. Try again.'),
85  html))
86 
87  ##
88  #
89  # Generates Widget value from \c data dictionary.
90  #
91  def value_from_datadict(self, data, files, name):
92  try:
93  return {'challenge': data['recaptcha_challenge_field'],
94  'response': data['recaptcha_response_field'],
95  'ip': data['recaptcha_ip_field']}
96  except KeyError:
97  return None
98 
99 
100 ##
101 #
102 # Field definition for a \c ReCAPTCHA.
103 #
104 class ReCaptchaField(Field):
105  widget = ReCaptchaWidget
106 
107  def clean(self, value):
108  if value is None:
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.')))
117