Added admin page
This commit is contained in:
Binary file not shown.
@@ -1,6 +1,7 @@
|
||||
# modules/admin/api.py
|
||||
from typing import Annotated
|
||||
from fastapi import APIRouter, Depends
|
||||
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
|
||||
@@ -9,29 +10,37 @@ from modules.auth.dependencies import admin_only
|
||||
|
||||
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"}
|
||||
|
||||
@router.get("/cleardb")
|
||||
def clear_db(db: Annotated[Session, Depends(get_db)], hard: bool):
|
||||
# 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.
|
||||
'hard' parameter determines if the database should be completely reset.
|
||||
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
|
||||
if hard:
|
||||
# ... existing hard clear logic ...
|
||||
Base.metadata.drop_all(bind=db.get_bind())
|
||||
Base.metadata.create_all(bind=db.get_bind())
|
||||
db.commit()
|
||||
return {"message": "Database reset (HARD)"}
|
||||
else:
|
||||
# ... existing soft clear logic ...
|
||||
tables = Base.metadata.tables.keys()
|
||||
for table_name in tables:
|
||||
# delete all tables that isn't the users table
|
||||
if table_name != "users":
|
||||
table = Base.metadata.tables[table_name]
|
||||
print(f"Deleting table: {table_name}")
|
||||
db.execute(table.delete())
|
||||
|
||||
# delete all non-admin accounts
|
||||
db.query(User).filter(User.role != UserRole.ADMIN).delete()
|
||||
db.commit()
|
||||
return {"message": "Database cleared"}
|
||||
Binary file not shown.
Reference in New Issue
Block a user