Skip to content

Commit 12ebd8c

Browse files
authored
[Fixes #12815] Add custom login view/template to support recaptcha on Login Form (#12825) (#12831)
* Add custom login view/template to support recaptcha on Login Form
1 parent 1a6108b commit 12ebd8c

File tree

6 files changed

+64
-16
lines changed

6 files changed

+64
-16
lines changed
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,10 @@
2424
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
2525
from django.utils.translation import gettext_lazy as _
2626

27-
try:
28-
from captcha.fields import ReCaptchaField
29-
except ImportError:
30-
from django_recaptcha.fields import ReCaptchaField
31-
3227
# Ported in from django-registration
3328
attrs_dict = {"class": "required"}
3429

3530

36-
class AllauthReCaptchaSignupForm(forms.Form):
37-
captcha = ReCaptchaField(label=False)
38-
39-
def signup(self, request, user):
40-
"""Required, or else it thorws deprecation warnings"""
41-
pass
42-
43-
4431
class ProfileCreationForm(UserCreationForm):
4532
class Meta:
4633
model = get_user_model()

geonode/people/forms/recaptcha.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Copyright (C) 2019 Open Source Geospatial Foundation - all rights reserved
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
#
17+
#########################################################################
18+
from django import forms
19+
from allauth.account.forms import LoginForm
20+
21+
try:
22+
from captcha.fields import ReCaptchaField
23+
except ImportError:
24+
from django_recaptcha.fields import ReCaptchaField
25+
26+
27+
class AllauthReCaptchaSignupForm(forms.Form):
28+
captcha = ReCaptchaField(label=False)
29+
30+
def signup(self, request, user):
31+
"""Required, or else it thorws deprecation warnings"""
32+
pass
33+
34+
35+
class AllauthRecaptchaLoginForm(LoginForm):
36+
captcha = ReCaptchaField(label=False)
37+
38+
def login(self, *args, **kwargs):
39+
return super(AllauthRecaptchaLoginForm, self).login(*args, **kwargs)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "account/login.html" %}
2+
{% comment %} Inherited from Django AllAuth default login form {% endcomment %}
3+
4+
{% block extra_script %}
5+
<script type="text/javascript">
6+
$( document ).ready(function() {
7+
let recaptchaControl= $(".g-recaptcha" );
8+
9+
if (recaptchaControl.length ) {
10+
recaptchaControl.removeClass('form-control');
11+
}
12+
});
13+
</script>
14+
{% endblock extra_script %}

geonode/people/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
#
1818
#########################################################################
19-
from allauth.account.views import SignupView
19+
from allauth.account.views import SignupView, LoginView
2020
from django.contrib.auth import get_user_model
2121
from django.contrib.auth.decorators import login_required
2222
from django.contrib import messages
@@ -57,6 +57,10 @@ def get_context_data(self, **kwargs):
5757
return ret
5858

5959

60+
class CustomLoginView(LoginView):
61+
template_name = "people/account_login.html"
62+
63+
6064
@login_required
6165
def profile_edit(request, username=None):
6266
if username is None:

geonode/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,8 +1399,11 @@
13991399
if "django_recaptcha" not in INSTALLED_APPS:
14001400
INSTALLED_APPS += ("django_recaptcha",)
14011401
ACCOUNT_SIGNUP_FORM_CLASS = os.getenv(
1402-
"ACCOUNT_SIGNUP_FORM_CLASS", "geonode.people.forms.AllauthReCaptchaSignupForm"
1402+
"ACCOUNT_SIGNUP_FORM_CLASS", "geonode.people.forms.recaptcha.AllauthReCaptchaSignupForm"
14031403
)
1404+
1405+
# https://docs.allauth.org/en/dev/account/configuration.html
1406+
ACCOUNT_FORMS = dict(login="geonode.people.forms.recaptcha.AllauthRecaptchaLoginForm")
14041407
"""
14051408
In order to generate reCaptcha keys, please see:
14061409
- https://pypi.org/project/django-recaptcha/#installation

geonode/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from geonode.utils import check_ogc_backend
4444
from geonode.base import register_url_event
4545
from geonode.messaging.urls import urlpatterns as msg_urls
46-
from .people.views import CustomSignupView
46+
from .people.views import CustomSignupView, CustomLoginView
4747
from oauth2_provider.urls import app_name as oauth2_app_name, base_urlpatterns, oidc_urlpatterns
4848

4949
admin.autodiscover()
@@ -93,6 +93,7 @@
9393
re_path(r"^h_keywords_api$", views.h_keywords, name="h_keywords_api"),
9494
# Social views
9595
re_path(r"^account/signup/", CustomSignupView.as_view(), name="account_signup"),
96+
re_path(r"^account/login/", CustomLoginView.as_view(), name="account_login"),
9697
re_path(r"^account/", include("allauth.urls")),
9798
re_path(r"^invitations/", include("geonode.invitations.urls", namespace="geonode.invitations")),
9899
re_path(r"^people/", include("geonode.people.urls")),

0 commit comments

Comments
 (0)