32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
# modules/admin/api.py
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, Depends, Body # Import Body
|
|
from pydantic import BaseModel # Import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
from core.database import Base, get_db
|
|
from modules.auth.models import User, UserRole
|
|
from modules.auth.dependencies import admin_only
|
|
from .tasks import cleardb
|
|
|
|
router = APIRouter(prefix="/admin", tags=["admin"], dependencies=[Depends(admin_only)])
|
|
|
|
# Define a Pydantic model for the request body
|
|
class ClearDbRequest(BaseModel):
|
|
hard: bool
|
|
|
|
@router.get("/")
|
|
def read_admin():
|
|
return {"message": "Admin route"}
|
|
|
|
# Change to POST and use the request body model
|
|
@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 # Get 'hard' from the payload
|
|
cleardb.delay(hard)
|
|
return {"message": "Clearing database in the background", "hard": hard}
|