Mise à jour API, scraper CIR et tests
Build & Deploy / build (push) Successful in 17s

This commit is contained in:
Mouigni
2026-08-16 17:13:43 +02:00
parent f8c8e16e7e
commit 742469b032
3 changed files with 195 additions and 132 deletions
+4 -12
View File
@@ -3,14 +3,17 @@ from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import text
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
from database.database import SessionLocal
from database.database import SessionLocal, Base, engine
from database.models import Document, ScraperRun, User
from api.auth import (
UserLogin, UserCreate, Token, UserResponse, RoleUpdate,
get_password_hash, verify_password,
create_access_token, decode_token, ACCESS_TOKEN_EXPIRE_MINUTES
)
Base.metadata.create_all(bind=engine)
app = FastAPI()
app.add_middleware(
@@ -21,7 +24,6 @@ app.add_middleware(
allow_headers=["*"],
)
# ── Fonction vérification admin ──────────────────────────────
def require_admin(token: str):
email = decode_token(token)
if not email:
@@ -33,7 +35,6 @@ def require_admin(token: str):
raise HTTPException(status_code=403, detail="Accès refusé")
return user
# ── Endpoints existants ──────────────────────────────────────
@app.get("/")
def root():
@@ -104,11 +105,9 @@ def get_documents():
db.close()
return result
# ── Migration colonne role ───────────────────────────────────
@app.post("/admin/migrate-add-role")
def migrate_add_role():
"""Endpoint temporaire — ajoute la colonne role si absente"""
db = SessionLocal()
try:
db.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR DEFAULT 'user'"))
@@ -119,15 +118,9 @@ def migrate_add_role():
finally:
db.close()
# ── Premier admin sans neckfire ──────────────────────────────
@app.post("/admin/make-admin")
def make_admin(token: str, target_email: str):
"""
Passe un user en admin.
Fonctionne sans auth si aucun admin n'existe encore.
Se désactive automatiquement si un admin existe déjà.
"""
db = SessionLocal()
existing_admins = db.query(User).filter(User.role == "admin").count()
@@ -149,7 +142,6 @@ def make_admin(token: str, target_email: str):
db.close()
return {"message": f"{target_email} est maintenant admin"}
# ── Endpoints admin ──────────────────────────────────────────
@app.get("/admin/stats")
def admin_stats(token: str):