adds schedule config view

This commit is contained in:
Michael Beck 2025-03-31 00:37:15 +02:00
parent 2b45c67ddf
commit 2868916cf6

View File

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, current_app, request
from flask import Blueprint, render_template, current_app, request, flash, redirect, url_for
from .models import ScheduleConfig, VolumeConfig
from .db import db
@ -22,6 +22,21 @@ def papers():
@bp.route("/schedule", methods=["GET", "POST"])
def schedule():
if request.method == "POST":
try:
# Validate form data
for hour in range(24):
key = f"hour_{hour}"
if key not in request.form:
raise ValueError(f"Missing data for hour {hour}")
try:
weight = float(request.form.get(key, 0))
if weight < 0 or weight > 5:
raise ValueError(f"Weight for hour {hour} must be between 0 and 5")
except ValueError:
raise ValueError(f"Invalid weight value for hour {hour}")
# Update database if validation passes
for hour in range(24):
key = f"hour_{hour}"
weight = float(request.form.get(key, 0))
@ -30,7 +45,13 @@ def schedule():
config.weight = weight
else:
db.session.add(ScheduleConfig(hour=hour, weight=weight))
db.session.commit()
flash("Schedule updated successfully!", "success")
except ValueError as e:
db.session.rollback()
flash(f"Error updating schedule: {str(e)}", "error")
schedule = {sc.hour: sc.weight for sc in ScheduleConfig.query.order_by(ScheduleConfig.hour).all()}
volume = VolumeConfig.query.first()