- AI chat interface with file uploads - Admin panel for managing OpenAI/Ollama endpoints - User authentication with JWT - SQLite database backend - SvelteKit frontend with dark theme
207 lines
6.1 KiB
Python
207 lines
6.1 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.core.database import get_db
|
|
from app.core.auth import get_current_admin_user, get_password_hash
|
|
from app.models.models import User, AIEndpoint, ChatMessage, UploadedFile
|
|
from app.schemas.schemas import (
|
|
UserResponse,
|
|
UserUpdate,
|
|
AIEndpointCreate,
|
|
AIEndpointUpdate,
|
|
AIEndpointResponse,
|
|
AdminStats
|
|
)
|
|
|
|
router = APIRouter(prefix="/admin", tags=["Admin"])
|
|
|
|
|
|
# User Management
|
|
@router.get("/users", response_model=List[UserResponse])
|
|
def list_users(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
users = db.query(User).offset(skip).limit(limit).all()
|
|
return users
|
|
|
|
|
|
@router.get("/users/{user_id}", response_model=UserResponse)
|
|
def get_user(
|
|
user_id: int,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
return user
|
|
|
|
|
|
@router.put("/users/{user_id}", response_model=UserResponse)
|
|
def update_user(
|
|
user_id: int,
|
|
user_data: UserUpdate,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
if user_data.email:
|
|
existing_user = db.query(User).filter(
|
|
User.email == user_data.email,
|
|
User.id != user_id
|
|
).first()
|
|
if existing_user:
|
|
raise HTTPException(status_code=400, detail="Email already registered")
|
|
user.email = user_data.email
|
|
|
|
if user_data.username:
|
|
existing_user = db.query(User).filter(
|
|
User.username == user_data.username,
|
|
User.id != user_id
|
|
).first()
|
|
if existing_user:
|
|
raise HTTPException(status_code=400, detail="Username already taken")
|
|
user.username = user_data.username
|
|
|
|
if user_data.password:
|
|
user.hashed_password = get_password_hash(user_data.password)
|
|
|
|
if user_data.role:
|
|
user.role = user_data.role
|
|
|
|
if user_data.is_active is not None:
|
|
user.is_active = user_data.is_active
|
|
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user
|
|
|
|
|
|
@router.delete("/users/{user_id}")
|
|
def delete_user(
|
|
user_id: int,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
if user.id == current_user.id:
|
|
raise HTTPException(status_code=400, detail="Cannot delete yourself")
|
|
|
|
db.delete(user)
|
|
db.commit()
|
|
return {"message": "User deleted successfully"}
|
|
|
|
|
|
# AI Endpoint Management
|
|
@router.get("/endpoints", response_model=List[AIEndpointResponse])
|
|
def list_endpoints(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
endpoints = db.query(AIEndpoint).offset(skip).limit(limit).all()
|
|
return endpoints
|
|
|
|
|
|
@router.post("/endpoints", response_model=AIEndpointResponse)
|
|
def create_endpoint(
|
|
endpoint_data: AIEndpointCreate,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
# If this is set as default, unset other defaults
|
|
if endpoint_data.is_default:
|
|
db.query(AIEndpoint).filter(AIEndpoint.is_default == True).update({"is_default": False})
|
|
|
|
new_endpoint = AIEndpoint(**endpoint_data.model_dump())
|
|
db.add(new_endpoint)
|
|
db.commit()
|
|
db.refresh(new_endpoint)
|
|
return new_endpoint
|
|
|
|
|
|
@router.get("/endpoints/{endpoint_id}", response_model=AIEndpointResponse)
|
|
def get_endpoint(
|
|
endpoint_id: int,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
endpoint = db.query(AIEndpoint).filter(AIEndpoint.id == endpoint_id).first()
|
|
if not endpoint:
|
|
raise HTTPException(status_code=404, detail="Endpoint not found")
|
|
return endpoint
|
|
|
|
|
|
@router.put("/endpoints/{endpoint_id}", response_model=AIEndpointResponse)
|
|
def update_endpoint(
|
|
endpoint_id: int,
|
|
endpoint_data: AIEndpointUpdate,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
endpoint = db.query(AIEndpoint).filter(AIEndpoint.id == endpoint_id).first()
|
|
if not endpoint:
|
|
raise HTTPException(status_code=404, detail="Endpoint not found")
|
|
|
|
update_data = endpoint_data.model_dump(exclude_unset=True)
|
|
|
|
# If setting as default, unset other defaults
|
|
if update_data.get("is_default"):
|
|
db.query(AIEndpoint).filter(
|
|
AIEndpoint.is_default == True,
|
|
AIEndpoint.id != endpoint_id
|
|
).update({"is_default": False})
|
|
|
|
for key, value in update_data.items():
|
|
setattr(endpoint, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(endpoint)
|
|
return endpoint
|
|
|
|
|
|
@router.delete("/endpoints/{endpoint_id}")
|
|
def delete_endpoint(
|
|
endpoint_id: int,
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
endpoint = db.query(AIEndpoint).filter(AIEndpoint.id == endpoint_id).first()
|
|
if not endpoint:
|
|
raise HTTPException(status_code=404, detail="Endpoint not found")
|
|
|
|
db.delete(endpoint)
|
|
db.commit()
|
|
return {"message": "Endpoint deleted successfully"}
|
|
|
|
|
|
# Admin Statistics
|
|
@router.get("/stats", response_model=AdminStats)
|
|
def get_stats(
|
|
current_user = Depends(get_current_admin_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
total_users = db.query(User).count()
|
|
total_endpoints = db.query(AIEndpoint).count()
|
|
total_messages = db.query(ChatMessage).count()
|
|
total_files = db.query(UploadedFile).count()
|
|
active_users = db.query(User).filter(User.is_active == True).count()
|
|
|
|
return AdminStats(
|
|
total_users=total_users,
|
|
total_endpoints=total_endpoints,
|
|
total_messages=total_messages,
|
|
total_files=total_files,
|
|
active_users=active_users
|
|
)
|