mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
add serializer functions for track and album objects
This commit is contained in:
+22
-9
@@ -6,14 +6,16 @@ from dataclasses import asdict
|
||||
|
||||
from flask import Blueprint, request
|
||||
|
||||
from app.models import FavType, Track
|
||||
from app.serializers.track import track_serializer
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.tracks import TrackStore
|
||||
from app.db.sqlite.albums import SQLiteAlbumMethods as adb
|
||||
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
|
||||
from app.models import FavType, Track
|
||||
from app.utils.hashing import create_hash
|
||||
from app.utils.remove_duplicates import remove_duplicates
|
||||
|
||||
from app.store.tracks import TrackStore
|
||||
from app.store.albums import AlbumStore
|
||||
from app.utils.hashing import create_hash
|
||||
from app.serializers.album import serialize_for_card
|
||||
from app.utils.remove_duplicates import remove_duplicates
|
||||
|
||||
get_albums_by_albumartist = adb.get_albums_by_albumartist
|
||||
check_is_fav = favdb.check_is_favorite
|
||||
@@ -79,7 +81,10 @@ def get_album_tracks_and_info():
|
||||
|
||||
album.is_favorite = check_is_fav(albumhash, FavType.album)
|
||||
|
||||
return {"tracks": tracks, "info": album}
|
||||
return {
|
||||
"tracks": [track_serializer(t, retain_disc=True) for t in tracks],
|
||||
"info": album,
|
||||
}
|
||||
|
||||
|
||||
@api.route("/album/<albumhash>/tracks", methods=["GET"])
|
||||
@@ -92,9 +97,10 @@ def get_album_tracks(albumhash: str):
|
||||
|
||||
for t in tracks:
|
||||
track = str(t["track"]).zfill(3)
|
||||
t["pos"] = int(f"{t['disc']}{track}")
|
||||
t["_pos"] = int(f"{t['disc']}{track}")
|
||||
|
||||
tracks = sorted(tracks, key=lambda t: t["pos"])
|
||||
tracks = sorted(tracks, key=lambda t: t["_pos"])
|
||||
tracks = [track_serializer(t, _remove={"_pos"}) for t in tracks]
|
||||
|
||||
return {"tracks": tracks}
|
||||
|
||||
@@ -122,7 +128,14 @@ def get_artist_albums():
|
||||
for a in albumartists
|
||||
]
|
||||
|
||||
albums = [a for a in albums if len(a["albums"]) > 0]
|
||||
albums = [
|
||||
{
|
||||
"artisthash": a["artisthash"],
|
||||
"albums": [serialize_for_card(a_) for a_ in (a["albums"])],
|
||||
}
|
||||
for a in albums
|
||||
if len(a["albums"]) > 0
|
||||
]
|
||||
|
||||
return {"data": albums}
|
||||
|
||||
|
||||
+11
-8
@@ -7,6 +7,8 @@ from flask import Blueprint, request
|
||||
|
||||
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
|
||||
from app.models import Album, FavType, Track
|
||||
from app.serializers.album import serialize_for_card_many
|
||||
from app.serializers.track import serialize_tracks
|
||||
from app.utils.remove_duplicates import remove_duplicates
|
||||
|
||||
from app.store.albums import AlbumStore
|
||||
@@ -22,7 +24,7 @@ class CacheEntry:
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, artisthash: str, albumhashes: set[str], tracks: list[Track]
|
||||
self, artisthash: str, albumhashes: set[str], tracks: list[Track]
|
||||
) -> None:
|
||||
self.albums: list[Album] = []
|
||||
self.tracks: list[Track] = []
|
||||
@@ -208,7 +210,7 @@ def get_artist(artisthash: str):
|
||||
|
||||
artist.is_favorite = favdb.check_is_favorite(artisthash, FavType.artist)
|
||||
|
||||
return {"artist": artist, "tracks": tracks[:limit]}
|
||||
return {"artist": artist, "tracks": serialize_tracks(tracks[:limit])}
|
||||
|
||||
|
||||
@api.route("/artist/<artisthash>/albums", methods=["GET"])
|
||||
@@ -265,11 +267,11 @@ def get_artist_albums(artisthash: str):
|
||||
|
||||
return {
|
||||
"artistname": artist.name,
|
||||
"albums": albums[:limit],
|
||||
"singles": singles[:limit],
|
||||
"eps": eps[:limit],
|
||||
"appearances": appearances[:limit],
|
||||
"compilations": compilations[:limit]
|
||||
"albums": serialize_for_card_many(albums[:limit]),
|
||||
"singles": serialize_for_card_many(singles[:limit]),
|
||||
"eps": serialize_for_card_many(eps[:limit]),
|
||||
"appearances": serialize_for_card_many(appearances[:limit]),
|
||||
"compilations": serialize_for_card_many(compilations[:limit]),
|
||||
}
|
||||
|
||||
|
||||
@@ -280,7 +282,8 @@ def get_all_artist_tracks(artisthash: str):
|
||||
"""
|
||||
tracks = TrackStore.get_tracks_by_artist(artisthash)
|
||||
|
||||
return {"tracks": tracks}
|
||||
return {"tracks": serialize_tracks(tracks)}
|
||||
|
||||
|
||||
#
|
||||
# @api.route("/artist/<artisthash>/similar", methods=["GET"])
|
||||
|
||||
+14
-12
@@ -2,13 +2,17 @@ from flask import Blueprint, request
|
||||
|
||||
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
|
||||
from app.models import FavType
|
||||
from app.serializers.album import serialize_for_card_many
|
||||
from app.serializers.track import serialize_tracks
|
||||
from app.utils.bisection import UseBisection
|
||||
|
||||
from app.store.artists import ArtistStore
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.tracks import TrackStore
|
||||
from app.serializers.favorites_serializer import recent_fav_track_serializer, recent_fav_album_serializer, \
|
||||
recent_fav_artist_serializer
|
||||
from app.serializers.favorites_serializer import (
|
||||
recent_fav_album_serializer,
|
||||
recent_fav_artist_serializer,
|
||||
)
|
||||
|
||||
api = Blueprint("favorite", __name__, url_prefix="/")
|
||||
|
||||
@@ -80,7 +84,7 @@ def get_favorite_albums():
|
||||
if limit == 0:
|
||||
limit = len(albums)
|
||||
|
||||
return {"albums": fav_albums[:limit]}
|
||||
return {"albums": serialize_for_card_many(fav_albums[:limit])}
|
||||
|
||||
|
||||
@api.route("/tracks/favorite")
|
||||
@@ -103,7 +107,7 @@ def get_favorite_tracks():
|
||||
if limit == 0:
|
||||
limit = len(tracks)
|
||||
|
||||
return {"tracks": tracks[:limit]}
|
||||
return {"tracks": serialize_tracks(tracks[:limit])}
|
||||
|
||||
|
||||
@api.route("/artists/favorite")
|
||||
@@ -194,20 +198,18 @@ def get_all_favorites():
|
||||
if fav[2] == FavType.album:
|
||||
try:
|
||||
album = [a for a in albums if a.albumhash == fav[1]][0]
|
||||
recents.append({
|
||||
"type": "album",
|
||||
"item": recent_fav_album_serializer(album)
|
||||
})
|
||||
recents.append(
|
||||
{"type": "album", "item": recent_fav_album_serializer(album)}
|
||||
)
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
if fav[2] == FavType.artist:
|
||||
try:
|
||||
artist = [a for a in artists if a.artisthash == fav[1]][0]
|
||||
recents.append({
|
||||
"type": "artist",
|
||||
"item": recent_fav_artist_serializer(artist)
|
||||
})
|
||||
recents.append(
|
||||
{"type": "artist", "item": recent_fav_artist_serializer(artist)}
|
||||
)
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user