32 lines
899 B
Python
32 lines
899 B
Python
# modules/admin/api.py
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from modules.auth.dependencies import admin_only
|
|
from .tasks import cleardb
|
|
|
|
router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])
|
|
|
|
|
|
class ClearDbRequest(BaseModel):
|
|
hard: bool
|
|
|
|
|
|
@router.get("/")
|
|
def read_admin():
|
|
return {"message": "Admin route"}
|
|
|
|
|
|
@router.post("/cleardb")
|
|
def clear_db(payload: ClearDbRequest, db: Annotated[Session, Depends(get_db)]):
|
|
"""
|
|
Clear the database based on the 'hard' flag in the request body.
|
|
'hard'=True: Drop and recreate all tables.
|
|
'hard'=False: Delete data from tables except users.
|
|
"""
|
|
hard = payload.hard
|
|
cleardb.delay(hard)
|
|
return {"message": "Clearing database in the background", "hard": hard}
|