adds schedule config view
This commit is contained in:
parent
2b45c67ddf
commit
2868916cf6
@ -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 .models import ScheduleConfig, VolumeConfig
|
||||||
from .db import db
|
from .db import db
|
||||||
|
|
||||||
@ -22,15 +22,36 @@ def papers():
|
|||||||
@bp.route("/schedule", methods=["GET", "POST"])
|
@bp.route("/schedule", methods=["GET", "POST"])
|
||||||
def schedule():
|
def schedule():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
for hour in range(24):
|
try:
|
||||||
key = f"hour_{hour}"
|
# Validate form data
|
||||||
weight = float(request.form.get(key, 0))
|
for hour in range(24):
|
||||||
config = ScheduleConfig.query.get(hour)
|
key = f"hour_{hour}"
|
||||||
if config:
|
if key not in request.form:
|
||||||
config.weight = weight
|
raise ValueError(f"Missing data for hour {hour}")
|
||||||
else:
|
|
||||||
db.session.add(ScheduleConfig(hour=hour, weight=weight))
|
try:
|
||||||
db.session.commit()
|
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))
|
||||||
|
config = ScheduleConfig.query.get(hour)
|
||||||
|
if config:
|
||||||
|
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()}
|
schedule = {sc.hour: sc.weight for sc in ScheduleConfig.query.order_by(ScheduleConfig.hour).all()}
|
||||||
volume = VolumeConfig.query.first()
|
volume = VolumeConfig.query.first()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user