no duplicate entries and added flash messages

This commit is contained in:
DaanoGames
2025-06-02 12:29:27 +02:00
parent 73286d1d8f
commit 73837bdb5c
3 changed files with 48 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
from flask import Blueprint, render_template, request, jsonify
from flask import Blueprint, render_template, request, jsonify, flash
from application.dashboard.models import AllowedPlate, LoggedItem
from application import db
from application.dashboard.forms import npForm
@@ -24,21 +24,31 @@ def add():
form = npForm()
if form.validate_on_submit():
if form.numberplate.data:
ap = AllowedPlate(plate=form.numberplate.data)
db.session.add(ap)
db.session.commit()
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")
# 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=form, 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
]
}
)
Plates = AllowedPlate.query.order_by(AllowedPlate.id).all()
return render_template("add.html", form=npForm(formdata=None), plates=Plates)