mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 11:19:57 +00:00
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
from flask import (
|
|
Blueprint,
|
|
render_template,
|
|
flash,
|
|
send_from_directory,
|
|
redirect,
|
|
url_for,
|
|
)
|
|
from application.dashboard.models import AllowedPlate, LoggedItem
|
|
from application import db
|
|
from application.dashboard.forms import npForm
|
|
from flask_login import login_required
|
|
|
|
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()) # type: ignore
|
|
.limit(50)
|
|
.all()
|
|
)
|
|
form = npForm()
|
|
return render_template("dashboard.html", plates=Plates, logs=logs, form=form)
|
|
|
|
|
|
@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):
|
|
# 404 if plate is invalid
|
|
editnp = AllowedPlate.query.filter_by(plate=plate).first_or_404()
|
|
|
|
# Get form data
|
|
form = npForm()
|
|
|
|
# Check if there is a valid submittion
|
|
if form.validate_on_submit():
|
|
# Compare form plate to existing plate
|
|
form_plate = form.numberplate.data
|
|
if editnp.plate == form_plate:
|
|
flash("Numberplate has not been changed")
|
|
return redirect(url_for("dash.dashboard"))
|
|
else: # Change detected
|
|
editnp.plate = form.numberplate.data
|
|
db.session.commit()
|
|
flash("Numberplate edited succesfully")
|
|
return redirect(url_for("dash.dashboard"))
|
|
form.numberplate.data = 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"))
|