From 2868916cf6ad5697291d8b4c71bcbac0932e8660 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Mon, 31 Mar 2025 00:37:15 +0200 Subject: [PATCH] adds schedule config view --- scipaperloader/views.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/scipaperloader/views.py b/scipaperloader/views.py index 5c9712a..3f80f98 100644 --- a/scipaperloader/views.py +++ b/scipaperloader/views.py @@ -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,15 +22,36 @@ def papers(): @bp.route("/schedule", methods=["GET", "POST"]) def schedule(): if request.method == "POST": - 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() + 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)) + 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()} volume = VolumeConfig.query.first()