add route to save folder as playlist

+ modify playlist table sql
This commit is contained in:
mungai-njoroge
2023-07-27 13:37:07 +03:00
parent d4a1a6ee1a
commit a0c51d5f82
6 changed files with 124 additions and 44 deletions
+34 -16
View File
@@ -15,15 +15,16 @@ class SQLitePlaylistMethods:
@staticmethod
def insert_one_playlist(playlist: dict):
# banner_pos,
# has_gif,
sql = """INSERT INTO playlists(
artisthashes,
banner_pos,
has_gif,
image,
last_updated,
name,
settings,
trackhashes
) VALUES(:artisthashes, :banner_pos, :has_gif, :image, :last_updated, :name, :trackhashes)
) VALUES(:artisthashes, :image, :last_updated, :name, :settings, :trackhashes)
"""
playlist = OrderedDict(sorted(playlist.items()))
@@ -93,7 +94,7 @@ class SQLitePlaylistMethods:
# FIXME: Extract the "add_track_to_playlist" method to use it for both the artisthash and trackhash lists.
@staticmethod
def add_item_to_json_list(playlist_id: int, field: str, items: list[str]):
def add_item_to_json_list(playlist_id: int, field: str, items: set[str]):
"""
Adds a string item to a json dumped list using a playlist id and field name.
Takes the playlist ID, a field name,
@@ -108,6 +109,7 @@ class SQLitePlaylistMethods:
if data is not None:
db_items: list[str] = json.loads(data[0])
# Remove duplicates, without changing the order.
for item in items:
if item in db_items:
items.remove(item)
@@ -124,30 +126,39 @@ class SQLitePlaylistMethods:
@classmethod
@background
def add_artist_to_playlist(cls, playlist_id: int, trackhash: str):
track = SQLiteTrackMethods.get_track_by_trackhash(trackhash)
if track is None:
return
def add_artists_to_playlist(
cls,
playlist_id: int,
trackhashes: list[str] = [],
artisthashes: set[str] = None,
):
if not artisthashes:
track = [SQLiteTrackMethods.get_track_by_trackhash(t) for t in trackhashes]
tracks = [t for t in track if t is not None]
artists: list[Artist] = track.artist # type: ignore
artisthashes = [a.artisthash for a in artists]
artisthashes: set[str] = set() # type: ignore
for track in tracks:
for a in track.artist:
artisthashes.add(a.artisthash)
cls.add_item_to_json_list(playlist_id, "artisthashes", artisthashes)
@staticmethod
def update_playlist(playlist_id: int, playlist: dict):
sql = """UPDATE playlists SET
has_gif = ?,
image = ?,
last_updated = ?,
name = ?
name = ?,
settings = ?
WHERE id = ?
"""
del playlist["id"]
del playlist["trackhashes"]
del playlist["artisthashes"]
del playlist["banner_pos"]
playlist["settings"] = json.dumps(playlist["settings"])
playlist = OrderedDict(sorted(playlist.items()))
params = (*playlist.values(), playlist_id)
@@ -172,10 +183,18 @@ class SQLitePlaylistMethods:
@staticmethod
def update_banner_pos(playlistid: int, pos: int):
sql = """UPDATE playlists SET banner_pos = ? WHERE id = ?"""
playlist = SQLitePlaylistMethods.get_playlist_by_id(playlistid)
if playlist is None:
return
playlist.settings["banner_pos"] = pos
settings_str = json.dumps(playlist.settings)
sql = """UPDATE playlists SET settings = ? WHERE id = ?"""
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, (pos, playlistid))
cur.execute(sql, (settings_str, playlistid))
@staticmethod
def remove_banner(playlistid: int):
@@ -200,7 +219,6 @@ class SQLitePlaylistMethods:
return
trackhashes: list[str] = json.loads(data[0])
print(tracks)
for track in tracks:
# {
+3 -2
View File
@@ -3,15 +3,16 @@ This file contains the SQL queries to create the database tables.
"""
# banner_pos integer NOT NULL,
# has_gif integer,
CREATE_USERDATA_TABLES = """
CREATE TABLE IF NOT EXISTS playlists (
id integer PRIMARY KEY,
artisthashes text,
banner_pos integer NOT NULL,
has_gif integer,
image text,
last_updated text not null,
name text not null,
settings text,
trackhashes text
);