mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 11:19:57 +00:00
112 lines
3.4 KiB
Python
112 lines
3.4 KiB
Python
from flask import (
|
|
Blueprint,
|
|
render_template,
|
|
request,
|
|
jsonify,
|
|
flash,
|
|
send_from_directory,
|
|
redirect,
|
|
url_for
|
|
# send_file,
|
|
)
|
|
from application.dashboard.models import AllowedPlate, LoggedItem
|
|
from application import db
|
|
import application
|
|
from application.dashboard.forms import npForm
|
|
from flask_login import login_required
|
|
from io import BytesIO
|
|
|
|
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
|
|
|
|
|
@dash_blueprint.route("/dashboard")
|
|
#@login_required
|
|
def dashboard():
|
|
Plates = AllowedPlate.query.all()
|
|
logs = (
|
|
LoggedItem.query.order_by(LoggedItem.dateLogged.desc()).limit(50).all()
|
|
)
|
|
return render_template("dashboard.html", plates=Plates, logs=logs)
|
|
|
|
|
|
@dash_blueprint.route("/add", methods=["GET", "POST"])
|
|
#@login_required
|
|
def add():
|
|
form = npForm()
|
|
|
|
if form.validate_on_submit():
|
|
plate = form.numberplate.data
|
|
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()
|
|
flash("Numberplate succesfully added")
|
|
# form wasn't valid
|
|
Plates = AllowedPlate.query.all()
|
|
return render_template(
|
|
"add.html", form=npForm(formdata=None), plates=Plates
|
|
)
|
|
|
|
|
|
@dash_blueprint.route("/live", methods=["GET"])
|
|
@login_required
|
|
def live():
|
|
return render_template("live.html")
|
|
|
|
|
|
@dash_blueprint.route("/live_image", methods=["GET"])
|
|
@login_required
|
|
def live_image():
|
|
return send_from_directory(
|
|
"static/live",
|
|
"feed.png",
|
|
as_attachment=False,
|
|
conditional=True,
|
|
)
|
|
|
|
|
|
@dash_blueprint.route('/logs', methods=['GET', 'POST'])
|
|
#@login_required
|
|
def logs():
|
|
form = LoggedItem.query.all()
|
|
return render_template("logs.html", form=form)
|
|
|
|
@dash_blueprint.route("/edit/<string:plate>",methods=["GET", "POST"])
|
|
#login_required
|
|
def edit(plate: str):
|
|
print(plate)
|
|
editnp = AllowedPlate.query.filter_by(plate=plate).first_or_404()
|
|
form = npForm()
|
|
if form.validate_on_submit():
|
|
commit = False
|
|
plate = form.numberplate.data
|
|
if plate: # To prevent red lines in VSCode
|
|
# Check if number plate already exists
|
|
if editnp.plate == form.numberplate.data:
|
|
flash("Numberplate has not been changed")
|
|
return redirect(url_for("dash.dashboard"))
|
|
if AllowedPlate.query.filter_by(plate=plate).first():
|
|
flash("Numberplate is already registered")
|
|
return redirect(url_for("dash.dashboard"))
|
|
if editnp.plate != form.numberplate.data:
|
|
editnp.plate = form.numberplate.data
|
|
commit = True
|
|
if commit:
|
|
db.session.commit()
|
|
flash("Numberplate edited succesfully")
|
|
return redirect(url_for("dash.dashboard"))
|
|
form.numberplate.data = editnp.plate
|
|
return render_template("edit.html",form=form)
|
|
|
|
@dash_blueprint.route("/delete/<string:plate>", methods=["POST"])
|
|
#@login_required
|
|
def delete_plate(plate: str):
|
|
plate_obj = AllowedPlate.query.filter_by(plate=plate).first_or_404()
|
|
db.session.delete(plate_obj)
|
|
db.session.commit()
|
|
flash("Numberplate deleted successfully.")
|
|
return redirect(url_for("dash.dashboard")) |