fix: BLS-Datenimport als Hintergrundjob gegen HTTP 504
Prüfung und Apply laufen serverseitig, die UI pollt nur noch den Status. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8e964509d7
commit
a07a668de2
6 files changed
+281
-35
No files matched your search
@@ -9,9 +9,12 @@ from pydantic import BaseModel
|
||||
|
||||
from auth import require_admin
|
||||
from bls.import_service import upsert_attributes, upsert_foods
|
||||
from bls.jobs import create_and_check, get_job, start_apply
|
||||
from bls.parser import parse_components_xlsx, parse_foods_xlsx
|
||||
from db import get_cursor, get_db, r2d
|
||||
|
||||
MAX_IMPORT_BYTES = 50 * 1024 * 1024
|
||||
|
||||
router = APIRouter(prefix="/api/admin/bls", tags=["admin", "bls"])
|
||||
|
||||
|
||||
@@ -111,6 +114,42 @@ async def import_foods(
|
||||
return {"dry_run": False, **stats}
|
||||
|
||||
|
||||
@router.post("/import/jobs")
|
||||
async def start_import_job(
|
||||
kind: str,
|
||||
file: UploadFile = File(...),
|
||||
session: dict = Depends(require_admin),
|
||||
):
|
||||
if kind not in ("components", "foods"):
|
||||
raise HTTPException(400, "kind muss components oder foods sein")
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
raise HTTPException(400, "Leere Datei")
|
||||
if len(raw) > MAX_IMPORT_BYTES:
|
||||
raise HTTPException(400, "Datei größer als 50 MB")
|
||||
job_id = create_and_check(kind, raw)
|
||||
return {"id": job_id, "status": "checking"}
|
||||
|
||||
|
||||
@router.get("/import/jobs/{job_id}")
|
||||
def import_job_status(job_id: str, session: dict = Depends(require_admin)):
|
||||
job = get_job(job_id)
|
||||
if not job:
|
||||
raise HTTPException(404, "Import-Job nicht gefunden")
|
||||
return job
|
||||
|
||||
|
||||
@router.post("/import/jobs/{job_id}/apply")
|
||||
def apply_import_job(job_id: str, session: dict = Depends(require_admin)):
|
||||
try:
|
||||
start_apply(job_id)
|
||||
except KeyError:
|
||||
raise HTTPException(404, "Import-Job nicht gefunden") from None
|
||||
except ValueError as e:
|
||||
raise HTTPException(409, str(e)) from e
|
||||
return get_job(job_id)
|
||||
|
||||
|
||||
@router.get("/foods")
|
||||
def admin_list_foods(
|
||||
q: Optional[str] = None,
|
||||
|
||||
Reference in new issue
Block a user