diff --git a/app/api/favorites.py b/app/api/favorites.py index 01f2a4d..0c6f35d 100644 --- a/app/api/favorites.py +++ b/app/api/favorites.py @@ -2,17 +2,15 @@ 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.serializers.album import serialize_for_card, serialize_for_card_many +from app.serializers.artist import serialize_for_card as serialize_artist +from app.serializers.track import serialize_track, 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_album_serializer, - recent_fav_artist_serializer, -) + api = Blueprint("favorite", __name__, url_prefix="/") @@ -196,23 +194,28 @@ def get_all_favorites(): break 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)} - ) - except IndexError: + album = next((a for a in albums if a.albumhash == fav[1]), None) + + if album is None: continue + recents.append({"type": "album", "item": serialize_for_card(album)}) 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)} - ) - except IndexError: + artist = next((a for a in artists if a.artisthash == fav[1]), None) + + if artist is None: continue + recents.append({"type": "artist", "item": serialize_artist(artist)}) + + if fav[2] == FavType.track: + track = next((t for t in tracks if t.trackhash == fav[1]), None) + + if track is None: + continue + + recents.append({"type": "track", "item": serialize_track(track)}) + return { "recents": recents[:album_limit], "tracks": tracks[:track_limit], diff --git a/app/models/track.py b/app/models/track.py index 0e6811f..0cc9ef5 100644 --- a/app/models/track.py +++ b/app/models/track.py @@ -37,7 +37,6 @@ class Track: trackhash: str last_mod: str | int - filetype: str = "" image: str = "" artist_hashes: str = "" is_favorite: bool = False diff --git a/app/serializers/favorites_serializer.py b/app/serializers/favorites_serializer.py deleted file mode 100644 index c9f65e3..0000000 --- a/app/serializers/favorites_serializer.py +++ /dev/null @@ -1,40 +0,0 @@ -from app.models import Album, Artist, Track - - -def recent_fav_track_serializer(track: Track) -> dict: - """ - Simplifies a track object into a dictionary to remove unused - properties on the client. - """ - return { - "image": track.image, - "title": track.title, - "trackhash": track.trackhash, - "filepath": track.filepath, - } - - -def recent_fav_album_serializer(album: Album) -> dict: - """ - Simplifies an album object into a dictionary to remove unused - properties on the client. - """ - return { - "image": album.image, - "title": album.og_title, - "albumhash": album.albumhash, - "artist": album.albumartists[0].name, - "colors": album.colors, - } - - -def recent_fav_artist_serializer(artist: Artist) -> dict: - """ - Simplifies an artist object into a dictionary to remove unused - properties on the client. - """ - return { - "image": artist.image, - "name": artist.name, - "artisthash": artist.artisthash, - } diff --git a/app/serializers/track.py b/app/serializers/track.py index bb195f7..b812a64 100644 --- a/app/serializers/track.py +++ b/app/serializers/track.py @@ -15,6 +15,7 @@ def serialize_track(track: Track, to_remove: set = {}, remove_disc=True) -> dict "disc", "track", "artist_hashes", + "created_date", }.union(to_remove) if not remove_disc: