-
Notifications
You must be signed in to change notification settings - Fork 994
Expand file tree
/
Copy pathsettingsroute.py
More file actions
89 lines (71 loc) · 2.88 KB
/
Copy pathsettingsroute.py
File metadata and controls
89 lines (71 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# api/routes/settings.py
import os
import yaml
from typing import Dict
from fastapi import APIRouter, Depends, HTTPException, Request
from ...datamodel import Settings
from ..deps import get_db
router = APIRouter()
def _get_authenticated_user_id(request: Request) -> str:
user_id = getattr(request.state, "user_id", None)
if not user_id:
raise HTTPException(status_code=401, detail="Unauthorized")
return user_id
def _get_or_create_settings(user_id: str, db) -> Settings:
"""Get existing settings or create default ones for user_id"""
response = db.get(Settings, filters={"user_id": user_id})
if response.status and response.data:
return response.data[0]
# Create default settings
default_settings = Settings(user_id=user_id)
upsert_response = db.upsert(default_settings)
if not upsert_response.status:
raise HTTPException(status_code=500, detail=upsert_response.message)
return upsert_response.data
@router.get("/")
async def get_settings(request: Request, db=Depends(get_db)) -> Dict:
try:
user_id = _get_authenticated_user_id(request)
settings = _get_or_create_settings(user_id, db)
return {"status": True, "data": settings}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
@router.put("/")
async def update_settings(settings: Settings, request: Request, db=Depends(get_db)) -> Dict:
try:
user_id = _get_authenticated_user_id(request)
settings.user_id = user_id
# Get existing settings to preserve the id
existing = _get_or_create_settings(user_id, db)
settings.id = existing.id if hasattr(existing, "id") else existing["id"]
response = db.upsert(settings)
if not response.status:
raise HTTPException(status_code=400, detail=response.message)
return {"status": True, "data": response.data}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
@router.get("/config-info")
async def get_config_info() -> Dict:
"""Get information about whether the app was started with a config file"""
config_file = os.environ.get("_CONFIG")
has_config_file = config_file is not None
config_content = None
if has_config_file and config_file:
try:
with open(config_file, "r") as f:
config_content = yaml.safe_load(f)
except Exception as e:
# If we can't read the config file, just log the error but don't fail
print(f"Warning: Could not read config file {config_file}: {e}")
return {
"status": True,
"data": {
"has_config_file": has_config_file,
"config_file_path": config_file if has_config_file else None,
"config_content": config_content,
},
}