port search to stores

+ fix favorites
This commit is contained in:
cwilvx
2024-07-27 21:44:33 +03:00
parent 5d32536758
commit b0e904c84f
25 changed files with 428 additions and 666 deletions
+12
View File
@@ -9,6 +9,7 @@ from app.lib.tagger import create_albums
from app.models import Album, Track
from app.store.artists import ArtistStore
from app.utils import flatten
from app.utils.auth import get_current_userid
from app.utils.customlist import CustomList
from app.utils.remove_duplicates import remove_duplicates
@@ -28,6 +29,17 @@ class AlbumMapEntry:
def basetitle(self):
return self.album.base_title
def increment_playcount(self, duration: int, timestamp: int):
self.album.lastplayed = timestamp
self.album.playduration += duration
self.album.playcount += 1
def toggle_favorite_user(self, userid: int | None = None):
if userid is None:
userid = get_current_userid()
self.album.toggle_favorite_user(userid)
class AlbumStore:
albums: list[Album] = CustomList()
+12
View File
@@ -5,6 +5,7 @@ from app.db.sqlite.artistcolors import SQLiteArtistMethods as ardb
from app.lib.tagger import create_artists
from app.models import Artist
from app.utils import flatten
from app.utils.auth import get_current_userid
from app.utils.bisection import use_bisection
from app.utils.customlist import CustomList
from app.utils.progressbar import tqdm
@@ -22,6 +23,17 @@ class ArtistMapEntry:
self.albumhashes: set[str] = set()
self.trackhashes: set[str] = set()
def increment_playcount(self, duration: int, timestamp: int):
self.artist.lastplayed = timestamp
self.artist.playduration += duration
self.artist.playcount += 1
def toggle_favorite_user(self, userid: int | None = None):
if userid is None:
userid = get_current_userid()
self.artist.toggle_favorite_user(userid)
class ArtistStore:
artists: list[Artist] = CustomList()
+19 -71
View File
@@ -2,13 +2,10 @@
import itertools
from typing import Callable, Iterable
from flask_jwt_extended import current_user
from app.db.libdata import TrackTable
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
# from app.db.sqlite.tracks import SQLiteTrackMethods as trackdb
from app.db.userdata import FavoritesTable
from app.models import Track
from app.utils.auth import get_current_userid
from app.utils.remove_duplicates import remove_duplicates
TRACKS_LOAD_KEY = ""
@@ -34,12 +31,24 @@ class TrackGroup:
"""
self.tracks.remove(track)
def set_fav_userids(self, userids: set[int]):
def increment_playcount(self, duration: int, timestamp: int):
"""
Sets the favorite userids.
Increments the playcount of all tracks in the group.
"""
for track in self.tracks:
track.fav_userids = userids
track.playcount += 1
track.lastplayed = timestamp
track.playduration += duration
def toggle_favorite_user(self, userid: int | None = None):
"""
Adds or removes a user from the list of users who have favorited the track.
"""
if userid is None:
userid = get_current_userid()
for track in self.tracks:
track.toggle_favorite_user(userid)
def get_best(self):
"""
@@ -47,21 +56,6 @@ class TrackGroup:
"""
return max(self.tracks, key=lambda x: x.bitrate)
def toggle_favorite(self, remove: bool = False):
"""
Adds a track to the favorites.
"""
userids = set(self.tracks[0].fav_userids)
if remove:
userids.remove(current_user["id"])
else:
userids.add(current_user["id"])
for track in self.tracks:
track.fav_userids = userids
def __len__(self):
return len(self.tracks)
@@ -72,7 +66,8 @@ class classproperty(property):
"""
def __get__(self, owner_self, owner_cls):
return self.fget(owner_cls)
if self.fget:
return self.fget(owner_cls)
class TrackStore:
@@ -118,35 +113,6 @@ class TrackStore:
else:
cls.trackhashmap[track.trackhash].append(track)
# favs = favdb.get_fav_tracks()
favs = FavoritesTable.get_all()
records: dict[str, set[int]] = dict()
# convert records: {trackhash: {userid, userid, ...}}
for fav in favs:
if fav.hash not in records:
# if trackhash not in dict, add it
# and set the value to a set containing the userid
records[fav.hash] = {fav.userid}
# if trackhash is in dict, add the userid to the set
records[fav.hash].add(fav.userid)
for record in records:
if instance_key != TRACKS_LOAD_KEY:
return
group = cls.trackhashmap.get(record, None)
if not group:
continue
group.set_fav_userids(records.get(record, set()))
# print("Done!")
# print(cls.trackhashmap.get("0d6b22c19c").tracks[0].fav_userids)
# sys.exit(0)
@classmethod
def add_track(cls, track: Track):
"""
@@ -219,24 +185,6 @@ class TrackStore:
"""
return len(cls.trackhashmap.get(trackhash, []))
@classmethod
def toggle_favorite(cls, trackhash: str, remove: bool = False):
"""
Adds a track to the favorites.
"""
group = cls.trackhashmap.get(trackhash)
if group:
group.toggle_favorite(remove=remove)
@classmethod
def remove_track_from_fav(cls, trackhash: str):
"""
Removes a track from the favorites.
"""
return cls.toggle_favorite(trackhash, remove=True)
# ================================================
# ================== GETTERS =====================
# ================================================
@@ -332,7 +280,7 @@ class TrackStore:
"""
predicate = lambda artisthashes, artisthash: artisthash in artisthashes
return cls.find_tracks_by(
key="artist_hashes", value=artisthash, predicate=predicate
key="artisthashes", value=artisthash, predicate=predicate
)
@classmethod