Skip to content

Commit 54ad20c

Browse files
committed
add recaptcha in registration form
1 parent 10d6b28 commit 54ad20c

7 files changed

Lines changed: 74 additions & 3 deletions

File tree

users/default_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@
5656
# http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html
5757
# for details.
5858
SQLALCHEMY_DATABASE_URI = "postgresql://scott:tiger@localhost:5432/mydatabase"
59+
60+
RECAPTCHA_PUBLIC_KEY = ""
61+
RECAPTCHA_PRIVATE_KEY = ""

users/static/css/user-map.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ html, body, #map {
6161
line-height: 18px;
6262
color: #555;
6363
}
64+
65+
#recaptcha_image img {
66+
width: 280px;
67+
}

users/static/js/user-map-component.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ function onMapClick(e) {
213213
};
214214
var popup = getUserFormPopup(user, ADD_USER_MODE);
215215
marker_new_user.bindPopup(popup).openPopup()
216+
217+
// activate captcha
218+
var captcha_element = document.getElementById("recaptcha-container");
219+
if (captcha_element !== null) {
220+
showCaptcha(captcha_element);
221+
}
216222
}
217223

218224
/**

users/static/js/user-map.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ function addUser() {
9696

9797
var twitter = $("#twitter").val();
9898

99+
var recaptcha_response_field = $("#recaptcha_response_field").val();
100+
var recaptcha_challenge_field = $("#recaptcha_challenge_field").val();
101+
99102
var is_client_side_valid = validate_user_form(name, email, website);
100103
if (is_client_side_valid) {
101104
$.ajax({
@@ -108,18 +111,28 @@ function addUser() {
108111
email_updates: email_updates,
109112
latitude: latitude,
110113
longitude: longitude,
111-
twitter: twitter
114+
twitter: twitter,
115+
recaptcha_response_field: recaptcha_response_field,
116+
recaptcha_challenge_field: recaptcha_challenge_field
112117
},
113118
success: function (response) {
114119
if (response.type.toString() == 'Error') {
115120
if (typeof response.name != 'undefined') {
116121
$name_input.parent().addClass('has-error');
117-
$name_input.attr('placeholder', response.name.toString());
122+
var $name_err = $("#name-error");
123+
$name_err.text(response.name.toString());
118124

119125
}
120126
if (typeof response.email != 'undefined') {
121127
$email_input.parent().addClass('has-error');
122-
$email_input.attr('placeholder', response.email.toString());
128+
var $email_err = $("#email-error");
129+
$email_err.text(response.email.toString());
130+
}
131+
if (typeof response.recaptcha_response_field != 'undefined') {
132+
var $captha_input = $("#recaptcha_response_field");
133+
$captha_input.parent().addClass('has-error');
134+
var $captcha_err = $("#captcha-error");
135+
$captcha_err.text(response.recaptcha_response_field.toString());
123136
}
124137
} else {
125138
//Clear marker

users/templates/html/base.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@
2525
<script language="javascript" type="text/javascript" src="{{ url_for('static', filename='js/user-map-state.js') }}"></script>
2626
<script language="javascript" type="text/javascript" src="{{ url_for('static', filename='js/user-map-utilities.js') }}"></script>
2727
<script language="javascript" type="text/javascript" src="{{ url_for('static', filename='js/validate.js') }}"></script>
28+
<script type="text/javascript" src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
29+
<script>
30+
function showCaptcha(element) {
31+
Recaptcha.create(
32+
"{{ config['RECAPTCHA_PUBLIC_KEY'] }}",
33+
element,
34+
{
35+
theme: "custom",
36+
custom_theme_widget: "recaptcha-widget"
37+
}
38+
);
39+
}
40+
</script>
2841
{% endblock head_resources %}
2942
</head>
3043
<body>

users/templates/html/user_form.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ <h3 class="panel-title">User Data</h3>
6060
<input style="display: none;" type="text" id="lng" name="lng"
6161
value=""/>
6262
</div>
63+
<div class="form-group">
64+
<div id="recaptcha-container">
65+
<div id="recaptcha-widget">
66+
<div id="recaptcha_image"></div>
67+
<div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
68+
69+
<span class="recaptcha_only_if_image">Enter the words above:</span>
70+
<span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>
71+
72+
<input class="form-control" type="text" id="recaptcha_response_field" name="recaptcha_response_field" placeholder="Required" />
73+
<span class="help-inline" id="captcha-error"></span>
74+
75+
<div><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div>
76+
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
77+
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>
78+
79+
<div><a href="javascript:Recaptcha.showhelp()">Help</a></div>
80+
</div>
81+
</div>
82+
</div>
6383

6484
<div class="form-group">
6585
<div>

users/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from flask import render_template, Response, request, current_app
99
from werkzeug.exceptions import default_exceptions
10+
from recaptcha.client import captcha
1011

1112
# App declared directly in __init__ as per
1213
# http://flask.pocoo.org/docs/patterns/packages/#larger-applications
@@ -123,6 +124,17 @@ def add_user_view():
123124
if user is not None:
124125
message['email'] = 'Email has been registered by other user.'
125126

127+
if not current_app.testing:
128+
captcha_resp = captcha.submit(
129+
request.form.get("recaptcha_challenge_field", ""),
130+
request.form.get("recaptcha_response_field", ""),
131+
current_app.config["RECAPTCHA_PRIVATE_KEY"],
132+
request.remote_addr,
133+
)
134+
135+
if not captcha_resp.is_valid:
136+
message["recaptcha_response_field"] = "Captcha is not valid"
137+
126138
# Process data
127139
if len(message) != 0:
128140
message['type'] = 'Error'

0 commit comments

Comments
 (0)