Commits

George Notaras committed f29f281

The remote IP is now added through a middleware class.

  • Participants
  • Parent commits 87d7d93

Comments (0)

Files changed (3)

File src/recaptchaworks/fields.py

         The optional ``private_key`` argument can be used to override the
         default use of the project-wide ``RECAPTCHA_PRIVATE_KEY`` setting.
         """
-        self.remote_ip = None
         self.private_key = private_key or settings.RECAPTCHA_PRIVATE_KEY
         self.use_ssl = use_ssl or settings.RECAPTCHA_USE_SSL
         super(RecaptchaField, self).__init__(*args, **kwargs)
 
     def clean(self, value):
-        if not self.remote_ip:
-            raise forms.ValidationError(self.error_messages['no-remote-ip'])
+        
         value = super(RecaptchaField, self).clean(value)
-        challenge, response = value
+        challenge, response, remote_ip = value
         if not challenge:
             raise forms.ValidationError(self.error_messages['challenge-error'])
         if not response:
             raise forms.ValidationError(self.error_messages['required'])
+        if not remote_ip:
+            raise forms.ValidationError(self.error_messages['no-remote-ip'])
         try:
-            value = validate_recaptcha(self.remote_ip, challenge, response,
+            value = validate_recaptcha(remote_ip, challenge, response,
                                        self.private_key, self.use_ssl)
         except RecaptchaError, e:
             if e.code == 'incorrect-captcha-sol':

File src/recaptchaworks/middleware.py

+# -*- coding: utf-8 -*-
+#
+#  This file is part of django-recaptcha-works.
+#
+#  django-recaptcha-works is an easy-to-use Django forms integration of the
+#  reCaptcha service.
+#
+#  Based on the code snippet #1644 as published on:
+#    - http://djangosnippets.org/snippets/1644/
+#
+#  Copyright (c) 2009-2010 Chris Beaven (SmileyChris), http://smileychris.com/
+#  Copyright (c) 2010 George Notaras <gnot@g-loaded.eu>, http://www.g-loaded.eu/
+#
+#  django-recaptcha-works is based on the code snippet #1644 as published on:
+#    - http://djangosnippets.org/snippets/1644/
+#
+#  Development Web Site:
+#    - http://www.codetrax.org/projects/django-recaptcha-works
+#
+#  Public Source Code Repository:
+#    - https://source.codetrax.org/hgroot/django-recaptcha-works
+#
+#  Licensed under the BSD License.
+#
+#  Redistribution and use in source and binary forms, with or without modification,
+#  are permitted provided that the following conditions are met:
+#
+#      1. Redistributions of source code must retain the above copyright notice, 
+#         this list of conditions and the following disclaimer.
+#      
+#      2. Redistributions in binary form must reproduce the above copyright 
+#         notice, this list of conditions and the following disclaimer in the
+#         documentation and/or other materials provided with the distribution.
+#
+#      3. Neither the name of Django nor the names of its contributors may be used
+#         to endorse or promote products derived from this software without
+#         specific prior written permission.
+#
+#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+#  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+#  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+class ReCaptchaRemoteIPMiddleware(object):
+    """
+    This middleware class adds the ``recaptcha_remote_ip_field`` to the
+    POST payload of the form submission request. The value is set to the IP
+    address of the client which submitted the form.
+     
+    """
+    def process_request(self, request):
+        if request.method != 'POST':
+            return
+        if 'recaptcha_challenge_field' in request.POST and 'recaptcha_response_field' in request.POST:
+            # This is a recaptcha protected form
+            data = request.POST.copy()
+            data['recaptcha_remote_ip_field'] = request.META['REMOTE_ADDR']
+            request.POST = data
+

File src/recaptchaworks/widgets.py

     def value_from_datadict(self, data, files, name):
         challenge = data.get('recaptcha_challenge_field')
         response = data.get('recaptcha_response_field')
-        return (challenge, response)
+        # This is added by the recaptcha middleware
+        remote_ip = data.get('recaptcha_remote_ip_field')
+        return (challenge, response, remote_ip)
 
     def id_for_label(self, id_):
         return None
+