first commit
This commit is contained in:
78
backend/app/api/routes/settings.py
Normal file
78
backend/app/api/routes/settings.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_user, get_db
|
||||
from app.core.security import verify_password
|
||||
from app.models.settings import GlobalSettings
|
||||
from app.models.user import User
|
||||
from app.schemas.settings import (
|
||||
RevealSshKeyRequest,
|
||||
RevealSshKeyResponse,
|
||||
SchedulerStatusResponse,
|
||||
SettingsResponse,
|
||||
SettingsUpdate,
|
||||
)
|
||||
from app.services.notification_service import notification_service
|
||||
from app.services.scheduler import scheduler_service
|
||||
from app.services.settings_service import settings_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def serialize_settings(settings: GlobalSettings) -> SettingsResponse:
|
||||
payload = SettingsResponse.model_validate(settings, from_attributes=True).model_dump()
|
||||
payload['global_ssh_key'] = None
|
||||
payload['has_global_ssh_key'] = bool((settings.global_ssh_key or '').strip())
|
||||
payload['has_default_switchos_credentials'] = bool(
|
||||
(settings.default_switchos_username or '').strip() or (settings.default_switchos_password or '').strip()
|
||||
)
|
||||
return SettingsResponse.model_validate(payload)
|
||||
|
||||
|
||||
@router.get('', response_model=SettingsResponse)
|
||||
def get_settings(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
_ = current_user
|
||||
settings = settings_service.get_or_create(db)
|
||||
return serialize_settings(settings)
|
||||
|
||||
|
||||
@router.get('/scheduler-status', response_model=SchedulerStatusResponse)
|
||||
def get_scheduler_status(current_user: User = Depends(get_current_user)):
|
||||
_ = current_user
|
||||
return scheduler_service.scheduler_status()
|
||||
|
||||
|
||||
@router.put('', response_model=SettingsResponse)
|
||||
def update_settings(payload: SettingsUpdate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
_ = current_user
|
||||
settings = settings_service.update(db, payload)
|
||||
scheduler_service.reschedule()
|
||||
return serialize_settings(settings)
|
||||
|
||||
|
||||
@router.post('/reveal-ssh-key', response_model=RevealSshKeyResponse)
|
||||
def reveal_ssh_key(
|
||||
payload: RevealSshKeyRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if not verify_password(payload.password, current_user.password_hash):
|
||||
raise HTTPException(status_code=400, detail='Current password is invalid')
|
||||
settings = settings_service.get_or_create(db)
|
||||
return {'global_ssh_key': settings.global_ssh_key}
|
||||
|
||||
|
||||
@router.post('/test-email')
|
||||
def test_email(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
_ = current_user
|
||||
settings = settings_service.get_or_create(db)
|
||||
notification_service.send_test_email(settings)
|
||||
return {'message': 'Test email sent'}
|
||||
|
||||
|
||||
@router.post('/test-pushover')
|
||||
def test_pushover(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
_ = current_user
|
||||
settings = settings_service.get_or_create(db)
|
||||
notification_service.send_test_pushover(settings)
|
||||
return {'message': 'Test pushover sent'}
|
||||
Reference in New Issue
Block a user