port: recent items for homepage

This commit is contained in:
cwilvx
2024-06-30 23:11:33 +03:00
parent 5759521de0
commit a3c4558d52
9 changed files with 92 additions and 37 deletions
+5
View File
@@ -3,6 +3,7 @@ from typing import Any
from sqlalchemy import (
create_engine,
delete,
func,
insert,
select,
)
@@ -81,6 +82,10 @@ class Base(MappedAsDataclass, DeclarativeBase):
def all(cls):
return cls.execute(select(cls))
@classmethod
def count(cls):
return cls.execute(select(func.count()).select_from(cls)).scalar()
def create_all():
Base().metadata.create_all(engine)
+11 -3
View File
@@ -35,9 +35,13 @@ class Base(MasterBase, DeclarativeBase):
if cls.__tablename__ == "track":
stmt = select(TrackTable.trackhash).where(cls.last_mod < create_date)
elif cls.__tablename__ == "album":
stmt = select(AlbumTable.albumhash).where(cls.created_date < create_date)
stmt = select(AlbumTable.albumhash).where(
cls.created_date < create_date
)
elif cls.__tablename__ == "artist":
stmt = select(ArtistTable.artisthash).where(cls.created_date < create_date)
stmt = select(ArtistTable.artisthash).where(
cls.created_date < create_date
)
result = conn.execute(stmt)
return {row[0] for row in result.fetchall()}
@@ -206,6 +210,11 @@ class TrackTable(Base):
return tracks_to_dataclasses(result.fetchall())
@classmethod
def get_recently_played(cls, limit: int):
result = cls.execute(select(cls).order_by(cls.lastplayed.desc()).limit(limit))
return tracks_to_dataclasses(result.fetchall())
@classmethod
def remove_tracks_by_filepaths(cls, filepaths: set[str]):
with DbManager(commit=True) as conn:
@@ -250,7 +259,6 @@ class AlbumTable(Base):
all = result.fetchall()
return albums_to_dataclasses(all)
@classmethod
def get_album_by_albumhash(cls, hash: str):
with DbManager() as conn:
+15
View File
@@ -25,6 +25,8 @@ from app.db.utils import (
plugin_to_dataclasses,
similar_artist_to_dataclass,
similar_artists_to_dataclass,
tracklog_to_dataclass,
tracklog_to_dataclasses,
tracks_to_dataclasses,
user_to_dataclass,
user_to_dataclasses,
@@ -166,6 +168,7 @@ class FavoritesTable(Base):
JSON(), nullable=True, default_factory=dict
)
@classmethod
def get_all(cls):
with DbManager() as conn:
@@ -259,3 +262,15 @@ class ScrobbleTable(Base):
def add(cls, item: dict[str, Any]):
item["userid"] = get_current_userid()
return cls.insert_one(item)
@classmethod
def get_all(cls, start: int, limit: int):
result = cls.execute(
select(cls)
.where(cls.userid == get_current_userid())
.order_by(cls.timestamp.desc())
.offset(start)
.limit(limit)
)
return tracklog_to_dataclasses(result.fetchall())
+8
View File
@@ -3,6 +3,7 @@ from typing import Any
from app.models import Album as AlbumModel, Artist as ArtistModel, Track as TrackModel
from app.models.favorite import Favorite
from app.models.lastfm import SimilarArtist
from app.models.logger import TrackLog
from app.models.plugins import Plugin
from app.models.user import User
@@ -73,3 +74,10 @@ def plugin_to_dataclass(entry: Any):
def plugin_to_dataclasses(entries: Any):
return [plugin_to_dataclass(entry) for entry in entries]
def tracklog_to_dataclass(entry: Any):
entry_dict = entry._asdict()
return TrackLog(**entry_dict)
def tracklog_to_dataclasses(entries: Any):
return [tracklog_to_dataclass(entry) for entry in entries]