Private
Public Access
1
0

Merge branch 'feature/contactform' into develop

This commit is contained in:
2024-05-22 20:57:43 +02:00
5 changed files with 33 additions and 88 deletions

View File

@@ -18,6 +18,8 @@ from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.admin.widgets import AdminDateWidget from django.contrib.admin.widgets import AdminDateWidget
from django.forms.widgets import SelectDateWidget, HiddenInput 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.utils import timezone, translation
from django.forms import ModelForm, Select from django.forms import ModelForm, Select
@@ -235,11 +237,16 @@ class SearchForm(forms.Form):
# simple form for Contact page. Sends email to info@rowsandall.com # simple form for Contact page. Sends email to info@rowsandall.com
class EmailForm(forms.Form): class EmailForm(forms.Form):
firstname = forms.CharField(max_length=255) firstname = forms.CharField(max_length=255, label="First Name")
lastname = forms.CharField(max_length=255, required=False) lastname = forms.CharField(max_length=255, required=False, label="Last Name")
email = forms.EmailField() email = forms.EmailField()
subject = forms.CharField(max_length=255) subject = forms.CharField(max_length=255)
message = forms.CharField(widget=forms.Textarea()) message = forms.CharField(widget=forms.Textarea())
captcha = ReCaptchaField(widget=ReCaptchaV3(
attrs={
'required_score': 0.85,
}
))
disqualificationreasons = ( disqualificationreasons = (

View File

@@ -1,11 +1,11 @@
{% extends "newbase.html" %} {% extends "newbase.html" %}
{% block title %}Contact Us{% endblock title %} {% block title %}Contact Us{% endblock title %}
{% block main %} {% block main %}
<h1>Contact us through email</h1>
<ul class="main-content"> <ul class="main-content">
<li class="grid_2"> <li class="grid_2">
<h1>Contact us through email</h1>
{% if form.errors %} {% if form.errors %}
<p style="color: red;"> <p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below. Please correct the error{{ form.errors|pluralize }} below.
@@ -16,46 +16,11 @@
<form method="post" action="/rowers/email/send/" id="contactform">{% csrf_token %} <form method="post" action="/rowers/email/send/" id="contactform">{% csrf_token %}
<p> <p>
<table> <table>
<tr><td> {{ form.as_table }}
<label>Name <span class="required">*</span></label> </table>
<span class="span"> <input type="submit" name="submitform" value="Send Message" />
</td><td> </p>
<input name= "firstname" class="inputtext" maxlength="255" size="12" /> </form>
<label>First</label>
</td></tr>
<tr><td>
</span>
<span class="span">
</td><td>
<input name= "lastname" class="inputtext" maxlength="255" size="18" />
<label>Last</label>
</span>
</td></tr>
<tr><td>
<label>Email Address <span class="required">*</span></label>
</td><td>
<input name="email" class="inputtext" type="text" maxlength="255" size="35" />
</td></tr>
<tr><td>
<label>Subject <span class="required">*</span></label>
</td><td>
<input name="subject" class="inputtext" type="text" maxlength="255" size="45" />
</td></tr>
</table>
<table>
<input type="hidden" name="g-recaptcha-response" id='recaptcha'>
</td></tr>
<tr><td>
<label>Message <span class="required">*</span></label>
</td><td>
<textarea name="message" class="inputtextarea" rows="11" cols="45"></textarea>
</td></tr>
<tr><td>
<input type="submit" name="submitform" value="Send Message" />
</td></tr>
</table>
</p>
</form>
</li> </li>
<li class="grid_2"> <li class="grid_2">
@@ -113,24 +78,3 @@
{% include 'menu_help.html' %} {% include 'menu_help.html' %}
{% endblock %} {% endblock %}
{% block scripts %}
<script src="https://www.google.com/recaptcha/api.js?render=6LdRtMwUAAAAAGcKcFc28pGvmEb1wwDY27i0AX8B"></script>
<script>
// 3
grecaptcha.ready(function() {
// 4
$('#contactform').submit(function(e){
var form = this;
// 5
e.preventDefault()
grecaptcha.execute('6LdRtMwUAAAAAGcKcFc28pGvmEb1wwDY27i0AX8B', {action: 'contactform'}).then(function(token) {
// 6
$('#recaptcha').val(token)
// 7
form.submit()
});
})
});
</script>
{% endblock %}

View File

@@ -763,7 +763,8 @@ urlpatterns = [
re_path(r'^email/send/$', views.sendmail, name='sendmail'), re_path(r'^email/send/$', views.sendmail, name='sendmail'),
re_path(r'^email/thankyou/$', re_path(r'^email/thankyou/$',
TemplateView.as_view(template_name='thankyou.html'), name='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( re_path(r'^about', TemplateView.as_view(
template_name='about_us.html'), name='about'), template_name='about_us.html'), name='about'),
re_path(r'^brochure/$', TemplateView.as_view(template_name='brochure.html'), re_path(r'^brochure/$', TemplateView.as_view(template_name='brochure.html'),

View File

@@ -1271,23 +1271,10 @@ def add_defaultfavorites(r):
# Shows email form and sends it if submitted # Shows email form and sends it if submitted
def sendmail(request): def sendmail(request):
form = EmailForm()
if request.method == 'POST': 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) 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'] firstname = form.cleaned_data['firstname']
lastname = form.cleaned_data['lastname'] lastname = form.cleaned_data['lastname']
email = form.cleaned_data['email'] email = form.cleaned_data['email']
@@ -1314,14 +1301,15 @@ def sendmail(request):
request, "Something went wrong trying to send the form") request, "Something went wrong trying to send the form")
return HttpResponseRedirect('/rowers/email/thankyou/') return HttpResponseRedirect('/rowers/email/thankyou/')
else: else:
if not success: if "captcha" in form.errors:
messages.error(request, 'Bots are not welcome') messages.error(request,"Bots are not welcome")
else: # pragma: no cover return HttpResponseRedirect(reverse("sendmail"))
messages.error(
request, 'Something went wrong. Please try again')
return HttpResponseRedirect('/rowers/email/') return render(request,'email.html',
else: {
return HttpResponseRedirect('/rowers/email/') 'form': form
})
def keyvalue_get_default(key, options, def_options): # pragma: no cover def keyvalue_get_default(key, options, def_options): # pragma: no cover

View File

@@ -82,6 +82,7 @@ INSTALLED_APPS = [
'rules', 'rules',
'taggit', 'taggit',
'boatmovers', 'boatmovers',
'django_recaptcha',
] ]
AUTHENTICATION_BACKENDS = ( AUTHENTICATION_BACKENDS = (
@@ -611,9 +612,13 @@ except KeyError:
try: try:
RECAPTCHA_SITE_KEY = CFG['recaptcha_site_key'] RECAPTCHA_SITE_KEY = CFG['recaptcha_site_key']
RECAPTCHA_SITE_SECRET = CFG['recaptcha_site_secret'] 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 except KeyError: # pragma: no cover
RECAPTCHA_SITE_KEY = '' RECAPTCHA_SITE_KEY = ''
RECAPTCHA_SITE_SECRET = '' RECAPTCHA_SITE_SECRET = ''
RECAPTCHA_PUBLIC_KEY = ''
RECAPTCHA_PRIVATE_KEY = ''
GEOIP_PATH = STATIC_ROOT GEOIP_PATH = STATIC_ROOT