mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-29 18:59:57 +00:00
Cleanup and simplification
This commit is contained in:
@@ -4,6 +4,7 @@ from wtforms import StringField, SubmitField
|
||||
|
||||
|
||||
class npForm(FlaskForm):
|
||||
numberplate = StringField('Insert plate here:', validators=[DataRequired()])
|
||||
submit = SubmitField('Submit')
|
||||
|
||||
numberplate = StringField(
|
||||
"Insert plate here:", validators=[DataRequired()]
|
||||
)
|
||||
submit = SubmitField("Submit")
|
||||
|
||||
@@ -1,36 +1,33 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
render_template,
|
||||
request,
|
||||
jsonify,
|
||||
flash,
|
||||
send_from_directory,
|
||||
redirect,
|
||||
url_for
|
||||
# send_file,
|
||||
url_for,
|
||||
)
|
||||
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
|
||||
# @login_required
|
||||
def dashboard():
|
||||
Plates = AllowedPlate.query.all()
|
||||
logs = (
|
||||
LoggedItem.query.order_by(LoggedItem.dateLogged.desc()).limit(50).all()
|
||||
LoggedItem.query.order_by(LoggedItem.dateLogged.desc()) # type: ignore
|
||||
.limit(50)
|
||||
.all()
|
||||
)
|
||||
return render_template("dashboard.html", plates=Plates, logs=logs)
|
||||
|
||||
|
||||
@dash_blueprint.route("/add", methods=["GET", "POST"])
|
||||
#@login_required
|
||||
# @login_required
|
||||
def add():
|
||||
form = npForm()
|
||||
|
||||
@@ -69,44 +66,43 @@ def live_image():
|
||||
)
|
||||
|
||||
|
||||
@dash_blueprint.route('/logs', methods=['GET', 'POST'])
|
||||
#@login_required
|
||||
@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
|
||||
|
||||
@dash_blueprint.route("/edit/<string:plate>", methods=["GET", "POST"])
|
||||
# login_required
|
||||
def edit(plate: str):
|
||||
print(plate)
|
||||
# 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():
|
||||
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:
|
||||
# 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
|
||||
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)
|
||||
form.numberplate.data = plate
|
||||
return render_template("edit.html", form=form)
|
||||
|
||||
|
||||
@dash_blueprint.route("/delete/<string:plate>", methods=["POST"])
|
||||
#@login_required
|
||||
# @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"))
|
||||
return redirect(url_for("dash.dashboard"))
|
||||
|
||||
Reference in New Issue
Block a user