:feature: Mise en place de l'interface et de la configuration
This commit is contained in:
1 parent
6278d19618
commit
311ec74668
21 files changed
+1920
-231
No files matched your search
+106
-22
@@ -7,26 +7,43 @@ from typing import Annotated
|
||||
import httpx
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import inspect, select, text
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.conf_store import read_config, write_config
|
||||
from app.database import Base, engine, get_db
|
||||
from app.models import IsoMovie
|
||||
from app.radarr import RadarrClient, movie_file_path
|
||||
from app.schemas import (
|
||||
ApiServiceSettingsRead,
|
||||
QbittorrentSettingsRead,
|
||||
ConnectionResult,
|
||||
HealthRead,
|
||||
IsoMovieRead,
|
||||
RadarrSettingsRead,
|
||||
RadarrSettingsWrite,
|
||||
ScanResult,
|
||||
SettingsRead,
|
||||
SettingsWrite,
|
||||
)
|
||||
from app.settings_store import radarr_credentials, read_setting, write_setting
|
||||
from app.settings_store import radarr_credentials
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Very small "migration" helper for dev: add new columns if the SQLite DB already exists.
|
||||
# (SQLAlchemy create_all doesn't alter existing tables.)
|
||||
try:
|
||||
inspector = inspect(engine)
|
||||
columns = {col["name"] for col in inspector.get_columns("iso_movies")}
|
||||
if "processed_at" not in columns:
|
||||
with engine.begin() as conn:
|
||||
conn.execute(text("ALTER TABLE iso_movies ADD COLUMN processed_at DATETIME"))
|
||||
except Exception:
|
||||
# Don't fail app startup for a best-effort migration.
|
||||
pass
|
||||
yield
|
||||
|
||||
|
||||
@@ -41,11 +58,32 @@ app.add_middleware(
|
||||
DbSession = Annotated[Session, Depends(get_db)]
|
||||
|
||||
|
||||
def client_from_db(db: Session) -> RadarrClient:
|
||||
try:
|
||||
url, api_key = radarr_credentials(db)
|
||||
except ValueError as error:
|
||||
raise HTTPException(status_code=409, detail=str(error)) from error
|
||||
def _api_service_read(url: str | None, api_key: str | None) -> ApiServiceSettingsRead:
|
||||
return ApiServiceSettingsRead(url=url, api_key_configured=api_key is not None)
|
||||
|
||||
|
||||
def _settings_read() -> SettingsRead:
|
||||
cfg = read_config()
|
||||
return SettingsRead(
|
||||
radarr=_api_service_read(cfg.radarr.url, cfg.radarr.api_key),
|
||||
sonarr=_api_service_read(cfg.sonarr.url, cfg.sonarr.api_key),
|
||||
prowlarr=_api_service_read(cfg.prowlarr.url, cfg.prowlarr.api_key),
|
||||
jellyfin=_api_service_read(cfg.jellyfin.url, cfg.jellyfin.api_key),
|
||||
seerr=_api_service_read(cfg.seerr.url, cfg.seerr.api_key),
|
||||
qbittorrent=QbittorrentSettingsRead(
|
||||
url=cfg.qbittorrent.url,
|
||||
username=cfg.qbittorrent.username,
|
||||
password_configured=cfg.qbittorrent.password is not None,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def client_from_config() -> RadarrClient:
|
||||
cfg = read_config()
|
||||
url = (cfg.radarr.url or "").rstrip("/")
|
||||
api_key = cfg.radarr.api_key
|
||||
if not url or not api_key:
|
||||
raise HTTPException(status_code=409, detail="La connexion Radarr n'est pas configurée")
|
||||
return RadarrClient(url, api_key)
|
||||
|
||||
|
||||
@@ -61,30 +99,63 @@ def health() -> HealthRead:
|
||||
|
||||
|
||||
@app.get("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
def get_radarr_settings(db: DbSession) -> RadarrSettingsRead:
|
||||
return RadarrSettingsRead(
|
||||
url=read_setting(db, "radarr_url"),
|
||||
api_key_configured=read_setting(db, "radarr_api_key") is not None,
|
||||
)
|
||||
def get_radarr_settings() -> RadarrSettingsRead:
|
||||
cfg = read_config()
|
||||
return RadarrSettingsRead(url=cfg.radarr.url, api_key_configured=cfg.radarr.api_key is not None)
|
||||
|
||||
|
||||
@app.put("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
def put_radarr_settings(
|
||||
payload: RadarrSettingsWrite, db: DbSession
|
||||
payload: RadarrSettingsWrite,
|
||||
) -> RadarrSettingsRead:
|
||||
cfg = read_config()
|
||||
url = str(payload.url).rstrip("/")
|
||||
write_setting(db, "radarr_url", url)
|
||||
cfg.radarr.url = url
|
||||
if payload.api_key:
|
||||
write_setting(db, "radarr_api_key", payload.api_key)
|
||||
db.commit()
|
||||
return RadarrSettingsRead(
|
||||
url=url, api_key_configured=read_setting(db, "radarr_api_key") is not None
|
||||
)
|
||||
cfg.radarr.api_key = payload.api_key
|
||||
write_config(cfg)
|
||||
return RadarrSettingsRead(url=url, api_key_configured=cfg.radarr.api_key is not None)
|
||||
|
||||
|
||||
@app.get("/api/settings", response_model=SettingsRead)
|
||||
def get_settings() -> SettingsRead:
|
||||
return _settings_read()
|
||||
|
||||
|
||||
@app.put("/api/settings", response_model=SettingsRead)
|
||||
def put_settings(payload: SettingsWrite) -> SettingsRead:
|
||||
cfg = read_config()
|
||||
|
||||
def apply_service(target, patch):
|
||||
if patch is None:
|
||||
return
|
||||
if patch.url is not None:
|
||||
target.url = str(patch.url).rstrip("/")
|
||||
if patch.api_key:
|
||||
target.api_key = patch.api_key
|
||||
|
||||
apply_service(cfg.radarr, payload.radarr)
|
||||
apply_service(cfg.sonarr, payload.sonarr)
|
||||
apply_service(cfg.prowlarr, payload.prowlarr)
|
||||
apply_service(cfg.jellyfin, payload.jellyfin)
|
||||
apply_service(cfg.seerr, payload.seerr)
|
||||
|
||||
if payload.qbittorrent is not None:
|
||||
qb = payload.qbittorrent
|
||||
if qb.url is not None:
|
||||
cfg.qbittorrent.url = str(qb.url).rstrip("/")
|
||||
if qb.username:
|
||||
cfg.qbittorrent.username = qb.username
|
||||
if qb.password:
|
||||
cfg.qbittorrent.password = qb.password
|
||||
|
||||
write_config(cfg)
|
||||
return _settings_read()
|
||||
|
||||
|
||||
@app.post("/api/settings/radarr/test", response_model=ConnectionResult)
|
||||
async def test_radarr(db: DbSession) -> ConnectionResult:
|
||||
client = client_from_db(db)
|
||||
async def test_radarr() -> ConnectionResult:
|
||||
client = client_from_config()
|
||||
try:
|
||||
status = await client.system_status()
|
||||
except (httpx.HTTPError, ValueError) as error:
|
||||
@@ -100,9 +171,22 @@ def list_movies(db: DbSession) -> list[IsoMovie]:
|
||||
return list(db.scalars(select(IsoMovie).order_by(IsoMovie.title)).all())
|
||||
|
||||
|
||||
@app.get("/api/files", response_model=list[IsoMovieRead])
|
||||
def list_files(db: DbSession) -> list[IsoMovie]:
|
||||
return list_movies(db)
|
||||
|
||||
|
||||
@app.get("/api/files/{file_id}", response_model=IsoMovieRead)
|
||||
def get_file(file_id: int, db: DbSession) -> IsoMovie:
|
||||
item = db.get(IsoMovie, file_id)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="Fichier introuvable")
|
||||
return item
|
||||
|
||||
|
||||
@app.post("/api/scans", response_model=ScanResult)
|
||||
async def scan_radarr(db: DbSession) -> ScanResult:
|
||||
client = client_from_db(db)
|
||||
client = client_from_config()
|
||||
try:
|
||||
movies = await client.movies()
|
||||
except (httpx.HTTPError, ValueError) as error:
|
||||
|
||||
Reference in new issue
Block a user