mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-29 18:59:57 +00:00
Update views.py
Fixed numbers plates not disappearing when submitting an existing number plate
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user