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,22 +2,11 @@
from quart import Blueprint, jsonify, request
from ..extensions import make_engine, make_session_factory
from ..extensions import get_session
from ..services.gallery_service import GalleryService
gallery_bp = Blueprint("gallery", __name__, url_prefix="/api/gallery")
_engine = None
_Session = None
def _session_factory():
global _engine, _Session
if _engine is None:
_engine = make_engine()
_Session = make_session_factory(_engine)
return _Session
@gallery_bp.route("/scroll", methods=["GET"])
async def scroll():
@@ -29,8 +18,7 @@ async def scroll():
tag_id_raw = request.args.get("tag_id")
tag_id = int(tag_id_raw) if tag_id_raw else None
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
svc = GalleryService(session)
try:
page = await svc.scroll(cursor=cursor, limit=limit, tag_id=tag_id)
@@ -63,8 +51,7 @@ async def scroll():
async def timeline():
tag_id_raw = request.args.get("tag_id")
tag_id = int(tag_id_raw) if tag_id_raw else None
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
svc = GalleryService(session)
buckets = await svc.timeline(tag_id=tag_id)
return jsonify([{"year": b.year, "month": b.month, "count": b.count} for b in buckets])
@@ -79,8 +66,7 @@ async def jump():
return jsonify({"error": "year and month query params required"}), 400
tag_id_raw = request.args.get("tag_id")
tag_id = int(tag_id_raw) if tag_id_raw else None
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
svc = GalleryService(session)
cursor = await svc.jump_cursor(year=year, month=month, tag_id=tag_id)
return jsonify({"cursor": cursor})
@@ -88,8 +74,7 @@ async def jump():
@gallery_bp.route("/image/<int:image_id>", methods=["GET"])
async def image_detail(image_id: int):
Session = _session_factory()
async with Session() as session:
async with get_session() as session:
svc = GalleryService(session)
payload = await svc.get_image_with_tags(image_id)
if payload is None: