import os, uuid from flask import Blueprint, request, jsonify, render_template, current_app from werkzeug.utils import secure_filename from .logic import run_summary_check summary_check_bp = Blueprint("summary_check", __name__, url_prefix="/summary-check") @summary_check_bp.route("/") def index(): return render_template("summary_check/index.html") @summary_check_bp.route("/api/check", methods=["POST"]) def api_check(): current_file = request.files.get("current_file") if not current_file or not current_file.filename: return jsonify({"error": "Upload this period's consolidated workbook"}), 400 def _save(f): path = os.path.join( current_app.config["UPLOAD_FOLDER"], f"{uuid.uuid4()}_{secure_filename(f.filename)}", ) f.save(path) return path current_path = _save(current_file) prior_path = None prior_file = request.files.get("prior_file") if prior_file and prior_file.filename: prior_path = _save(prior_file) try: results, totals, error = run_summary_check(current_path, prior_path) finally: os.remove(current_path) if prior_path: os.remove(prior_path) if error: return jsonify({"error": error}), 400 mismatches = [r for r in results if not r["matches"]] return jsonify({ "status": "ok", "accounts": results, "totals": totals, "mismatch_count": len(mismatches), "total_count": len(results), "prior_file_used": prior_path is not None, }), 200