Add backend support for settings
This commit is contained in:
@@ -3,8 +3,8 @@ from fastapi import FastAPI
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from model import Log, SessionLocal, Script
|
||||
from run_scripts import run_scripts
|
||||
from model import Log, SessionLocal, Script, Settings
|
||||
from run_scripts import run_scripts, update_requirements, update_environment
|
||||
import uvicorn
|
||||
|
||||
app = FastAPI()
|
||||
@@ -52,6 +52,76 @@ def hello():
|
||||
return {"message": "Welcome to the Project Monitor API"}
|
||||
|
||||
|
||||
# Define Pydantic models for Settings
|
||||
class SettingsBase(BaseModel):
|
||||
requirements: str
|
||||
environment: str
|
||||
user: str
|
||||
|
||||
|
||||
class SettingsUpdate(SettingsBase):
|
||||
pass
|
||||
|
||||
|
||||
class SettingsResponse(SettingsBase):
|
||||
id: int
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# Settings API Endpoints
|
||||
@app.get("/settings", response_model=list[SettingsResponse])
|
||||
def read_settings():
|
||||
db = SessionLocal()
|
||||
settings = db.query(Settings).all()
|
||||
db.close()
|
||||
return settings
|
||||
|
||||
|
||||
@app.post("/settings", response_model=SettingsResponse)
|
||||
def create_setting(settings: SettingsBase):
|
||||
db = SessionLocal()
|
||||
new_setting = Settings(**settings.model_dump())
|
||||
db.add(new_setting)
|
||||
db.commit()
|
||||
db.refresh(new_setting)
|
||||
db.close()
|
||||
|
||||
return new_setting
|
||||
|
||||
|
||||
@app.get("/settings/{settings_id}", response_model=SettingsResponse)
|
||||
def read_setting(settings_id: int):
|
||||
db = SessionLocal()
|
||||
setting = db.query(Settings).filter(Settings.id == settings_id).first()
|
||||
db.close()
|
||||
if not setting:
|
||||
raise HTTPException(status_code=404, detail="Setting not found")
|
||||
return setting
|
||||
|
||||
|
||||
@app.put("/settings/{settings_id}", response_model=SettingsResponse)
|
||||
def update_setting(settings_id: int, settings: SettingsUpdate):
|
||||
db = SessionLocal()
|
||||
existing_setting = db.query(Settings).filter(Settings.id == settings_id).first()
|
||||
if not existing_setting:
|
||||
raise HTTPException(status_code=404, detail="Setting not found")
|
||||
|
||||
if existing_setting.requirements != settings.requirements:
|
||||
existing_setting.requirements = settings.requirements
|
||||
update_requirements(settings)
|
||||
|
||||
if existing_setting.environment != settings.environment:
|
||||
existing_setting.environment = settings.environment
|
||||
update_environment(settings)
|
||||
|
||||
db.commit()
|
||||
db.refresh(existing_setting)
|
||||
db.close()
|
||||
|
||||
return existing_setting
|
||||
|
||||
|
||||
@app.get("/script", response_model=list[ScriptResponse])
|
||||
def read_scripts():
|
||||
db = SessionLocal()
|
||||
|
||||
Reference in New Issue
Block a user