fix: album favorite state, artist and album colors

+ fix: unserialized artist result
+ misc
This commit is contained in:
cwilvx
2024-08-02 12:25:55 +03:00
parent 16db3e1ad2
commit 0463c80070
16 changed files with 195 additions and 165 deletions
+2 -8
View File
@@ -3,7 +3,6 @@ Contains all the album routes.
"""
from dataclasses import asdict
from pprint import pprint
import random
from pydantic import BaseModel, Field
@@ -39,7 +38,6 @@ def get_album_tracks_and_info(body: AlbumHashSchema):
Returns album info and tracks for the given albumhash.
"""
albumhash = body.albumhash
# album = AlbumDb.get_album_by_albumhash(albumhash)
albumentry = AlbumStore.albummap.get(albumhash)
if albumentry is None:
@@ -53,14 +51,9 @@ def get_album_tracks_and_info(body: AlbumHashSchema):
tracks=tracks, singleTrackAsSingle=UserConfig().showAlbumsAsSingles
)
print("is_favorite", album.is_favorite)
track_total = sum({int(t.extra.get("track_total", 1) or 1) for t in tracks})
avg_bitrate = sum(t.bitrate for t in tracks) // (len(tracks) or 1)
album.fav_userids = [1]
pprint(album)
return {
"info": {
**asdict(album),
@@ -128,10 +121,11 @@ def get_more_from_artist(body: GetMoreFromArtistsBody):
a
for a in albums
# INFO: filter out albums added to other artists
if a.albumhash not in seen_hashes
if a.albumhash not in seen_hashes and artisthash in a.artisthashes
# INFO: filter out albums with the same base title
and create_hash(a.base_title) != create_hash(base_title)
]
all_albums[artisthash] = serialize_for_card_many(
[a for a in albums if create_hash(a.base_title) != create_hash(base_title)][
:limit
+13 -3
View File
@@ -21,7 +21,7 @@ from app.config import UserConfig
from app.db.userdata import SimilarArtistTable
from app.serializers.album import serialize_for_card_many
from app.serializers.artist import serialize_for_cards
from app.serializers.artist import serialize_for_cards, serialize_for_card
from app.serializers.track import serialize_tracks
from app.store.albums import AlbumStore
@@ -69,7 +69,14 @@ def get_artist(path: ArtistHashSchema, query: TrackLimitSchema):
artist.genres.insert(0, {"name": decade, "genrehash": decade})
return {
"artist": {**asdict(artist), "is_favorite": artist.is_favorite},
"artist": {
**serialize_for_card(artist),
"duration": sum(t.duration for t in tracks) if tracks else 0,
"trackcount": tcount,
"albumcount": artist.albumcount,
"genres": artist.genres,
"is_favorite": artist.is_favorite,
},
"tracks": serialize_tracks(tracks[:limit]),
}
@@ -128,7 +135,10 @@ def get_artist_albums(path: ArtistHashSchema, query: GetArtistAlbumsQuery):
res["singles_and_eps"].append(album)
elif album.type == "compilation":
res["compilations"].append(album)
elif album.albumhash in missing_albumhashes or artisthash not in album.artisthashes:
elif (
album.albumhash in missing_albumhashes
or artisthash not in album.artisthashes
):
res["appearances"].append(album)
else:
res["albums"].append(album)
+30 -25
View File
@@ -1,13 +1,11 @@
from typing import List, TypeVar
from flask_jwt_extended import current_user
from flask_openapi3 import Tag
from flask_openapi3 import APIBlueprint
from pydantic import BaseModel, Field
from app.api.apischemas import GenericLimitSchema
from app.db.libdata import ArtistTable
from app.db.libdata import AlbumTable, TrackTable
from app.db.libdata import TrackTable
from app.db.userdata import FavoritesTable
from app.models import FavType
from app.settings import Defaults
@@ -45,6 +43,29 @@ class FavoritesAddBody(BaseModel):
type: str = Field(description="The type of the item", example=FavType.album)
def toggle_fav(type: str, hash: str):
"""
Toggles a favorite item.
"""
if type == FavType.track:
entry = TrackStore.trackhashmap.get(hash)
if entry is not None:
entry.toggle_favorite_user()
elif type == FavType.album:
entry = AlbumStore.albummap.get(hash)
if entry is not None:
entry.toggle_favorite_user()
elif type == FavType.artist:
entry = ArtistStore.artistmap.get(hash)
if entry is not None:
entry.toggle_favorite_user()
return {"msg": "Added to favorites"}
@api.post("/add")
def toggle_favorite(body: FavoritesAddBody):
"""
@@ -56,21 +77,7 @@ def toggle_favorite(body: FavoritesAddBody):
except:
return {"msg": "Failed! An error occured"}, 500
if body.type == FavType.track:
entry = TrackStore.trackhashmap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
elif body.type == FavType.album:
entry = AlbumStore.albummap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
elif body.type == FavType.artist:
entry = ArtistStore.artistmap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
toggle_fav(body.type, body.hash)
return {"msg": "Added to favorites"}
@@ -80,14 +87,12 @@ def remove_favorite(body: FavoritesAddBody):
"""
Removes a favorite from the database.
"""
FavoritesTable.remove_item({"hash": body.hash, "type": body.type})
try:
FavoritesTable.remove_item({"hash": body.hash, "type": body.type})
except:
return {"msg": "Failed! An error occured"}, 500
if body.type == FavType.track:
TrackTable.set_is_favorite(body.hash, False)
elif body.type == FavType.album:
AlbumTable.set_is_favorite(body.hash, False)
elif body.type == FavType.artist:
ArtistTable.set_is_favorite(body.hash, False)
toggle_fav(body.type, body.hash)
return {"msg": "Removed from favorites"}