From 8214e36a64097e0b6436ff6fa747c4b6b28d09d5 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 2 Jun 2025 12:52:16 +0200 Subject: [PATCH] Update views.py Fixed numbers plates not disappearing when submitting an existing number plate --- application/dashboard/views.py | 48 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/application/dashboard/views.py b/application/dashboard/views.py index 34725ae..0c90980 100644 --- a/application/dashboard/views.py +++ b/application/dashboard/views.py @@ -25,30 +25,26 @@ def add(): if form.validate_on_submit(): plate = form.numberplate.data - print("test") - if AllowedPlate.query.filter_by(plate=plate).first(): - print("test1") - flash("Numberplate is already registered") - return render_template( - "dashboard.html", - form=npForm(formdata=None), - feedback="Numberplate is already registered", - ) - ap = AllowedPlate(plate=plate) - db.session.add(ap) - db.session.commit() - print("test2") - flash("Numberplate succesfully added") + if plate: # To prevent red lines in VSCode + # Check if number plate already exists + if AllowedPlate.query.filter_by(plate=plate).first(): + flash("Numberplate is already registered") + else: # NP does not exist + ap = AllowedPlate(plate=plate) + db.session.add(ap) + db.session.commit() - # Update the list on the page with JavaScript - if request.headers.get("X-Requested-With") == "XMLHttpRequest": - plates = AllowedPlate.query.order_by(AllowedPlate.id).all() - return jsonify( - { - "plates": [ - {"id": p.id, "plate": p.plate} for p in plates - ] - } - ) - Plates = AllowedPlate.query.order_by(AllowedPlate.id).all() - return render_template("add.html", form=npForm(formdata=None), plates=Plates) + # Update the list on the page with JavaScript + if request.headers.get("X-Requested-With") == "XMLHttpRequest": + plates = AllowedPlate.query.order_by(AllowedPlate.id).all() + return jsonify( + { + "plates": [ + {"id": p.id, "plate": p.plate} for p in plates + ] + } + ) + # form wasn't valid + return render_template( + "add.html", form=npForm(formdata=None), plates=Plates + )