Skip to content

Commit

Permalink
Merge branch 'release/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaidoussi committed Apr 23, 2014
2 parents a3d98c8 + 8f4c4fc commit ee9c994
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 45 deletions.
4 changes: 4 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ Other
-----
* Brooks Travis
* `Denis Mishchishin <https://github.com/denz>`_
* `Joshua Peper <https://github.com/zout>`_
* `Rodrigo Primo <https://github.com/rodrigoprimo>`_
* `snnwolf <https://github.com/snnwolf>`_
* `Adriano Orioli <https://github.com/Aorioli>`_

7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========


1.0 (2014-04-23)
----------------

#. Added Python 3 support
#. Added French, Dutch and Brazilian Portuguese translations

0.0.9 (2014-02-14)
------------------
#. Bugfix: release master and not develop. This should fix the confusion due to master having been the default branch on Github.
Expand Down
17 changes: 17 additions & 0 deletions captcha/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

PY2 = sys.version_info[0] == 2
if PY2:
text_type = unicode
from urllib2 import Request, urlopen
from urllib import urlencode
else:
from urllib.request import Request, urlopen
from urllib.parse import urlencode
text_type = str


def want_bytes(s, encoding='utf-8', errors='strict'):
if isinstance(s, text_type):
s = s.encode(encoding, errors)
return s
87 changes: 46 additions & 41 deletions captcha/client.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import urllib
import urllib2
import django
if django.VERSION[1] >= 5:
import json
else:
from django.utils import simplejson as json

from django.conf import settings
from django.template.loader import render_to_string
from django.utils import simplejson as json
from django.utils.safestring import mark_safe
from django.utils.translation import get_language

from captcha._compat import want_bytes, urlencode, Request, urlopen, PY2

DEFAULT_API_SSL_SERVER = "https://www.google.com/recaptcha/api"
DEFAULT_API_SERVER = "http://www.google.com/recaptcha/api"
DEFAULT_VERIFY_SERVER = "www.google.com"
DEFAULT_WIDGET_TEMPLATE = 'captcha/widget.html'
DEFAULT_WIDGET_TEMPLATE_AJAX = 'captcha/widget_ajax.html'

API_SSL_SERVER = getattr(settings, "CAPTCHA_API_SSL_SERVER", \
DEFAULT_API_SSL_SERVER)
API_SSL_SERVER = getattr(settings, "CAPTCHA_API_SSL_SERVER",
DEFAULT_API_SSL_SERVER)
API_SERVER = getattr(settings, "CAPTCHA_API_SERVER", DEFAULT_API_SERVER)
VERIFY_SERVER = getattr(settings, "CAPTCHA_VERIFY_SERVER", \
DEFAULT_VERIFY_SERVER)
VERIFY_SERVER = getattr(settings, "CAPTCHA_VERIFY_SERVER",
DEFAULT_VERIFY_SERVER)

if getattr(settings, "CAPTCHA_AJAX", False):
WIDGET_TEMPLATE = getattr(settings, "CAPTCHA_WIDGET_TEMPLATE", \
DEFAULT_WIDGET_TEMPLATE_AJAX)
WIDGET_TEMPLATE = getattr(settings, "CAPTCHA_WIDGET_TEMPLATE",
DEFAULT_WIDGET_TEMPLATE_AJAX)
else:
WIDGET_TEMPLATE = getattr(settings, "CAPTCHA_WIDGET_TEMPLATE", \
DEFAULT_WIDGET_TEMPLATE)
WIDGET_TEMPLATE = getattr(settings, "CAPTCHA_WIDGET_TEMPLATE",
DEFAULT_WIDGET_TEMPLATE)


RECAPTCHA_SUPPORTED_LANUAGES = ('en', 'nl', 'fr', 'de', 'pt', 'ru', 'es', 'tr')
Expand All @@ -37,9 +41,9 @@ def __init__(self, is_valid, error_code=None):


def displayhtml(public_key,
attrs,
use_ssl=False,
error=None):
attrs,
use_ssl=False,
error=None):
"""Gets the HTML to display for reCAPTCHA
public_key -- The public api key
Expand All @@ -58,20 +62,21 @@ def displayhtml(public_key,
if not 'lang' in attrs:
attrs['lang'] = get_language()[:2]

return render_to_string(WIDGET_TEMPLATE,
{'api_server': server,
'public_key': public_key,
'error_param': error_param,
'lang': attrs['lang'],
'options': mark_safe(json.dumps(attrs, indent=2))
})
return render_to_string(
WIDGET_TEMPLATE,
{'api_server': server,
'public_key': public_key,
'error_param': error_param,
'lang': attrs['lang'],
'options': mark_safe(json.dumps(attrs, indent=2))
})


def submit(recaptcha_challenge_field,
recaptcha_response_field,
private_key,
remoteip,
use_ssl=False):
recaptcha_response_field,
private_key,
remoteip,
use_ssl=False):
"""
Submits a reCAPTCHA request for verification. Returns RecaptchaResponse
for the request
Expand All @@ -91,38 +96,38 @@ def submit(recaptcha_challenge_field,
error_code='incorrect-captcha-sol'
)

def encode_if_necessary(s):
if isinstance(s, unicode):
return s.encode('utf-8')
return s
params = urlencode({
'privatekey': want_bytes(private_key),
'remoteip': want_bytes(remoteip),
'challenge': want_bytes(recaptcha_challenge_field),
'response': want_bytes(recaptcha_response_field),
})

params = urllib.urlencode({
'privatekey': encode_if_necessary(private_key),
'remoteip': encode_if_necessary(remoteip),
'challenge': encode_if_necessary(recaptcha_challenge_field),
'response': encode_if_necessary(recaptcha_response_field),
})
if not PY2:
params = params.encode('utf-8')

if use_ssl:
verify_url = 'https://%s/recaptcha/api/verify' % VERIFY_SERVER
else:
verify_url = 'http://%s/recaptcha/api/verify' % VERIFY_SERVER

request = urllib2.Request(
req = Request(
url=verify_url,
data=params,
headers={
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "reCAPTCHA Python"
}
)
'Content-type': 'application/x-www-form-urlencoded',
'User-agent': 'reCAPTCHA Python'
}
)

httpresp = urllib2.urlopen(request)
httpresp = urlopen(req)

return_values = httpresp.read().splitlines()
httpresp.close()

return_code = return_values[0]
if not PY2:
return_code = return_code.decode('utf-8')

if (return_code == "true"):
return RecaptchaResponse(is_valid=True)
Expand Down
8 changes: 6 additions & 2 deletions captcha/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

from django import forms
from django.conf import settings
from django.utils.encoding import smart_unicode
try:
from django.utils.encoding import smart_unicode
except ImportError:
from django.utils.encoding import smart_text as smart_unicode

from django.utils.translation import ugettext_lazy as _

from captcha import client
Expand All @@ -12,7 +16,7 @@

class ReCaptchaField(forms.CharField):
default_error_messages = {
'captcha_invalid': _(u'Incorrect, please try again.')
'captcha_invalid': _('Incorrect, please try again.')
}

def __init__(self, public_key=None, private_key=None, use_ssl=None, \
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='django-recaptcha',
version='0.0.9',
version='1.0',
description='Django recaptcha form field/widget app.',
long_description=open('README.rst', 'r').read() + open('AUTHORS.rst', 'r').read() + open('CHANGELOG.rst', 'r').read(),
author='Praekelt Foundation',
Expand All @@ -18,7 +18,7 @@
classifiers=[
"Programming Language :: Python",
"License :: OSI Approved :: BSD License",
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Operating System :: OS Independent",
"Framework :: Django",
"Intended Audience :: Developers",
Expand Down

0 comments on commit ee9c994

Please sign in to comment.