From ce978bfe8eefc2e0836c2e21079c762732c97dcb Mon Sep 17 00:00:00 2001
From: Sander Roosendaal
Date: Wed, 22 May 2024 20:47:20 +0200
Subject: [PATCH] new form with recaptcha
---
rowers/forms.py | 7 ++--
rowers/templates/email.html | 66 +++----------------------------------
rowers/urls.py | 3 +-
rowers/views/statements.py | 32 +++++-------------
rowsandall_app/settings.py | 5 +++
5 files changed, 25 insertions(+), 88 deletions(-)
diff --git a/rowers/forms.py b/rowers/forms.py
index 53770e44..3cec1361 100644
--- a/rowers/forms.py
+++ b/rowers/forms.py
@@ -18,6 +18,8 @@ from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.widgets import SelectDateWidget, HiddenInput
+from django_recaptcha.fields import ReCaptchaField
+from django_recaptcha.widgets import ReCaptchaV3
from django.utils import timezone, translation
from django.forms import ModelForm, Select
@@ -235,11 +237,12 @@ class SearchForm(forms.Form):
# simple form for Contact page. Sends email to info@rowsandall.com
class EmailForm(forms.Form):
- firstname = forms.CharField(max_length=255)
- lastname = forms.CharField(max_length=255, required=False)
+ firstname = forms.CharField(max_length=255, label="First Name")
+ lastname = forms.CharField(max_length=255, required=False, label="Last Name")
email = forms.EmailField()
subject = forms.CharField(max_length=255)
message = forms.CharField(widget=forms.Textarea())
+ captcha = ReCaptchaField(widget=ReCaptchaV3)
disqualificationreasons = (
diff --git a/rowers/templates/email.html b/rowers/templates/email.html
index dc31ae20..fc8bf3f1 100644
--- a/rowers/templates/email.html
+++ b/rowers/templates/email.html
@@ -16,46 +16,11 @@
-
+ {{ form.as_table }}
+
+
+
+
@@ -113,24 +78,3 @@
{% include 'menu_help.html' %}
{% endblock %}
-{% block scripts %}
-
-
-{% endblock %}
diff --git a/rowers/urls.py b/rowers/urls.py
index c1190705..a17520b9 100644
--- a/rowers/urls.py
+++ b/rowers/urls.py
@@ -763,7 +763,8 @@ urlpatterns = [
re_path(r'^email/send/$', views.sendmail, name='sendmail'),
re_path(r'^email/thankyou/$',
TemplateView.as_view(template_name='thankyou.html'), name='thankyou'),
- re_path(r'^email/$', TemplateView.as_view(template_name='email.html'), name='email'),
+ re_path(r'^email/$', views.sendmail, name='sendmail'),
+# TemplateView.as_view(template_name='email.html'), name='email'),
re_path(r'^about', TemplateView.as_view(
template_name='about_us.html'), name='about'),
re_path(r'^brochure/$', TemplateView.as_view(template_name='brochure.html'),
diff --git a/rowers/views/statements.py b/rowers/views/statements.py
index 351ca027..997a5646 100644
--- a/rowers/views/statements.py
+++ b/rowers/views/statements.py
@@ -1271,23 +1271,10 @@ def add_defaultfavorites(r):
# Shows email form and sends it if submitted
def sendmail(request):
+ form = EmailForm()
if request.method == 'POST':
- # test recaptcha
- response_string = request.POST.get('g-recaptcha-response')
- # replace below with settings
- recaptcha_secret = RECAPTCHA_SITE_SECRET
- url = 'https://www.google.com/recaptcha/api/siteverify'
- data = {
- 'secret': recaptcha_secret,
- 'response': response_string,
- }
- response = requests.post(url, data=data, verify=True)
- success = False
- if response.status_code == 200:
- success = response.json().get('success')
-
form = EmailForm(request.POST)
- if form.is_valid() and success: # pragma: no cover
+ if form.is_valid(): # pragma: no cover
firstname = form.cleaned_data['firstname']
lastname = form.cleaned_data['lastname']
email = form.cleaned_data['email']
@@ -1313,15 +1300,12 @@ def sendmail(request):
messages.error(
request, "Something went wrong trying to send the form")
return HttpResponseRedirect('/rowers/email/thankyou/')
- else:
- if not success:
- messages.error(request, 'Bots are not welcome')
- else: # pragma: no cover
- messages.error(
- request, 'Something went wrong. Please try again')
- return HttpResponseRedirect('/rowers/email/')
- else:
- return HttpResponseRedirect('/rowers/email/')
+
+
+ return render(request,'email.html',
+ {
+ 'form': form
+ })
def keyvalue_get_default(key, options, def_options): # pragma: no cover
diff --git a/rowsandall_app/settings.py b/rowsandall_app/settings.py
index 684c41ec..757c540e 100644
--- a/rowsandall_app/settings.py
+++ b/rowsandall_app/settings.py
@@ -82,6 +82,7 @@ INSTALLED_APPS = [
'rules',
'taggit',
'boatmovers',
+ 'django_recaptcha',
]
AUTHENTICATION_BACKENDS = (
@@ -611,9 +612,13 @@ except KeyError:
try:
RECAPTCHA_SITE_KEY = CFG['recaptcha_site_key']
RECAPTCHA_SITE_SECRET = CFG['recaptcha_site_secret']
+ RECAPTCHA_PUBLIC_KEY = CFG['recaptcha_site_key']
+ RECAPTCHA_PRIVATE_KEY = CFG['recaptcha_site_secret']
except KeyError: # pragma: no cover
RECAPTCHA_SITE_KEY = ''
RECAPTCHA_SITE_SECRET = ''
+ RECAPTCHA_PUBLIC_KEY = ''
+ RECAPTCHA_PRIVATE_KEY = ''
GEOIP_PATH = STATIC_ROOT