mirror of
https://github.com/StefBuwalda/ProjectIOT.git
synced 2025-10-30 03:09:58 +00:00
Fixed flash msgs on add page
This commit is contained in:
@@ -55,34 +55,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
document.getElementById('numberplateForm').addEventListener('submit', async function (e) {
|
|
||||||
e.preventDefault();
|
|
||||||
const form = e.target;
|
|
||||||
const formData = new FormData(form);
|
|
||||||
|
|
||||||
const response = await fetch(form.action || window.location.pathname, {
|
|
||||||
method: 'POST',
|
|
||||||
body: formData,
|
|
||||||
headers: { 'X-Requested-With': 'XMLHttpRequest' }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
// Update the table body
|
|
||||||
const tbody = document.getElementById('numberplatesBody');
|
|
||||||
tbody.innerHTML = '';
|
|
||||||
data.plates.forEach(plate => {
|
|
||||||
tbody.innerHTML += `<tr>
|
|
||||||
<td><small class="fs-6">${plate.id}</small></td>
|
|
||||||
<td><small class="fs-6">${plate.plate}</small></td>
|
|
||||||
</tr>`;
|
|
||||||
});
|
|
||||||
form.reset();
|
|
||||||
} else {
|
|
||||||
alert('Failed to add numberplate.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -4,12 +4,14 @@ from flask import (
|
|||||||
request,
|
request,
|
||||||
jsonify,
|
jsonify,
|
||||||
flash,
|
flash,
|
||||||
send_from_directory,
|
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")
|
||||||
|
|
||||||
@@ -27,7 +29,6 @@ def dashboard():
|
|||||||
@dash_blueprint.route("/add", methods=["GET", "POST"])
|
@dash_blueprint.route("/add", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def add():
|
def add():
|
||||||
Plates = AllowedPlate.query.all()
|
|
||||||
form = npForm()
|
form = npForm()
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
@@ -41,18 +42,8 @@ def add():
|
|||||||
db.session.add(ap)
|
db.session.add(ap)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash("Numberplate succesfully added")
|
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
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
# form wasn't valid
|
# form wasn't valid
|
||||||
|
Plates = AllowedPlate.query.all()
|
||||||
return render_template(
|
return render_template(
|
||||||
"add.html", form=npForm(formdata=None), plates=Plates
|
"add.html", form=npForm(formdata=None), plates=Plates
|
||||||
)
|
)
|
||||||
@@ -67,9 +58,6 @@ def live():
|
|||||||
@dash_blueprint.route("/live_image", methods=["GET"])
|
@dash_blueprint.route("/live_image", methods=["GET"])
|
||||||
@login_required
|
@login_required
|
||||||
def live_image():
|
def live_image():
|
||||||
return send_from_directory(
|
image_copy = BytesIO(application.last_image.getvalue())
|
||||||
"static/live",
|
image_copy.seek(0)
|
||||||
"feed.png",
|
return send_file(image_copy, mimetype="image/jpeg")
|
||||||
as_attachment=False,
|
|
||||||
conditional=True,
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user