feat: closed batch loop — manual positions, Generate CVs, download links

This commit is contained in:
root
2026-07-28 19:22:50 +00:00
parent aff5011bac
commit 29c0a0c348
4 changed files with 213 additions and 1 deletions

92
main.py
View File

@@ -1075,6 +1075,98 @@ async def batch_chat_endpoint(batch_id: str, request: Request):
return {"response": ai_response}
@app.put("/api/batches/{batch_id}/positions")
async def update_batch_positions(batch_id: str, request: Request):
"""Update the positions array on a batch (add/edit/remove positions)."""
body = await request.json()
positions = body.get("positions", [])
row = db.execute(
"UPDATE cv_batches SET positions = %s, updated_at = NOW() WHERE id = %s RETURNING *",
(Json(positions), batch_id)
)
if not row:
raise HTTPException(404, "Batch not found")
return dict(row)
@app.post("/api/batches/{batch_id}/generate")
async def generate_batch_cvs(batch_id: str):
"""Generate CVs for all approved items in the batch using the selected template."""
import httpx
batch = db.query("SELECT * FROM cv_batches WHERE id = %s", (batch_id,), fetch='one')
if not batch:
raise HTTPException(404, "Batch not found")
template_id = batch.get("template_id")
if not template_id:
raise HTTPException(400, "No template selected for this batch")
# Get approved items with candidate data
items = db.query("""
SELECT bi.*, c.first_name, c.last_name, c.email, c.phone, c.address,
c.linkedin, c.github, c.website, c.summary, c.raw_cv_text
FROM batch_items bi
JOIN candidates c ON bi.candidate_id = c.id
WHERE bi.batch_id = %s AND bi.status = 'approved'
ORDER BY bi.position_title, bi.match_score DESC
""", (batch_id,))
if not items:
raise HTTPException(400, "No approved candidates to generate CVs for")
# For each approved item, fetch full candidate data and call Carbone render
results = []
carbone_url = f"http://localhost:8771/api/carbone/templates/{template_id}/render"
for item in items:
candidate_id = str(item["candidate_id"])
# Fetch full candidate data (skills, experience, education, certs)
cand = dict(db.query("SELECT * FROM candidates WHERE id = %s", (candidate_id,), fetch='one'))
cand["skills"] = [dict(s) for s in db.query("SELECT * FROM skills WHERE candidate_id = %s", (candidate_id,))]
cand["experience"] = [dict(e) for e in db.query("SELECT * FROM experience WHERE candidate_id = %s", (candidate_id,))]
cand["education"] = [dict(e) for e in db.query("SELECT * FROM education WHERE candidate_id = %s", (candidate_id,))]
cand["certifications"] = [dict(c) for c in db.query("SELECT * FROM certifications WHERE candidate_id = %s", (candidate_id,))]
# Use realigned data if available, else original
realigned = item.get("realigned_cv_data")
if realigned and isinstance(realigned, dict) and realigned.get("realignment_suggestion"):
# For now, use original data — realignment is advisory only
pass
try:
async with httpx.AsyncClient(timeout=120) as client:
resp = await client.post(carbone_url, json={"candidateData": cand})
if resp.status_code == 200:
# Save the PDF
import tempfile
pdf_dir = os.path.join(str(static_dir), "generated")
os.makedirs(pdf_dir, exist_ok=True)
safe_name = f"{item['first_name']}_{item['last_name']}".replace(" ", "_")
filename = f"{batch['name'].replace(' ', '_')}_{safe_name}_{candidate_id[:8]}.pdf"
filepath = os.path.join(pdf_dir, filename)
with open(filepath, "wb") as f:
f.write(resp.content)
# Update batch item with path
db.execute(
"UPDATE batch_items SET generated_cv_path = %s, updated_at = NOW() WHERE id = %s",
(f"/static/generated/{filename}", item["id"])
)
results.append({"candidate": f"{item['first_name']} {item['last_name']}", "status": "ok", "file": filename})
else:
results.append({"candidate": f"{item['first_name']} {item['last_name']}", "status": "error", "error": resp.text[:200]})
except Exception as e:
results.append({"candidate": f"{item['first_name']} {item['last_name']}", "status": "error", "error": str(e)})
# Update batch status to exported
db.execute("UPDATE cv_batches SET status = 'exported', updated_at = NOW() WHERE id = %s", (batch_id,))
return {"success": True, "generated": len([r for r in results if r["status"] == "ok"]), "results": results}
# ============================================================
# SERVE FRONTEND
# ============================================================