George Notaras avatar George Notaras committed 0e01afa

Initial code split into several files

Comments (0)

Files changed (17)

+django-recaptcha-works Authors
+------------------------------
+Chris Beaven (SmileyChris), http://smileychris.com/
+  * author of the original code as published on djangosnippets.org:
+    - http://djangosnippets.org/snippets/1644/
+
+George Notaras <gnot@g-loaded.eu>, http://www.g-loaded.eu/
+  * release as a Django app
+  * SSL support
+  * migration to the Google hosted reCAPTCHA API
+  * extension of the available app settings

Empty file added.

+Copyright (c) Django Software Foundation and individual contributors.
+All rights reserved.
+
+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.
+include AUTHORS
+include INSTALL
+include LICENSE
+include MANIFEST.in
+include NOTICE
+include README
+include setup.cfg
+include setup.py
+recursive-include src/recaptchaworks *.py *.json *.html 
+"""
+An easy-to-use Django forms integration of the reCaptcha service.
+
+
+To use, simply base your form off the ``RecaptchaForm`` class. This class adds
+a new argument that must be provided to the form, ``remote_ip``.
+
+Two settings which must be set in your project's ``settings`` module are 
+``RECAPTCHA_PUBLIC_KEY`` and ``RECAPTCHA_PRIVATE_KEY``, the public and private
+keys for your domain, respectively. 
+
+Following is an example of creating a basic comment form class and then
+using an instance of the form in a view::
+
+    from django import forms
+    from mysite.utils import recaptcha
+
+    class CommentForm(recaptcha.RecaptchaForm):
+        name = forms.CharField()
+        comment = forms.CharField(widget=Textarea())
+        captcha = recaptcha.RecaptchaField()
+
+    def comment(request):
+        comment_form = CommentForm(remote_ip=request.META['REMOTE_ADDR'])
+        ...
+
+If you need to use a different base form (such as ``ModelForm``), use multiple
+inheritance like so::
+
+    class MyModelForm(BaseRecaptchaForm, ModelForm):
+        ...
+"""
+[install]
+optimize = 1
+
+[bdist_rpm]
+doc_files = AUTHORS INSTALL LICENSE NOTICE README
+group = Development/Libraries
+#! /usr/bin/env python
+# -*- 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.
+#
+#  NOTES
+#
+#  Create source distribution tarball:
+#    python setup.py sdist --formats=gztar
+#
+#  Create binary distribution rpm:
+#    python setup.py bdist --formats=rpm
+#
+#  Create binary distribution rpm with being able to change an option:
+#    python setup.py bdist_rpm --release 7
+#
+#  Test installation:
+#    python setup.py install --prefix=/usr --root=/tmp
+#
+#  Install:
+#    python setup.py install
+#  Or:
+#    python setup.py install --prefix=/usr
+#
+
+import sys
+import os
+sys.path.append(os.path.abspath('src'))
+
+from setuptools import setup, find_packages
+
+from recaptcha_works import get_version 
+
+
+if __name__=='__main__':
+    setup(
+        name = 'django-recaptcha-works',
+        version = get_version(),
+        author = 'Chris Beaven, George Notaras',
+        author_email = 'gnot@g-loaded.eu',
+        maintainer = 'George Notaras',
+        maintainer_email = 'gnot@g-loaded.eu',
+        url = 'http://www.codetrax.org/projects/django-recaptcha-works',
+        description = 'django-recaptcha-works is an easy-to-use Django forms integration of the reCaptcha service.',
+        long_description = 'django-recaptcha-works is an easy-to-use Django forms integration of the reCaptcha service.',
+        download_url = 'https://source.codetrax.org/hgroot/django-recaptcha-works',
+        classifiers = [
+            'Development Status :: 4 - Beta',
+            'Environment :: Web Environment',
+            'Framework :: Django',
+            'Intended Audience :: Developers',
+            'License :: OSI Approved :: BSD License',
+            'Natural Language :: English',
+            'Operating System :: OS Independent',
+            'Programming Language :: Python',
+            'Topic :: Software Development :: Libraries :: Application Frameworks',
+            'Topic :: Software Development :: Libraries :: Python Modules',
+        ],
+        license = 'BSD License (3 clause)',
+        package_dir = {'': 'src'},
+        packages = find_packages(),
+        include_package_data = True,
+    )
+

src/recaptchaworks/__init__.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.
+#
+
+VERSION = (1, 0, 2, 'final', 0)
+
+def get_version():
+    version = '%d.%d.%d' % (VERSION[0], VERSION[1], VERSION[2])
+    return version
+
+

src/recaptchaworks/exceptions.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 RecaptchaError(Exception):
+    def __init__(self, code):
+        self.code = code
+
+    def __str__(self):
+        return self.code
+

src/recaptchaworks/fields.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.
+#
+
+from django import forms
+
+from recaptchaworks.widgets import RecaptchaWidget
+from recaptchaworks import settings
+from recaptchaworks.exceptions import RecaptchaError
+from recaptchaworks.utils import validate_recaptcha
+
+
+class RecaptchaField(forms.Field):
+    widget = RecaptchaWidget
+    default_error_messages = {
+        'required': u'Please enter the CAPTCHA solution.',
+        'invalid': u'An incorrect CAPTCHA solution was entered.',
+        'no-remote-ip': u'CAPTCHA failed due to no visible IP address.',
+        'challenge-error': u'An error occurred with the CAPTCHA service - try '
+            'refreshing.',
+        'unknown-error': u'The CAPTCHA service returned the following error: '
+            '%(code)s.',
+    }
+
+    def __init__(self, private_key=None, *args, **kwargs):
+        """
+        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
+        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
+        if not challenge:
+            raise forms.ValidationError(self.error_messages['challenge-error'])
+        if not response:
+            raise forms.ValidationError(self.error_messages['required'])
+        try:
+            value = validate_recaptcha(self.remote_ip, challenge, response,
+                                       self.private_key)
+        except RecaptchaError, e:
+            if e.code == 'incorrect-captcha-sol':
+                raise forms.ValidationError(self.error_messages['invalid'])
+            raise forms.ValidationError(self.error_messages['unknown-error'] %
+                                        {'code': e.code})
+        return value
+

src/recaptchaworks/forms.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.
+#
+
+from django import forms
+
+from recaptchaworks.fields import RecaptchaField
+
+
+class BaseRecaptchaForm(forms.BaseForm):
+    def __init__(self, remote_ip, *args, **kwargs):
+        super(BaseRecaptchaForm, self).__init__(*args, **kwargs)
+        for field in self.fields.values():
+            if isinstance(field, RecaptchaField):
+                field.remote_ip = remote_ip
+
+
+class RecaptchaForm(BaseRecaptchaForm, forms.Form):
+    pass
+

src/recaptchaworks/models.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.
+#

src/recaptchaworks/settings.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.
+#
+
+from django.conf import settings
+
+
+RECAPTCHA_PRIVATE_KEY = getattr(settings, 'RECAPTCHA_PRIVATE_KEY', '')
+RECAPTCHA_PUBLIC_KEY = getattr(settings, 'RECAPTCHA_PUBLIC_KEY', '')
+RECAPTCHA_USE_SSL = getattr(settings, 'RECAPTCHA_USE_SSL', True)
+
+_RECAPTCHA_OPTIONS_SCRIPT_HTML = u'''<script type="text/javascript">
+   var RecaptchaOptions = %r;
+</script>
+'''
+RECAPTCHA_OPTIONS_SCRIPT_HTML = getattr(settings, 'RECAPTCHA_OPTIONS_SCRIPT_HTML', _RECAPTCHA_OPTIONS_SCRIPT_HTML)
+
+_RECAPTCHA_HTML = u'''%(options)s<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=%(public_key)s"></script>
+<noscript>
+   <iframe src="http://api.recaptcha.net/noscript?k=%(public_key)s"
+       height="300" width="500" frameborder="0"></iframe><br />
+   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
+   </textarea>
+   <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
+</noscript>'''
+RECAPTCHA_HTML = getattr(settings, 'RECAPTCHA_HTML', _RECAPTCHA_HTML)
+

src/recaptchaworks/tests.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.
+#
+
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}

src/recaptchaworks/utils.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.
+#
+
+import httplib
+from django.utils.http import urlencode
+
+from recaptchaworks.exceptions import RecaptchaError
+        
+
+def validate_recaptcha(remote_ip, challenge, response, private_key):
+    assert challenge, 'No challenge was provided for reCaptcha validation'
+    # Request validation from recaptcha.net
+    params = dict(privatekey=private_key, remoteip=remote_ip,
+                  challenge=challenge, response=response)
+    params = urlencode(params)
+    headers = {"Content-type": "application/x-www-form-urlencoded",
+               "Accept": "text/plain"}
+    conn = httplib.HTTPConnection("api-verify.recaptcha.net")
+    conn.request("POST", "/verify", params, headers)
+    response = conn.getresponse()
+    if response.status == 200:
+        data = response.read()
+    else:
+        data = ''
+    conn.close()
+    # Validate based on response data
+    result = data.startswith('true')
+    if not result:
+        bits = data.split('\n', 2)
+        error_code = ''
+        if len(bits) > 1:
+            error_code = bits[1]
+        raise RecaptchaError(error_code)
+    # Return dictionary
+    return result
+

src/recaptchaworks/views.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.
+#

src/recaptchaworks/widgets.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.
+#
+
+from django import forms
+from django.utils.safestring import mark_safe
+
+from recaptchaworks import settings
+
+
+class RecaptchaWidget(forms.Widget):
+    def __init__(self, theme=None, tabindex=None, public_key=None):
+        '''
+        From http://recaptcha.net/apidocs/captcha/client.html#look-n-feel:
+
+            theme:      'red' | 'white' | 'blackglass' | 'clean'
+    
+                Defines which theme to use for reCAPTCHA.
+    
+            tabindex:   any integer
+    
+                Sets a tabindex for the reCAPTCHA text box. If other elements
+                in the form use a tabindex, this should be set so that
+                navigation is easier for the user.
+            
+        The optional ``public_key`` argument can be used to override the
+        default use of the project-wide ``RECAPTCHA_PUBLIC_KEY`` setting.
+        '''
+        options = {}
+        if theme:
+            options['theme'] = theme
+        if tabindex:
+            options['tabindex'] = tabindex
+        self.options = options
+        self.public_key = public_key or settings.RECAPTCHA_PUBLIC_KEY
+        super(RecaptchaWidget, self).__init__()
+
+    def render(self, name, value, attrs=None):
+        args = dict(public_key=self.public_key, options='')
+        if self.options:
+            args['options'] = settings.RECAPTCHA_OPTIONS_SCRIPT_HTML % self.options
+        return mark_safe(settings.RECAPTCHA_HTML % args)
+
+    def value_from_datadict(self, data, files, name):
+        challenge = data.get('recaptcha_challenge_field')
+        response = data.get('recaptcha_response_field')
+        return (challenge, response)
+
+    def id_for_label(self, id_):
+        return None
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.