Permalink
Browse files

i wish ted had told me the captcha didn't bloody work :'(

  • Loading branch information...
1 parent 36f709f commit fa5d0a3b93be3ec069b7cc65b9563b77614c298f Justine Tunney committed Jan 15, 2012
Showing with 40 additions and 21 deletions.
  1. +21 −12 occupywallst/fields.py
  2. +11 −0 occupywallst/middleware.py
  3. +1 −0 occupywallst/settings.py
  4. +7 −9 occupywallst/widgets.py
@@ -1,30 +1,39 @@
-from django.conf import settings
+r"""
+
+ occupywallst.fields
+ ~~~~~~~~~~~~~~~~~~~
+
+ Form fields and stuff
+
+"""
+
from django import forms
+from django.conf import settings
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy as _
-
from recaptcha.client import captcha
-from occupywallst.widgets import ReCaptcha
+from occupywallst import widgets
+
class ReCaptchaField(forms.CharField):
default_error_messages = {
- 'captcha_invalid': _(u'Invalid captcha')
+ 'captcha_invalid': _(u'Invalid captcha'),
}
def __init__(self, *args, **kwargs):
- self.widget = ReCaptcha
+ self.widget = widgets.ReCaptchaWidget
self.required = False
super(ReCaptchaField, self).__init__(*args, **kwargs)
def clean(self, values):
- return values[0]
super(ReCaptchaField, self).clean(values[1])
- recaptcha_challenge_value = smart_unicode(values[0])
- recaptcha_response_value = smart_unicode(values[1])
- check_captcha = captcha.submit(recaptcha_challenge_value,
- recaptcha_response_value, settings.RECAPTCHA_PRIVATE_KEY, {})
- if not check_captcha.is_valid:
+ challenge = smart_unicode(values[0])
+ response = smart_unicode(values[1])
+ ip = smart_unicode(values[2])
+ key = settings.RECAPTCHA_PRIVATE_KEY
+ res = captcha.submit(challenge, response, key, ip)
+ if not res.is_valid:
raise forms.util.ValidationError(
self.error_messages['captcha_invalid'])
- return values[0]
+ return challenge
@@ -52,3 +52,14 @@ def process_response(self, request, response):
if request.user.is_authenticated():
request.META["CSRF_COOKIE_USED"] = True
return response
+
+
+class ReCaptchaMiddleware(object):
+ """Add IP to ReCaptcha POSTs as workaround to django forms"""
+
+ def process_request(self, request):
+ if request.method == 'POST':
+ if 'recaptcha_challenge_field' in request.POST:
+ data = request.POST.copy()
+ data['recaptcha_magic_ip_field'] = request.META['REMOTE_ADDR']
+ request.POST = data
@@ -162,6 +162,7 @@
# 'django.middleware.csrf.CsrfViewMiddleware',
# 'occupywallst.middleware.CsrfCookieWhenLoggedIn',
'occupywallst.middleware.NeverCache',
+ 'occupywallst.middleware.ReCaptchaMiddleware',
]
INSTALLED_APPS = [
@@ -7,31 +7,29 @@
"""
-
from django import forms
from django.utils.safestring import mark_safe
from django.conf import settings
from recaptcha.client import captcha
-class ReCaptcha(forms.widgets.Widget):
- recaptcha_challenge_name = 'recaptcha_challenge_field'
- recaptcha_response_name = 'recaptcha_response_field'
-
+class ReCaptchaWidget(forms.widgets.Widget):
def render(self, name, value, attrs=None):
- return mark_safe(u'%s' % captcha.displayhtml(
- settings.RECAPTCHA_PUBLIC_KEY))
+ html = captcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY)
+ return mark_safe(unicode(html))
def value_from_datadict(self, data, files, name):
- return [data.get(self.recaptcha_challenge_name, None),
- data.get(self.recaptcha_response_name, None)]
+ return [data.get('recaptcha_challenge_field', None),
+ data.get('recaptcha_response_field', None),
+ data.get('recaptcha_magic_ip_field', None)]
class ImageWidget(forms.widgets.FileInput):
"""
A FileInput that displays an image and a copyable link instead of
a file path.
"""
+
def render(self, name, value, attrs=None):
# TODO: find the smart way to get the display image url
if hasattr(value, 'url'):

3 comments on commit fa5d0a3

@thirtyseven
Contributor

I'm sorry, last I checked it looked pretty work-y to me :(

@thirtyseven
Contributor

Seems I should have checked the API docs a bit more carefully...

@jart
Owner
jart commented on fa5d0a3 Jan 15, 2012

No worries <3 I'm still in debt to you for the help you've been able to offer this project!

Please sign in to comment.