mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 11:19:57 +00:00
no duplicate entries and added flash messages
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user