-
Notifications
You must be signed in to change notification settings - Fork 37
refactor(server): extract the artist-alias routes into routers/artist_aliases.py (R3) #838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """Artist aliases / Tidy-up (P4) — canonicalize messy artist tags at DISPLAY | ||
| ("ACDC" -> "AC/DC") without touching feedpak files or the scanner-derived | ||
| songs.artist. All DB-only. | ||
|
|
||
| Extracted verbatim from ``server.py`` (R3); only the decorator receiver | ||
| (``@app`` -> ``@router``) and the singleton read (``meta_db`` -> | ||
| ``appstate.meta_db``) changed. The read stays a module attribute so a re-imported | ||
| ``server`` re-publishes a fresh DB into the seam — see ``appstate.py``. | ||
| """ | ||
|
|
||
| from fastapi import APIRouter | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| import appstate | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.get("/api/artist-aliases") | ||
| def list_artist_aliases(): | ||
| """Existing raw→canonical overrides (the Tidy-up 'current merges' list).""" | ||
| return {"aliases": appstate.meta_db.list_artist_aliases()} | ||
|
|
||
|
|
||
| @router.get("/api/artists/raw") | ||
| def list_raw_artists(limit: int = 2000): | ||
| """Distinct RAW artist names + song counts + current canonical — the Tidy-up | ||
| picker (you merge raw variants into one canonical).""" | ||
| return {"artists": appstate.meta_db.raw_artists(limit)} | ||
|
|
||
|
|
||
| @router.post("/api/artist-aliases") | ||
| def set_artist_alias(data: dict): | ||
| """Upsert one override: {raw_name, canonical_name, mb_artist_id?}. A self-alias | ||
| (raw == canonical) clears the row instead (un-merge).""" | ||
| raw = (data.get("raw_name") or "").strip() | ||
| canon = (data.get("canonical_name") or "").strip() | ||
| if not raw or not canon: | ||
| return JSONResponse({"error": "raw_name and canonical_name are required"}, 400) | ||
| result = appstate.meta_db.set_artist_alias(raw, canon, (data.get("mb_artist_id") or None)) | ||
| if not result.get("ok"): | ||
| # Would form a cycle (raw → … → raw) — refuse rather than corrupt the chain. | ||
| return JSONResponse( | ||
| {"error": "alias would create a cycle", "raw_name": raw, "canonical_name": canon}, | ||
| 409) | ||
| return {"ok": True, "raw_name": raw, "canonical_name": result.get("canonical_name", canon)} | ||
|
|
||
|
|
||
| @router.post("/api/artist-aliases/merge") | ||
| def merge_artist_aliases(data: dict): | ||
| """Merge several raw artist variants into one canonical: | ||
| {raw_names: [...], canonical_name}. The canonical's own self-alias is skipped. | ||
| Returns {merged: N}.""" | ||
| canon = (data.get("canonical_name") or "").strip() | ||
| raws = data.get("raw_names") | ||
| if not canon: | ||
| return JSONResponse({"error": "canonical_name is required"}, 400) | ||
| if not isinstance(raws, list) or not raws: | ||
| return JSONResponse({"error": "raw_names must be a non-empty array"}, 400) | ||
| n = appstate.meta_db.merge_artists(raws, canon) | ||
| return {"merged": n, "canonical_name": canon} | ||
|
|
||
|
|
||
| @router.delete("/api/artist-aliases/{raw_name:path}") | ||
| def delete_artist_alias(raw_name: str): | ||
| """Remove one override so that raw artist stands on its own again.""" | ||
| appstate.meta_db.remove_artist_alias(raw_name) | ||
| return {"ok": True} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.