mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 12:33:03 +00:00
🔷 add PlayingFrom component to right sidebar
🔷 move upNext card into separate component 🔷 a lot of refactors
This commit is contained in:
+15
-19
@@ -8,6 +8,7 @@ from app import instances, api
|
||||
from app.lib import playlistlib
|
||||
from app import models
|
||||
from app import exceptions
|
||||
from app import serializer
|
||||
|
||||
playlist_bp = Blueprint("playlist", __name__, url_prefix="/")
|
||||
|
||||
@@ -17,15 +18,7 @@ TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist
|
||||
|
||||
@playlist_bp.route("/playlists", methods=["GET"])
|
||||
def get_all_playlists():
|
||||
ppp = deepcopy(api.PLAYLISTS)
|
||||
playlists = []
|
||||
|
||||
for pl in ppp:
|
||||
pl.count = len(pl.tracks)
|
||||
pl.tracks = []
|
||||
playlists.append(pl)
|
||||
|
||||
return {"data": playlists}
|
||||
return {"data": [serializer.Playlist(p) for p in api.PLAYLISTS]}
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/new", methods=["POST"])
|
||||
@@ -35,16 +28,16 @@ def create_playlist():
|
||||
playlist = {
|
||||
"name": data["name"],
|
||||
"description": "",
|
||||
"tracks": [],
|
||||
"count": 0,
|
||||
"pre_tracks": [],
|
||||
"lastUpdated": 0,
|
||||
"image": "",
|
||||
}
|
||||
|
||||
try:
|
||||
p_in_db = instances.playlist_instance.get_playlist_by_name(playlist["name"])
|
||||
for pl in api.PLAYLISTS:
|
||||
if pl.name == playlist["name"]:
|
||||
raise PlaylistExists("Playlist already exists.")
|
||||
|
||||
if p_in_db:
|
||||
raise PlaylistExists("Playlist already exists.")
|
||||
except PlaylistExists as e:
|
||||
return {"error": str(e)}, 409
|
||||
|
||||
@@ -71,12 +64,15 @@ def add_track_to_playlist(playlist_id: str):
|
||||
return {"msg": "I think It's done"}, 200
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlist_id>")
|
||||
def get_single_p_info(playlist_id: str):
|
||||
@playlist_bp.route("/playlist/<playlistid>")
|
||||
def get_single_p_info(playlistid: str):
|
||||
for p in api.PLAYLISTS:
|
||||
if p.playlistid == playlist_id:
|
||||
p.count = len(p.tracks)
|
||||
return {"data": p}
|
||||
if p.playlistid == playlistid:
|
||||
print(p)
|
||||
tracks = p.get_tracks()
|
||||
return {"info": serializer.Playlist(p), "tracks": tracks}
|
||||
|
||||
return {"info": {}, "tracks": []}
|
||||
|
||||
|
||||
# @playlist_bp.route("/playlist/<playlist_id>/info")
|
||||
|
||||
@@ -9,7 +9,6 @@ convert_many = db.convert_many
|
||||
convert_one = db.convert_one
|
||||
|
||||
|
||||
|
||||
class Playlists(db.Mongo):
|
||||
"""
|
||||
The class for all playlist-related database operations.
|
||||
@@ -43,20 +42,15 @@ class Playlists(db.Mongo):
|
||||
playlist = self.collection.find_one({"_id": ObjectId(id)})
|
||||
return convert_one(playlist)
|
||||
|
||||
def add_track_to_playlist(self, playlistid: str, track: models.Track):
|
||||
def add_track_to_playlist(self, playlistid: str, track: dict) -> None:
|
||||
"""
|
||||
Adds a track to a playlist.
|
||||
"""
|
||||
track = {
|
||||
"title": track.title,
|
||||
"artists": track.artists,
|
||||
"album": track.album,
|
||||
}
|
||||
|
||||
return self.collection.update_one(
|
||||
{"_id": ObjectId(playlistid)},
|
||||
{"$push": {"tracks": track}},
|
||||
).modified_count
|
||||
{"$push": {"pre_tracks": track}},
|
||||
)
|
||||
|
||||
def get_playlist_by_name(self, name: str) -> dict:
|
||||
"""
|
||||
|
||||
@@ -81,7 +81,6 @@ def populate():
|
||||
|
||||
albumslib.create_everything()
|
||||
folderslib.run_scandir()
|
||||
playlistlib.create_all_playlists()
|
||||
|
||||
end = time.time()
|
||||
|
||||
|
||||
@@ -15,14 +15,20 @@ def add_track(playlistid: str, trackid: str):
|
||||
"""
|
||||
for playlist in api.PLAYLISTS:
|
||||
if playlist.playlistid == playlistid:
|
||||
track = trackslib.get_track_by_id(trackid)
|
||||
tt = trackslib.get_track_by_id(trackid)
|
||||
|
||||
if track not in playlist.tracks:
|
||||
playlist.tracks.append(track)
|
||||
track = {
|
||||
"title": tt.title,
|
||||
"artists": tt.artists,
|
||||
"album": tt.album,
|
||||
}
|
||||
|
||||
try:
|
||||
playlist.add_track(track)
|
||||
instances.playlist_instance.add_track_to_playlist(playlistid, track)
|
||||
return
|
||||
else:
|
||||
raise TrackExistsInPlaylist("Track already in playlist.")
|
||||
except TrackExistsInPlaylist as e:
|
||||
return {"error": str(e)}, 409
|
||||
|
||||
|
||||
def get_playlist_tracks(pid: str):
|
||||
@@ -31,7 +37,6 @@ def get_playlist_tracks(pid: str):
|
||||
return p.tracks
|
||||
|
||||
|
||||
|
||||
def create_all_playlists():
|
||||
"""
|
||||
Gets all playlists from the database.
|
||||
|
||||
+26
-3
@@ -2,11 +2,12 @@
|
||||
Contains all the models for objects generation and typing.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date
|
||||
from typing import List
|
||||
from app import api
|
||||
from app import settings
|
||||
from app.exceptions import TrackExistsInPlaylist
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -99,10 +100,11 @@ class Playlist:
|
||||
|
||||
playlistid: str
|
||||
name: str
|
||||
description: str
|
||||
image: str
|
||||
tracks: List[Track]
|
||||
_pre_tracks: list = field(init=False, repr=False)
|
||||
lastUpdated: int
|
||||
description: str = ""
|
||||
count: int = 0
|
||||
"""A list of track objects in the playlist"""
|
||||
|
||||
@@ -111,8 +113,29 @@ class Playlist:
|
||||
self.name = data["name"]
|
||||
self.description = data["description"]
|
||||
self.image = ""
|
||||
self.tracks = create_playlist_tracks(data["tracks"])
|
||||
self._pre_tracks = data["pre_tracks"]
|
||||
self.tracks = []
|
||||
self.lastUpdated = data["lastUpdated"]
|
||||
self.count = len(self._pre_tracks)
|
||||
|
||||
def get_tracks(self) -> List[Track]:
|
||||
"""
|
||||
Generates and returns Track objects from pre_tracks
|
||||
"""
|
||||
return create_playlist_tracks(self._pre_tracks)
|
||||
|
||||
def update_count(self):
|
||||
self.count = len(self.tracks)
|
||||
|
||||
def add_track(self, track):
|
||||
if track not in self._pre_tracks:
|
||||
self._pre_tracks.append(track)
|
||||
self.update_count()
|
||||
else:
|
||||
raise TrackExistsInPlaylist("Track already exists in playlist")
|
||||
|
||||
def update_desc(self, desc):
|
||||
self.description = desc
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user