70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from flask import Blueprint, jsonify, request, send_file
|
|
|
|
from app.auth_jwt import get_current_user
|
|
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():
|
|
user = get_current_user()
|
|
if not user:
|
|
return None, (jsonify({'error': 'Unauthorized'}), 401)
|
|
return user, None
|
|
|
|
|
|
@workspace_schedule_bp.route('/schedule-runs', methods=['GET'])
|
|
def workspace_schedule_runs():
|
|
user, err = _require_user()
|
|
if err:
|
|
return err
|
|
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) if started_after_str else None
|
|
started_before = datetime.fromisoformat(started_before_str) 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, err = _require_user()
|
|
if err:
|
|
return err
|
|
run = get_run(run_id)
|
|
if not run:
|
|
return jsonify({'error': 'Not found'}), 404
|
|
if not can_view_run(run, user.id):
|
|
return jsonify({'error': 'Forbidden'}), 403
|
|
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, err = _require_user()
|
|
if err:
|
|
return err
|
|
run = get_run(run_id)
|
|
if not run:
|
|
return jsonify({'error': 'Not found'}), 404
|
|
if not can_view_run(run, user.id):
|
|
return jsonify({'error': 'Forbidden'}), 403
|
|
if not run.excel_file_data:
|
|
return jsonify({'error': 'Excel not found'}), 404
|
|
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')
|