Files
alembic/app/routers/library.py
T
andrew c87ddc2649 Dashboard detail, library filtering/pagination, simplified playlist times,
job descriptions

Dashboard: format breakdown (FLAC/MP3/etc. counts), approximate library
size (bitrate*length/8, same approximation `beet stats` itself uses --
verified to match its "100.5 GiB" output exactly against the real
library), and a credential auth-state summary per scope (configured y/n
plus, for Bandcamp, cookie expiry parsed locally from the stored cookie
jar -- deliberately not a live network probe like pipeline-status.sh's
Qobuz check, since this renders on every dashboard load).

Library: was one unfiltered page dumping all 4072 tracks. Added search
(artist/title/album), playlist and format filter dropdowns, and real
SQL-level pagination (LIMIT/OFFSET, not fetch-everything-then-slice).
beets_service gained count_items()/distinct_formats() to support this.

Playlists: cron_expr is still the stored/scheduled representation, but the
UI now shows and edits a plain daily time picker instead of raw cron
syntax -- every playlist schedule today is a simple daily HH:MM anyway.
playlist_service.cron_to_time()/time_to_cron() convert at the router
boundary; verified round-trip against all real playlist cron values.

Jobs: each maintenance job now shows a short one-line description of what
it actually does (MAINTENANCE_JOB_DESCRIPTIONS), and "next run" is
formatted consistently with playlists' time style (HH:MM, with a day
qualifier for non-today runs -- maintenance jobs can be weekly/monthly,
unlike playlists' plain daily schedule).

Verified end-to-end against the real production data (4072 tracks, 100.5
GiB, real credentials): stats/format-breakdown/search/pagination all
correct, all 7 credential scopes report configured with Bandcamp's real
cookie expiry (325 days), and a full authenticated page-render sweep
(dashboard, library with every filter combination, playlist detail,
jobs) all returned 200 with no template errors.
2026-07-08 15:39:22 -06:00

104 lines
3.3 KiB
Python

from fastapi import APIRouter, Depends, Form, HTTPException, Request
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates
from app.db import get_db
from app.security.deps import require_auth
from app.services import beets_service, genre_fix, library_edit, retag
router = APIRouter(prefix="/library", tags=["library"])
templates = Jinja2Templates(directory="app/templates")
PAGE_SIZE = 50
@router.get("")
async def library_index(
request: Request,
grouping: str | None = None,
format: str | None = None,
q: str | None = None,
page: int = 1,
user: dict = Depends(require_auth),
):
page = max(1, page)
offset = (page - 1) * PAGE_SIZE
items = beets_service.query_items(grouping=grouping, search=q, format=format, limit=PAGE_SIZE, offset=offset)
total = beets_service.count_items(grouping=grouping, search=q, format=format)
total_pages = max(1, (total + PAGE_SIZE - 1) // PAGE_SIZE)
stats = beets_service.stats()
return templates.TemplateResponse(
request,
"library/index.html",
{
"items": items,
"grouping": grouping or "",
"format": format or "",
"q": q or "",
"page": page,
"total_pages": total_pages,
"total": total,
"all_formats": beets_service.distinct_formats(),
"all_groupings": stats["groupings"],
},
)
@router.get("/track/{item_id}")
async def track_detail(request: Request, item_id: int, user: dict = Depends(require_auth)):
item = beets_service.get_item(item_id)
if item is None:
raise HTTPException(404, "no such track")
return templates.TemplateResponse(
request,
"library/track_detail.html",
{"item": item, "editable_fields": library_edit.EDITABLE_FIELDS},
)
@router.post("/track/{item_id}")
async def update_track(
request: Request,
item_id: int,
user: dict = Depends(require_auth),
db=Depends(get_db),
):
form = await request.form()
changes = {
field: value
for field, value in form.items()
if field in library_edit.EDITABLE_FIELDS and value != ""
}
changed_by = user.get("email") or user.get("sub", "unknown")
changed = await library_edit.update_track_fields(db, item_id, changes, changed_by)
return RedirectResponse(url=f"/library/track/{item_id}?changed={len(changed)}", status_code=303)
@router.post("/track/{item_id}/genre")
async def set_genre(
item_id: int,
artist: str = Form(...),
genre: str = Form(...),
user: dict = Depends(require_auth),
):
changed_by = user.get("email") or user.get("sub", "unknown")
await genre_fix.set_artist_genre(artist, genre, changed_by)
return RedirectResponse(url=f"/library/track/{item_id}", status_code=303)
@router.post("/track/{item_id}/retag")
async def retag_track(
item_id: int,
url: str = Form(...),
keep_genre: bool = Form(False),
user: dict = Depends(require_auth),
):
item = beets_service.get_item(item_id)
if item is None:
raise HTTPException(404, "no such track")
changed_by = user.get("email") or user.get("sub", "unknown")
await retag.retag_from_url(item["path"], url, changed_by, keep_genre=keep_genre)
return RedirectResponse(url=f"/library/track/{item_id}", status_code=303)