failing the captcha no longer returns a 500

This commit is contained in:
Alie 2023-07-14 20:29:07 +02:00
parent 8631a0259e
commit 3eb3dfca48
2 changed files with 52 additions and 37 deletions

View File

@ -1,3 +1,4 @@
from re import Pattern
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as txt from django.utils.translation import gettext_lazy as txt
@ -11,5 +12,5 @@ class SignbookForm(forms.Form):
def validate_capcha(self): def validate_capcha(self):
if self.cleaned_data["capcha"].lower() != "i love me": if self.cleaned_data["capcha"].lower() != "i love me":
raise ValidationError(txt('Invalid captcha')) raise ValidationError(txt('Invalid captcha'), code="invalid")
return self return self

View File

@ -1,3 +1,4 @@
from django.forms import ValidationError
from django.http import HttpResponseNotFound, HttpResponseRedirect from django.http import HttpResponseNotFound, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
import os, random import os, random
@ -47,46 +48,59 @@ def signbook(request):
signer_list = Booksigner.objects.all() signer_list = Booksigner.objects.all()
if request.method == 'POST': if request.method == 'POST':
form = SignbookForm(request.POST) form = SignbookForm(request.POST)
if form.is_valid() and form.validate_capcha(): try:
newsigner = {} if form.is_valid() and form.validate_capcha():
newsigner["name"] = form.cleaned_data['name'] newsigner = {}
newsigner["website"] = form.cleaned_data['website'] newsigner["name"] = form.cleaned_data['name']
newsigner["email"] = form.cleaned_data['email'] newsigner["website"] = form.cleaned_data['website']
newsigner["comment"] = form.cleaned_data['comment'] newsigner["email"] = form.cleaned_data['email']
host = request.META["SERVER_NAME"] newsigner["comment"] = form.cleaned_data['comment']
print(host) host = request.META["SERVER_NAME"]
match host: print(host)
case "localhost": match host:
newsigner["hosting"] = "Local" case "localhost":
case r".*\.fai.st": newsigner["hosting"] = "Local"
newsigner["hosting"] = "fai" case r".*\.fai.st":
case _: newsigner["hosting"] = "fai"
newsigner["hosting"] = "???" case _:
newsigner["hosting"] = "???"
signer_list = Booksigner.objects.all() signer_list = Booksigner.objects.all()
if newsigner: if newsigner:
Booksigner.objects.create( Booksigner.objects.create(
name=newsigner["name"], name=newsigner["name"],
email=newsigner["email"], email=newsigner["email"],
website=newsigner["website"], website=newsigner["website"],
comment=newsigner["comment"], comment=newsigner["comment"],
hosting=newsigner["hosting"], hosting=newsigner["hosting"],
) )
context = {
"title": "THE GUESTBOOK",
"signer_list": signer_list
}
return render(request, "signbook.html", context)
context = { context = {
"title": "THE GUESTBOOK", "title": "THE GUESTBOOK",
"signer_list": signer_list "signer_list": signer_list,
"error_message": "You didn't input a required input.",
} }
return render(request, "signbook.html", context) return render(
context = { request,
"title": "THE GUESTBOOK", "signbook.html",
"signer_list": signer_list, context
"error_message": "You didn't input a required input.", )
} except ValidationError as e:
return render( print("Error: ", e)
request, context = {
"signbook.html", "title": "THE GUESTBOOK",
context "signer_list": signer_list,
) "error_message": "Invalid captcha.",
}
return render(
request,
"signbook.html",
context
)
else: else:
context = { context = {
"title": "THE GUESTBOOK", "title": "THE GUESTBOOK",