Fixed flash msgs on add page

This commit is contained in:
2025-06-05 12:02:13 +02:00
parent 75b19f2e1b
commit b26a1905ad
2 changed files with 7 additions and 49 deletions

View File

@@ -55,34 +55,4 @@
</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 %}

View File

@@ -4,12 +4,14 @@ from flask import (
request,
jsonify,
flash,
send_from_directory,
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")
@@ -27,7 +29,6 @@ def dashboard():
@dash_blueprint.route("/add", methods=["GET", "POST"])
@login_required
def add():
Plates = AllowedPlate.query.all()
form = npForm()
if form.validate_on_submit():
@@ -41,18 +42,8 @@ def add():
db.session.add(ap)
db.session.commit()
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
Plates = AllowedPlate.query.all()
return render_template(
"add.html", form=npForm(formdata=None), plates=Plates
)
@@ -67,9 +58,6 @@ def live():
@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,
)
image_copy = BytesIO(application.last_image.getvalue())
image_copy.seek(0)
return send_file(image_copy, mimetype="image/jpeg")