From 76997ca3fa0bb54497186fea84248695db1636f7 Mon Sep 17 00:00:00 2001 From: cwilvx Date: Mon, 21 Apr 2025 21:40:54 +0300 Subject: [PATCH] show playlists in folder view --- app/api/favorites.py | 1 - app/api/folder.py | 73 ++++++++++++++++++++++++++++++++++++++++++-- app/config.py | 2 ++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/app/api/favorites.py b/app/api/favorites.py index fe21758f..0baf77f9 100644 --- a/app/api/favorites.py +++ b/app/api/favorites.py @@ -5,7 +5,6 @@ from flask_openapi3 import APIBlueprint from pydantic import BaseModel, Field from app.api.apischemas import GenericLimitSchema -from app.db.libdata import TrackTable from app.db.userdata import FavoritesTable from app.lib.extras import get_extra_info from app.models import FavType diff --git a/app/api/folder.py b/app/api/folder.py index 3cdad2f8..1756d1d0 100644 --- a/app/api/folder.py +++ b/app/api/folder.py @@ -2,6 +2,7 @@ Contains all the folder routes. """ +from datetime import datetime import os from pathlib import Path @@ -14,8 +15,10 @@ from showinfm import show_in_file_manager from app import settings from app.config import UserConfig from app.db.libdata import TrackTable +from app.db.userdata import FavoritesTable, PlaylistTable from app.lib.folderslib import get_files_and_dirs, get_folders -from app.serializers.track import serialize_track +from app.serializers.track import serialize_track, serialize_tracks +from app.store.tracks import TrackStore from app.utils.wintools import is_windows, win_replace_slash tag = Tag(name="Folders", description="Get folders and tracks in a directory") @@ -73,6 +76,7 @@ def get_folder_tree(body: FolderTree): Returns a list of all the folders and tracks in the given folder. """ + og_req_dir = body.folder req_dir = body.folder tracks_only = body.tracks_only @@ -96,6 +100,54 @@ def get_folder_tree(body: FolderTree): "tracks": [], } + if req_dir.startswith("$playlist"): + splits = req_dir.split("/") + + if len(splits) == 2: + pid = splits[1] + playlist = PlaylistTable.get_by_id(int(pid)) + tracks = TrackStore.get_tracks_by_trackhashes( + playlist.trackhashes[body.start : body.start + body.limit] + ) + + return { + "path": req_dir, + "folders": [], + "tracks": serialize_tracks(tracks), + } + + playlists = PlaylistTable.get_all() + playlists = sorted( + playlists, + key=lambda p: datetime.strptime(p.last_updated, "%Y-%m-%d %H:%M:%S"), + reverse=True, + ) + + for playlist in playlists: + playlist.clear_lists() + + return { + "path": req_dir, + "folders": [ + { + "name": p.name, + "path": f"$playlist/{p.id}", + } + for p in playlists + ], + "tracks": [], + } + + if req_dir == "$favorites": + tracks, total = FavoritesTable.get_fav_tracks(body.start, body.limit) + tracks = TrackStore.get_tracks_by_trackhashes([t.hash for t in tracks]) + + return { + "tracks": serialize_tracks(tracks), + "folders": [], + "path": req_dir, + } + if is_windows(): # Trailing slash needed when drive letters are passed, # Remember, the trailing slash is removed in the client. @@ -104,7 +156,7 @@ def get_folder_tree(body: FolderTree): else: req_dir = "/" + req_dir if not req_dir.startswith("/") else req_dir - return get_files_and_dirs( + results = get_files_and_dirs( req_dir, start=body.start, limit=body.limit, @@ -115,6 +167,23 @@ def get_folder_tree(body: FolderTree): foldersort_reverse=body.foldersort_reverse, ) + if og_req_dir == "$home" and config.showPlaylistsInFolderView: + # Get all playlists and return them as a list of folders + playlists_item = { + "name": "Playlists", + "path": "$playlists", + } + + favorites_item = { + "name": "Favorites", + "path": "$favorites", + } + + results["folders"].insert(0, playlists_item) + results["folders"].insert(0, favorites_item) + + return results + def get_all_drives(is_win: bool = False): """ diff --git a/app/config.py b/app/config.py index 178a233f..7169f519 100644 --- a/app/config.py +++ b/app/config.py @@ -30,6 +30,7 @@ class UserConfig: "Smith & Thell", "Peter, Paul & Mary", "Simon & Garfunkel", + "Judy & Mary", } ) genreSeparators: set[str] = field(default_factory=lambda: {"/", ";", "&"}) @@ -48,6 +49,7 @@ class UserConfig: enablePeriodicScans: bool = False scanInterval: int = 10 enableWatchdog: bool = False + showPlaylistsInFolderView: bool = False # plugins enablePlugins: bool = True