Private
Public Access
1
0

improved contact form

This commit is contained in:
Sander Roosendaal
2020-01-06 22:35:58 +01:00
parent 028ddfcfe7
commit 4c1f717be5
5 changed files with 145 additions and 115 deletions

View File

@@ -1073,28 +1073,39 @@ def add_defaultfavorites(r):
f.save()
return 1
# Shows email form and sends it if submitted
def sendmail(request):
if request.method == 'POST':
# test recaptcha
response_string = request.POST.get('g-recaptcha-response')
# replace below with settings
recaptcha_secret = '6LdRtMwUAAAAABc3piLDlI5VNDkOtEMIOckNi9tm'
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():
if form.is_valid() and success:
firstname = form.cleaned_data['firstname']
lastname = form.cleaned_data['lastname']
email = form.cleaned_data['email']
subject = form.cleaned_data['subject']
botcheck = form.cleaned_data['botcheck'].lower()
subject = 'Rowsandall Contact Form:'+form.cleaned_data['subject']
message = form.cleaned_data['message']
if botcheck == 'yes':
try:
fullemail = firstname + " " + lastname + " " + "<" + email + ">"
send_mail(subject, message, fullemail, ['info@rowsandall.com'])
return HttpResponseRedirect('/rowers/email/thankyou/')
except:
return HttpResponseRedirect('/rowers/email/')
else:
messages.error(request,'You have to answer YES to the question')
return HttpResponseRedirect('/rowers/email/')
fullemail = firstname + " " + lastname + " " + "<" + email + ">"
send_mail(subject, message, fullemail, ['info@rowsandall.com'])
return HttpResponseRedirect('/rowers/email/thankyou/')
else:
if not success:
messages.error(request,'Bots are not welcome')
else:
messages.error(request,'Something went wrong. Please try again')
return HttpResponseRedirect('/rowers/email/')
else:
return HttpResponseRedirect('/rowers/email/')