refactor(fc2c-i): sweep blueprints + ml tasks onto shared get_session (covers tasks/ml.py, a survey gap)

This commit is contained in:
2026-05-15 15:48:02 -04:00
parent fc33c23dd5
commit f7f75fcac6
9 changed files with 42 additions and 164 deletions
+5 -20
View File
@@ -2,28 +2,16 @@
from quart import Blueprint, jsonify, request
from ..extensions import make_engine, make_session_factory
from ..extensions import get_session
from ..models import TagAllowlist
from ..services.ml.allowlist import AllowlistService
allowlist_bp = Blueprint("allowlist", __name__, url_prefix="/api")
_engine = None
_Session = None
def _session_factory():
global _engine, _Session
if _engine is None:
_engine = make_engine()
_Session = make_session_factory(_engine)
return _Session
@allowlist_bp.route("/allowlist", methods=["GET"])
async def list_allowlist():
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
rows = await AllowlistService(session).list_all()
return jsonify(
[
@@ -40,8 +28,7 @@ async def list_allowlist():
@allowlist_bp.route("/tags/<int:tag_id>/allowlist", methods=["GET"])
async def get_one(tag_id: int):
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
row = await session.get(TagAllowlist, tag_id)
if row is None:
return jsonify({"error": "not on allowlist"}), 404
@@ -58,8 +45,7 @@ async def patch_threshold(tag_id: int):
mc = float(body["min_confidence"])
if not (0 < mc <= 1):
return jsonify({"error": "min_confidence must be in (0, 1]"}), 400
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
await AllowlistService(session).update_threshold(tag_id, mc)
await session.commit()
return "", 204
@@ -67,8 +53,7 @@ async def patch_threshold(tag_id: int):
@allowlist_bp.route("/tags/<int:tag_id>/allowlist", methods=["DELETE"])
async def remove(tag_id: int):
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
await AllowlistService(session).remove(tag_id)
await session.commit()
return "", 204