Spaces:
Running on Zero
Running on Zero
Download stemdeck/api/mixes.py from AnjaneyaTurai/stemdeck-gpu: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/spaces/AnjaneyaTurai/stemdeck-gpu/resolve/main/stemdeck/api/mixes.py
- Command line
-
hf download hf://spaces/AnjaneyaTurai/stemdeck-gpu/stemdeck/api/mixes.py
-
curl -L -o mixes.py https://huggingface.co/spaces/AnjaneyaTurai/stemdeck-gpu/resolve/main/stemdeck/api/mixes.py
2.86 kB
| """Saved mixes per song (only the requesting visitor's songs).""" | |
| from __future__ import annotations | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from fastapi.responses import Response | |
| from sqlmodel import Session, col, select | |
| from stemdeck.db import get_session | |
| from stemdeck.library import owned_song | |
| from stemdeck.models import Mix | |
| from stemdeck.schemas import MixIn, MixOut, MixUpdateIn, mix_out | |
| from stemdeck.settings import get_settings | |
| from stemdeck.visitors import current_owner | |
| router = APIRouter(tags=["mixes"]) | |
| MAX_MIXES_PER_SONG_PUBLIC = 30 | |
| def _mix_or_404(session: Session, song_id: str, mix_id: str, owner: str) -> Mix: | |
| owned_song(session, song_id, owner) | |
| mix = session.get(Mix, mix_id) | |
| if mix is None or mix.song_id != song_id: | |
| raise HTTPException(404, "Mix not found.") | |
| return mix | |
| def list_mixes(song_id: str, session: Session = Depends(get_session), | |
| owner: str = Depends(current_owner)) -> list[MixOut]: | |
| owned_song(session, song_id, owner) | |
| mixes = session.exec(select(Mix).where(Mix.song_id == song_id).order_by(col(Mix.created_at))).all() | |
| return [mix_out(m) for m in mixes] | |
| def create_mix(song_id: str, body: MixIn, session: Session = Depends(get_session), | |
| owner: str = Depends(current_owner)) -> MixOut: | |
| owned_song(session, song_id, owner) | |
| if get_settings().public: | |
| count = len(session.exec(select(Mix.id).where(Mix.song_id == song_id)).all()) | |
| if count >= MAX_MIXES_PER_SONG_PUBLIC: | |
| raise HTTPException(409, f"This song already has {count} saved mixes. Delete one first.") | |
| mix = Mix(song_id=song_id, name=body.name.strip(), settings=body.settings) | |
| session.add(mix) | |
| session.commit() | |
| session.refresh(mix) | |
| return mix_out(mix) | |
| def update_mix(song_id: str, mix_id: str, body: MixUpdateIn, session: Session = Depends(get_session), | |
| owner: str = Depends(current_owner)) -> MixOut: | |
| mix = _mix_or_404(session, song_id, mix_id, owner) | |
| if body.name is not None: | |
| mix.name = body.name.strip() | |
| if body.settings is not None: | |
| mix.settings = body.settings | |
| session.add(mix) | |
| session.commit() | |
| session.refresh(mix) | |
| return mix_out(mix) | |
| def delete_mix(song_id: str, mix_id: str, session: Session = Depends(get_session), | |
| owner: str = Depends(current_owner)) -> Response: | |
| session.delete(_mix_or_404(session, song_id, mix_id, owner)) | |
| session.commit() | |
| return Response(status_code=204) | |