mirror of
				https://github.com/StefBuwalda/ProjectIOT.git
				synced 2025-10-31 03:39:57 +00:00 
			
		
		
		
	Cleanup and simplification
This commit is contained in:
		| @@ -4,6 +4,7 @@ from wtforms import StringField, SubmitField | |||||||
|  |  | ||||||
|  |  | ||||||
| class npForm(FlaskForm): | class npForm(FlaskForm): | ||||||
|     numberplate = StringField('Insert plate here:', validators=[DataRequired()]) |     numberplate = StringField( | ||||||
|     submit = SubmitField('Submit') |         "Insert plate here:", validators=[DataRequired()] | ||||||
|  |     ) | ||||||
|  |     submit = SubmitField("Submit") | ||||||
|   | |||||||
| @@ -1,36 +1,33 @@ | |||||||
| from flask import ( | from flask import ( | ||||||
|     Blueprint, |     Blueprint, | ||||||
|     render_template, |     render_template, | ||||||
|     request, |  | ||||||
|     jsonify, |  | ||||||
|     flash, |     flash, | ||||||
|     send_from_directory, |     send_from_directory, | ||||||
|     redirect, |     redirect, | ||||||
|     url_for |     url_for, | ||||||
| #    send_file, |  | ||||||
| ) | ) | ||||||
| from application.dashboard.models import AllowedPlate, LoggedItem | from application.dashboard.models import AllowedPlate, LoggedItem | ||||||
| from application import db | from application import db | ||||||
| import application |  | ||||||
| from application.dashboard.forms import npForm | from application.dashboard.forms import npForm | ||||||
| from flask_login import login_required | from flask_login import login_required | ||||||
| from io import BytesIO |  | ||||||
|  |  | ||||||
| dash_blueprint = Blueprint("dash", __name__, template_folder="templates") | dash_blueprint = Blueprint("dash", __name__, template_folder="templates") | ||||||
|  |  | ||||||
|  |  | ||||||
| @dash_blueprint.route("/dashboard") | @dash_blueprint.route("/dashboard") | ||||||
| #@login_required | # @login_required | ||||||
| def dashboard(): | def dashboard(): | ||||||
|     Plates = AllowedPlate.query.all() |     Plates = AllowedPlate.query.all() | ||||||
|     logs = ( |     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) |     return render_template("dashboard.html", plates=Plates, logs=logs) | ||||||
|  |  | ||||||
|  |  | ||||||
| @dash_blueprint.route("/add", methods=["GET", "POST"]) | @dash_blueprint.route("/add", methods=["GET", "POST"]) | ||||||
| #@login_required | # @login_required | ||||||
| def add(): | def add(): | ||||||
|     form = npForm() |     form = npForm() | ||||||
|  |  | ||||||
| @@ -69,41 +66,40 @@ def live_image(): | |||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @dash_blueprint.route('/logs', methods=['GET', 'POST']) | @dash_blueprint.route("/logs", methods=["GET", "POST"]) | ||||||
| #@login_required | # @login_required | ||||||
| def logs(): | def logs(): | ||||||
|     form = LoggedItem.query.all() |     form = LoggedItem.query.all() | ||||||
|     return render_template("logs.html", form=form) |     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): | def edit(plate: str): | ||||||
|     print(plate) |     # 404 if plate is invalid | ||||||
|     editnp = AllowedPlate.query.filter_by(plate=plate).first_or_404() |     editnp = AllowedPlate.query.filter_by(plate=plate).first_or_404() | ||||||
|  |  | ||||||
|  |     # Get form data | ||||||
|     form = npForm() |     form = npForm() | ||||||
|  |  | ||||||
|  |     # Check if there is a valid submittion | ||||||
|     if form.validate_on_submit(): |     if form.validate_on_submit(): | ||||||
|         commit = False |         # Compare form plate to existing plate | ||||||
|         plate = form.numberplate.data |         form_plate = form.numberplate.data | ||||||
|         if plate:  # To prevent red lines in VSCode |         if editnp.plate == form_plate: | ||||||
|             # Check if number plate already exists |  | ||||||
|             if editnp.plate == form.numberplate.data: |  | ||||||
|             flash("Numberplate has not been changed") |             flash("Numberplate has not been changed") | ||||||
|             return redirect(url_for("dash.dashboard")) |             return redirect(url_for("dash.dashboard")) | ||||||
|             if AllowedPlate.query.filter_by(plate=plate).first(): |         else:  # Change detected | ||||||
|                 flash("Numberplate is already registered") |  | ||||||
|                 return redirect(url_for("dash.dashboard")) |  | ||||||
|         if editnp.plate != form.numberplate.data: |  | ||||||
|             editnp.plate = form.numberplate.data |             editnp.plate = form.numberplate.data | ||||||
|             commit = True |  | ||||||
|         if commit: |  | ||||||
|             db.session.commit() |             db.session.commit() | ||||||
|             flash("Numberplate edited succesfully") |             flash("Numberplate edited succesfully") | ||||||
|             return redirect(url_for("dash.dashboard")) |             return redirect(url_for("dash.dashboard")) | ||||||
|     form.numberplate.data = editnp.plate |     form.numberplate.data = plate | ||||||
|     return render_template("edit.html",form=form) |     return render_template("edit.html", form=form) | ||||||
|  |  | ||||||
|  |  | ||||||
| @dash_blueprint.route("/delete/<string:plate>", methods=["POST"]) | @dash_blueprint.route("/delete/<string:plate>", methods=["POST"]) | ||||||
| #@login_required | # @login_required | ||||||
| def delete_plate(plate: str): | def delete_plate(plate: str): | ||||||
|     plate_obj = AllowedPlate.query.filter_by(plate=plate).first_or_404() |     plate_obj = AllowedPlate.query.filter_by(plate=plate).first_or_404() | ||||||
|     db.session.delete(plate_obj) |     db.session.delete(plate_obj) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user