...
 
Commits (3)
......@@ -25,29 +25,18 @@
"""Google reCaptcha submit helper"""
import logging
import requests
from recaptcha.client import captcha
log = logging.getLogger(__name__)
def submit(request, params):
if not params['recaptcha_challenge_field']:
# An empty recaptcha_challenge_field means there is a problem with Google reCaptcha service
# don't return an error
return captcha.RecaptchaResponse(is_valid=True)
if not params['recaptcha_response_field']:
return captcha.RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')
response = captcha.submit(
params['recaptcha_challenge_field'],
params['recaptcha_response_field'],
request.registry.settings['recaptcha.private_key'],
request.remote_addr)
if not response.is_valid and \
response.error_code in ['invalid-site-private-key', 'invalid-request-cookie', 'recaptcha-not-reachable']:
log.debug('reCaptcha Google service error: {}'.format(response.error_code))
return captcha.RecaptchaResponse(is_valid=True)
return response
captcha_response = params['g_recaptcha_response']
url = "https://www.google.com/recaptcha/api/siteverify"
params = {
'secret': request.registry.settings['recaptcha.private_key'],
'response': captcha_response,
}
verify_captcha = requests.get(url, params=params, verify=True)
return verify_captcha.json()
......@@ -104,5 +104,4 @@ class EmailForm(formencode.Schema):
email_from = validators.Email(not_empty=True)
subject = validators.String(not_empty=True, strip=True)
body = validators.String(not_empty=True, strip=True)
recaptcha_challenge_field = validators.String()
recaptcha_response_field = validators.String()
g_recaptcha_response = validators.String()
......@@ -54,6 +54,7 @@ function initEmailForm(options) {
url: options.url
}).done(function(data, textStatus, jqXHR) {
if (data.errors) {
alert('Captcha error : Your email has not been sent. Please accept our apologies.');
$(options.formSelector).find('#captcha-error').hide();
if (data.errors.captcha) {
Recaptcha.reload();
......
......@@ -12,6 +12,11 @@ var RecaptchaOptions = {
};
</script>
<script src="${request.static_path('passim:static/js/contact.js', _query=dict(version=app_version))}"></script>
<script>
function convertRecaptchaFieldName(data) {
$('#form_contact').append('<input type="hidden" name="g_recaptcha_response" value="' + data[0] + '"/>')
}
</script>
</%def>
${parent.page_header()}
......@@ -32,7 +37,7 @@ ${content | n}
<div class="fade modal" id="contact-modal" tabindex="-1" role="dialog" aria-labelledby="contact-modal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form name="email" role="form">
<form id="form_contact" name="email" role="form">
<fieldset>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
......@@ -58,9 +63,8 @@ ${content | n}
<span class="help-block"></span>
</div>
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=${request.registry.settings['recaptcha.public_key']}">
</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-callback="convertRecaptchaFieldName" data-sitekey="${request.registry.settings['recaptcha.public_key']}"></div>
<span class="text-danger" id="captcha-error" style="display:none">${_('Incorrect please try again')}</span>
</div>
......
......@@ -6,6 +6,11 @@ var RecaptchaOptions = {
};
</script>
<script src="${request.static_path('passim:static/js/contribute.js', _query=dict(version=app_version))}"></script>
<script>
function convertRecaptchaFieldName(data) {
$('#form_contribute').append('<input type="hidden" name="g_recaptcha_response" value="' + data[0] + '"/>')
}
</script>
<%page args="subject, body"/>
% if success_message:
<div class="alert alert-success">
......@@ -18,7 +23,7 @@ var RecaptchaOptions = {
<div class="fade modal" id="contribute-modal" tabindex="-1" role="dialog" aria-labelledby="contribute-modal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form name="email" role="form">
<form id="form_contribute" name="email" role="form">
<fieldset>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
......@@ -44,9 +49,8 @@ var RecaptchaOptions = {
<span class="help-block"></span>
</div>
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=${request.registry.settings['recaptcha.public_key']}">
</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-callback="convertRecaptchaFieldName" data-sitekey="${request.registry.settings['recaptcha.public_key']}"></div>
<span class="text-danger" id="captcha-error" style="display:none">${_('Incorrect please try again')}</span>
</div>
......
......@@ -30,11 +30,11 @@ $(function () {
// Configuration du support de la carte du zoom maximal et minimal
var osmUrl="http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png";
var osmUrl="http://tile-{s}.easter-eggs.com/{z}/{x}/{y}.png";
var osmAttrib=[
'Données cartographiques CC-By-SA ',
'<a href="http://openstreetmap.org/" target="_blank">OpenStreetMap<\/a>, ',
'Tuiles fournies par <a href="http://www.opencyclemap.org/" target="_blank">Andy Allan<\/a>'
'Tuiles fournies par <a href="https://www.easter-eggs.com/" target="_blank">Easter-Eggs<\/a>'
].join('');
var osmLayer = new L.TileLayer(osmUrl, {minZoom: 2, maxZoom: 16, attribution: osmAttrib});
......
......@@ -31,11 +31,11 @@ $(function () {
// Configuration du support de la carte du zoom maximal et minimal
var osmUrl="http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png";
var osmUrl="http://tile-{s}.easter-eggs.com/{z}/{x}/{y}.png";
var osmAttrib=[
'Données cartographiques CC-By-SA ',
'<a href="http://openstreetmap.org/" target="_blank">OpenStreetMap<\/a>, ',
'Tuiles fournies par <a href="http://www.opencyclemap.org/" target="_blank">Andy Allan<\/a>'
'Tuiles fournies par <a href="https://www.easter-eggs.com/" target="_blank">Easter-Eggs<\/a>'
].join('');
var osmLayer = new L.TileLayer(osmUrl, {minZoom: 2, maxZoom: 16, attribution: osmAttrib});
......
......@@ -30,11 +30,11 @@ $(function () {
// Configuration du support de la carte du zoom maximal et minimal
var osmUrl="http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png";
var osmUrl="http://tile-{s}.easter-eggs.com/{z}/{x}/{y}.png";
var osmAttrib=[
'Données cartographiques CC-By-SA ',
'<a href="http://openstreetmap.org/" target="_blank">OpenStreetMap<\/a>, ',
'Tuiles fournies par <a href="http://www.opencyclemap.org/" target="_blank">Andy Allan<\/a>'
'Tuiles fournies par <a href="https://www.easter-eggs.com/" target="_blank">Easter-Eggs<\/a>'
].join('');
var osmLayer = new L.TileLayer(osmUrl, {minZoom: 2, maxZoom: 16, attribution: osmAttrib});
......
......@@ -31,11 +31,11 @@ $(function () {
// Configuration du support de la carte du zoom maximal et minimal
var osmUrl="http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png";
var osmUrl="http://tile-{s}.easter-eggs.com/{z}/{x}/{y}.png";
var osmAttrib=[
'Données cartographiques CC-By-SA ',
'<a href="http://openstreetmap.org/" target="_blank">OpenStreetMap<\/a>, ',
'Tuiles fournies par <a href="http://www.opencyclemap.org/" target="_blank">Andy Allan<\/a>'
'Tuiles fournies par <a href="https://www.easter-eggs.com/" target="_blank">Easter-Eggs<\/a>'
].join('');
var osmLayer = new L.TileLayer(osmUrl, {minZoom: 2, maxZoom: 16, attribution: osmAttrib});
......
......@@ -60,8 +60,8 @@ def email(request, template):
if not form.errors:
recaptcha_response = captcha.submit(request, form.result)
if not recaptcha_response.is_valid:
form.errors['captcha'] = recaptcha_response.error_code
if recaptcha_response['success'] is not True:
form.errors['g_recaptcha_response'] = recaptcha_response.get('error-codes')
if form.errors:
return dict(errors=form.errors)
......
......@@ -70,7 +70,7 @@ def build_table(request, data_types, transport_types):
transport_offer_count = len(transport_offer_pois)
line.append(
[
"{0}%".format(str(round(float(data_type_count) * 100. / float(transport_offer_count), 2))),
"{0}%".format(str(round(float(data_type_count) * 100. / float(transport_offer_count), 2))) if transport_offer_count != 0 else '--',
request.route_url(
'api.opendata.export', _query={
'data_types':data_type,
......@@ -97,7 +97,7 @@ def build_table(request, data_types, transport_types):
).distinct('_id')
transport_offer_count = len(transport_offer_pois)
last_line.append([
"{0}%".format(str(round(float(data_type_count) * 100. / float(transport_offer_count), 2))),
"{0}%".format(str(round(float(data_type_count) * 100. / float(transport_offer_count), 2))) if transport_offer_count != 0 else '--',
request.route_url(
'api.opendata.export', _query={
'data_types':data_type,
......