Files
syops/backend/app/api/workspace_schedule.py
T

81 lines
2.8 KiB
Python

"""
backend/app/api/workspace_schedule.py
用途:工作台排程运行记录查询 API。
依赖:app.core.auth.jwt、app.core.errors、schedule.query_service。
逻辑:鉴权后只读查询 ScheduleRun;未登录 raise UnauthorizedError。
"""
from __future__ import annotations
from datetime import datetime
from flask import Blueprint, jsonify, request, send_file
from app.core.auth.jwt import get_current_user
from app.core.errors import ForbiddenError, NotFoundError, UnauthorizedError
from app.services.schedule.query_service import can_view_run, get_run, list_runs, serialize_run
workspace_schedule_bp = Blueprint("workspace_schedule", __name__)
def _require_user():
"""校验当前用户;未登录时抛出 UnauthorizedError。"""
user = get_current_user()
if not user:
raise UnauthorizedError()
return user
@workspace_schedule_bp.route("/schedule-runs", methods=["GET"])
def workspace_schedule_runs():
user = _require_user()
status = str(request.args.get("status") or "").strip() or None
schedule_name = str(request.args.get("schedule_name") or "").strip() or None
started_after_str = request.args.get("started_after")
started_before_str = request.args.get("started_before")
started_after = datetime.fromisoformat(started_after_str.replace("Z", "+00:00")) if started_after_str else None
started_before = datetime.fromisoformat(started_before_str.replace("Z", "+00:00")) if started_before_str else None
limit = int(request.args.get("limit") or 100)
return jsonify(
list_runs(
notify_user_id=user.id,
schedule_name=schedule_name,
status=status,
started_after=started_after,
started_before=started_before,
limit=limit,
)
)
@workspace_schedule_bp.route("/schedule-runs/<int:run_id>", methods=["GET"])
def workspace_schedule_run_detail(run_id: int):
user = _require_user()
run = get_run(run_id)
if not run:
raise NotFoundError("Not found")
if not can_view_run(run, user.id):
raise ForbiddenError("Forbidden")
return jsonify(serialize_run(run))
@workspace_schedule_bp.route("/schedule-runs/<int:run_id>/excel", methods=["GET"])
def workspace_schedule_run_excel(run_id: int):
from io import BytesIO
user = _require_user()
run = get_run(run_id)
if not run:
raise NotFoundError("Not found")
if not can_view_run(run, user.id):
raise ForbiddenError("Forbidden")
if not run.excel_file_data:
raise NotFoundError("Excel not found")
buf = BytesIO(run.excel_file_data)
return send_file(
buf,
as_attachment=True,
download_name=run.excel_file_name or "result.xlsx",
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)