feat: causal lab - bouton Mettre a jour + PATCH endpoint

- TabEditor: updateExisting() + bouton bleu visible si templateId set
- Bouton Creer renomme en copie pour clarifier
- PATCH /api/causal-lab/template/{id} pour graph_json + metadonnees

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OpenSquared
2026-06-28 10:02:28 +02:00
parent e8fb7c13aa
commit dd31f92aaf
2 changed files with 133 additions and 31 deletions

View File

@@ -402,6 +402,41 @@ def create_template(body: CreateTemplateRequest):
raise HTTPException(500, str(e))
@router.patch("/api/causal-lab/template/{template_id}")
def patch_template(template_id: int, body: dict):
"""Met à jour un template existant en entier (graph_json, métadonnées, coefficients)."""
try:
from services.database import get_conn
from services.causal_graphs import get_template
conn = get_conn()
tmpl = get_template(conn, template_id)
if not tmpl:
conn.close(); raise HTTPException(404, "Template introuvable")
sets, params = [], []
for field in ("name", "category", "sub_type", "description", "ai_rationale"):
if field in body:
sets.append(f"{field}=?"); params.append(body[field])
if "instruments" in body:
sets.append("instruments=?"); params.append(json.dumps(body["instruments"]))
if "graph_json" in body:
sets.append("graph_json=?"); params.append(json.dumps(body["graph_json"]))
if not sets:
conn.close(); return {"ok": True}
sets.append("updated_at=datetime('now')")
params.append(template_id)
conn.execute(f"UPDATE causal_graph_templates SET {', '.join(sets)} WHERE id=?", params)
conn.commit(); conn.close()
return {"ok": True}
except HTTPException:
raise
except Exception as e:
logger.error(f"[causal_lab] patch_template {template_id}: {e}")
raise HTTPException(500, str(e))
@router.delete("/api/causal-lab/template/{template_id}")
def delete_template(template_id: int):
"""Supprime un template utilisateur (les templates system sont protégés)."""