feat: update user preferences and system settings management
This commit is contained in:
@@ -14,7 +14,7 @@ from core.security import (
|
||||
decode_access_token
|
||||
)
|
||||
from db.database import get_db
|
||||
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, update_user_aliyun_key, get_user_preferences, update_user_preferences
|
||||
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, update_user_aliyun_key, get_user_preferences, update_user_preferences, is_local_model_enabled
|
||||
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyUpdate, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["authentication"])
|
||||
@@ -246,7 +246,17 @@ async def get_preferences(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
prefs = get_user_preferences(db, current_user.id)
|
||||
return UserPreferencesResponse(**prefs)
|
||||
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
available_backends = ["aliyun"]
|
||||
if local_enabled or current_user.is_superuser:
|
||||
available_backends.append("local")
|
||||
|
||||
return {
|
||||
"default_backend": prefs.get("default_backend", "aliyun"),
|
||||
"onboarding_completed": prefs.get("onboarding_completed", False),
|
||||
"available_backends": available_backends
|
||||
}
|
||||
|
||||
@router.put("/preferences")
|
||||
@limiter.limit("10/minute")
|
||||
@@ -256,10 +266,24 @@ async def update_preferences(
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
user = update_user_preferences(db, current_user.id, preferences.dict())
|
||||
if not user:
|
||||
if preferences.default_backend == "local":
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
if not local_enabled and not current_user.is_superuser:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
updated_user = update_user_preferences(
|
||||
db,
|
||||
current_user.id,
|
||||
preferences.model_dump()
|
||||
)
|
||||
|
||||
if not updated_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
return {"message": "Preferences updated"}
|
||||
|
||||
return {"message": "Preferences updated successfully"}
|
||||
|
||||
@@ -285,14 +285,27 @@ async def create_custom_voice_job(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from core.security import decrypt_api_key
|
||||
from db.crud import get_user_preferences, is_local_model_enabled
|
||||
|
||||
backend_type = req_data.backend or settings.DEFAULT_BACKEND
|
||||
if backend_type == "aliyun":
|
||||
if not current_user.aliyun_api_key:
|
||||
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
|
||||
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
|
||||
if not user_api_key:
|
||||
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
|
||||
user_prefs = get_user_preferences(db, current_user.id)
|
||||
preferred_backend = user_prefs.get("default_backend", "aliyun")
|
||||
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
can_use_local = local_enabled or current_user.is_superuser
|
||||
|
||||
backend_type = req_data.backend if hasattr(req_data, 'backend') and req_data.backend else preferred_backend
|
||||
|
||||
if backend_type == "local" and not can_use_local:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
|
||||
try:
|
||||
validate_text_length(req_data.text)
|
||||
@@ -362,14 +375,27 @@ async def create_voice_design_job(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from core.security import decrypt_api_key
|
||||
from db.crud import get_user_preferences, is_local_model_enabled
|
||||
|
||||
backend_type = req_data.backend or settings.DEFAULT_BACKEND
|
||||
if backend_type == "aliyun":
|
||||
if not current_user.aliyun_api_key:
|
||||
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
|
||||
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
|
||||
if not user_api_key:
|
||||
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
|
||||
user_prefs = get_user_preferences(db, current_user.id)
|
||||
preferred_backend = user_prefs.get("default_backend", "aliyun")
|
||||
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
can_use_local = local_enabled or current_user.is_superuser
|
||||
|
||||
backend_type = req_data.backend if hasattr(req_data, 'backend') and req_data.backend else preferred_backend
|
||||
|
||||
if backend_type == "local" and not can_use_local:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
|
||||
try:
|
||||
validate_text_length(req_data.text)
|
||||
@@ -450,14 +476,27 @@ async def create_voice_clone_job(
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from core.security import decrypt_api_key
|
||||
from db.crud import get_user_preferences, is_local_model_enabled
|
||||
|
||||
backend_type = backend or settings.DEFAULT_BACKEND
|
||||
if backend_type == "aliyun":
|
||||
if not current_user.aliyun_api_key:
|
||||
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
|
||||
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
|
||||
if not user_api_key:
|
||||
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
|
||||
user_prefs = get_user_preferences(db, current_user.id)
|
||||
preferred_backend = user_prefs.get("default_backend", "aliyun")
|
||||
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
can_use_local = local_enabled or current_user.is_superuser
|
||||
|
||||
backend_type = backend if backend else preferred_backend
|
||||
|
||||
if backend_type == "local" and not can_use_local:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
|
||||
try:
|
||||
validate_text_length(text)
|
||||
|
||||
@@ -15,9 +15,12 @@ from db.crud import (
|
||||
list_users,
|
||||
create_user_by_admin,
|
||||
update_user,
|
||||
delete_user
|
||||
delete_user,
|
||||
get_system_setting,
|
||||
update_system_setting,
|
||||
is_local_model_enabled
|
||||
)
|
||||
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse
|
||||
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse, SystemSettingsUpdate, SystemSettingsResponse
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
limiter = Limiter(key_func=get_remote_address)
|
||||
@@ -167,3 +170,42 @@ async def delete_user_by_id(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
@router.get("/system/settings", response_model=SystemSettingsResponse)
|
||||
async def get_system_settings(
|
||||
current_user: Annotated[User, Depends(require_superuser)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
local_enabled = is_local_model_enabled(db)
|
||||
return {"local_model_enabled": local_enabled}
|
||||
|
||||
@router.put("/system/settings")
|
||||
async def update_system_settings(
|
||||
settings: SystemSettingsUpdate,
|
||||
current_user: Annotated[User, Depends(require_superuser)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from db.models import User
|
||||
from datetime import datetime
|
||||
|
||||
update_system_setting(db, "local_model_enabled", {"enabled": settings.local_model_enabled})
|
||||
|
||||
if not settings.local_model_enabled:
|
||||
users = db.query(User).filter(User.is_superuser == False).all()
|
||||
migrated_count = 0
|
||||
|
||||
for user in users:
|
||||
prefs = user.user_preferences or {}
|
||||
if prefs.get("default_backend") == "local":
|
||||
prefs["default_backend"] = "aliyun"
|
||||
user.user_preferences = prefs
|
||||
user.updated_at = datetime.utcnow()
|
||||
migrated_count += 1
|
||||
|
||||
db.commit()
|
||||
return {
|
||||
"message": "System settings updated",
|
||||
"users_migrated": migrated_count
|
||||
}
|
||||
|
||||
return {"message": "System settings updated", "users_migrated": 0}
|
||||
|
||||
Reference in New Issue
Block a user