114 lines
4.7 KiB
Python
114 lines
4.7 KiB
Python
# HTTP routes for statement extraction, recheck, and export - request in, call services, response out.
|
|
from flask import Blueprint, request, jsonify, send_file
|
|
|
|
import dependencies
|
|
from schemas import extraction_schema
|
|
from schemas.extraction_schema import ValidationError
|
|
from services import pipeline_service
|
|
from services import excel_service
|
|
from repositories import file_repository
|
|
|
|
extraction_bp = Blueprint("extraction", __name__, url_prefix="/api")
|
|
|
|
|
|
@extraction_bp.route("/process", methods=["POST"])
|
|
def process():
|
|
"""
|
|
Extracts and verifies a freshly-uploaded statement.
|
|
|
|
Data flow:
|
|
request (multipart form: statement_file, opening_balance?, closing_balance?)
|
|
-> extraction_schema.parse_process_request (validate)
|
|
-> file_repository.save_upload (write to disk)
|
|
-> pipeline_service.run (extract + check)
|
|
-> excel_service.build_workbook + file_repository.save_workbook (export)
|
|
-> JSON response: the pipeline result plus filename/title
|
|
"""
|
|
try:
|
|
file_storage, opening_balance, closing_balance = extraction_schema.parse_process_request(request)
|
|
except ValidationError as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
|
|
path = file_repository.save_upload(file_storage, dependencies.UPLOAD_FOLDER)
|
|
try:
|
|
result = pipeline_service.run(path, dependencies.DOCLING_URL, opening_balance, closing_balance)
|
|
except ValueError as e:
|
|
return jsonify({"error": str(e)}), 400
|
|
except Exception as e:
|
|
return jsonify({"error": f"Could not parse {file_storage.filename}: {e}"}), 400
|
|
finally:
|
|
file_repository.remove_upload(path)
|
|
|
|
workbook = excel_service.build_workbook(result, title=file_storage.filename)
|
|
fname = file_repository.new_output_filename()
|
|
file_repository.save_workbook(workbook, dependencies.OUTPUT_FOLDER, fname)
|
|
|
|
return jsonify({**result, "filename": fname, "title": file_storage.filename}), 200
|
|
|
|
|
|
@extraction_bp.route("/download/<filename>")
|
|
def download(filename):
|
|
"""Downloads a previously-exported Excel file by name."""
|
|
path = file_repository.resolve_output_path(dependencies.OUTPUT_FOLDER, filename)
|
|
if path is None:
|
|
return jsonify({"error": "File not found"}), 404
|
|
return send_file(path, as_attachment=True)
|
|
|
|
|
|
@extraction_bp.route("/recheck", methods=["POST"])
|
|
def recheck():
|
|
"""
|
|
Re-runs the checks against a (possibly user-edited) transaction list
|
|
without re-extracting anything - used for live status/flag updates in
|
|
the UI right after an edit. See pipeline_service.recheck().
|
|
|
|
Data flow:
|
|
request (JSON: transactions, page_reports, period?, balances?, narrative_direction?)
|
|
-> extraction_schema.parse_recheck_request (validate)
|
|
-> pipeline_service.recheck (re-run checks only)
|
|
-> JSON response: {status, checks}
|
|
"""
|
|
data = extraction_schema.parse_recheck_request(request.get_json(silent=True))
|
|
check_results = pipeline_service.recheck(
|
|
data["transactions"], data["page_reports"], data["period"],
|
|
data["opening_balance"], data["closing_balance"],
|
|
narrative_direction=data["narrative_direction"],
|
|
)
|
|
return jsonify(check_results), 200
|
|
|
|
|
|
@extraction_bp.route("/export", methods=["POST"])
|
|
def export():
|
|
"""
|
|
Re-runs the checks against the (possibly edited) transaction list,
|
|
same as /api/recheck, then writes and returns the Excel workbook in
|
|
one round trip - so a downloaded file always reflects whatever is
|
|
currently on screen, edits included, rather than the original
|
|
extraction from before any correction.
|
|
|
|
Data flow:
|
|
request (JSON: transactions, page_reports, period?, balances?,
|
|
narrative_direction?, brought_forward_balance?, title?)
|
|
-> extraction_schema.parse_export_request (validate)
|
|
-> pipeline_service.recheck (re-run checks on the edited data)
|
|
-> excel_service.build_workbook + file_repository.save_workbook
|
|
-> file response: the .xlsx attachment
|
|
"""
|
|
data = extraction_schema.parse_export_request(request.get_json(silent=True))
|
|
check_results = pipeline_service.recheck(
|
|
data["transactions"], data["page_reports"], data["period"],
|
|
data["opening_balance"], data["closing_balance"],
|
|
narrative_direction=data["narrative_direction"],
|
|
)
|
|
result = {
|
|
"transactions": data["transactions"],
|
|
"page_reports": data["page_reports"],
|
|
"period": data["period"],
|
|
"brought_forward_balance": data["brought_forward_balance"],
|
|
**check_results,
|
|
}
|
|
workbook = excel_service.build_workbook(result, title=data["title"])
|
|
fname = file_repository.new_output_filename()
|
|
path = file_repository.save_workbook(workbook, dependencies.OUTPUT_FOLDER, fname)
|
|
return send_file(path, as_attachment=True, download_name=fname)
|