Skip to content

Commit

Permalink
[Fixes GeoNode#12815] Add custom login view/template to support recap…
Browse files Browse the repository at this point in the history
…tcha on Login Form (GeoNode#12825) (GeoNode#12831)

* Add custom login view/template to support recaptcha on Login Form
  • Loading branch information
cmotadev authored Jan 21, 2025
1 parent 1a6108b commit 12ebd8c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
13 changes: 0 additions & 13 deletions geonode/people/forms.py → geonode/people/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,10 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.utils.translation import gettext_lazy as _

try:
from captcha.fields import ReCaptchaField
except ImportError:
from django_recaptcha.fields import ReCaptchaField

# Ported in from django-registration
attrs_dict = {"class": "required"}


class AllauthReCaptchaSignupForm(forms.Form):
captcha = ReCaptchaField(label=False)

def signup(self, request, user):
"""Required, or else it thorws deprecation warnings"""
pass


class ProfileCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
Expand Down
39 changes: 39 additions & 0 deletions geonode/people/forms/recaptcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Copyright (C) 2019 Open Source Geospatial Foundation - all rights reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from django import forms
from allauth.account.forms import LoginForm

try:
from captcha.fields import ReCaptchaField
except ImportError:
from django_recaptcha.fields import ReCaptchaField


class AllauthReCaptchaSignupForm(forms.Form):
captcha = ReCaptchaField(label=False)

def signup(self, request, user):
"""Required, or else it thorws deprecation warnings"""
pass


class AllauthRecaptchaLoginForm(LoginForm):
captcha = ReCaptchaField(label=False)

def login(self, *args, **kwargs):
return super(AllauthRecaptchaLoginForm, self).login(*args, **kwargs)
14 changes: 14 additions & 0 deletions geonode/people/templates/people/account_login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "account/login.html" %}
{% comment %} Inherited from Django AllAuth default login form {% endcomment %}

{% block extra_script %}
<script type="text/javascript">
$( document ).ready(function() {
let recaptchaControl= $(".g-recaptcha" );

if (recaptchaControl.length ) {
recaptchaControl.removeClass('form-control');
}
});
</script>
{% endblock extra_script %}
6 changes: 5 additions & 1 deletion geonode/people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from allauth.account.views import SignupView
from allauth.account.views import SignupView, LoginView
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib import messages
Expand Down Expand Up @@ -57,6 +57,10 @@ def get_context_data(self, **kwargs):
return ret


class CustomLoginView(LoginView):
template_name = "people/account_login.html"


@login_required
def profile_edit(request, username=None):
if username is None:
Expand Down
5 changes: 4 additions & 1 deletion geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,8 +1399,11 @@
if "django_recaptcha" not in INSTALLED_APPS:
INSTALLED_APPS += ("django_recaptcha",)
ACCOUNT_SIGNUP_FORM_CLASS = os.getenv(
"ACCOUNT_SIGNUP_FORM_CLASS", "geonode.people.forms.AllauthReCaptchaSignupForm"
"ACCOUNT_SIGNUP_FORM_CLASS", "geonode.people.forms.recaptcha.AllauthReCaptchaSignupForm"
)

# https://docs.allauth.org/en/dev/account/configuration.html
ACCOUNT_FORMS = dict(login="geonode.people.forms.recaptcha.AllauthRecaptchaLoginForm")
"""
In order to generate reCaptcha keys, please see:
- https://pypi.org/project/django-recaptcha/#installation
Expand Down
3 changes: 2 additions & 1 deletion geonode/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from geonode.utils import check_ogc_backend
from geonode.base import register_url_event
from geonode.messaging.urls import urlpatterns as msg_urls
from .people.views import CustomSignupView
from .people.views import CustomSignupView, CustomLoginView
from oauth2_provider.urls import app_name as oauth2_app_name, base_urlpatterns, oidc_urlpatterns

admin.autodiscover()
Expand Down Expand Up @@ -93,6 +93,7 @@
re_path(r"^h_keywords_api$", views.h_keywords, name="h_keywords_api"),
# Social views
re_path(r"^account/signup/", CustomSignupView.as_view(), name="account_signup"),
re_path(r"^account/login/", CustomLoginView.as_view(), name="account_login"),
re_path(r"^account/", include("allauth.urls")),
re_path(r"^invitations/", include("geonode.invitations.urls", namespace="geonode.invitations")),
re_path(r"^people/", include("geonode.people.urls")),
Expand Down

0 comments on commit 12ebd8c

Please sign in to comment.