mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
🔶 add update_playlist route
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
"""
|
||||
Contains all the playlist routes.
|
||||
"""
|
||||
from copy import deepcopy
|
||||
from typing import List
|
||||
|
||||
from flask import Blueprint, request
|
||||
from app import instances, api
|
||||
from app.lib import playlistlib
|
||||
@@ -68,13 +67,46 @@ def add_track_to_playlist(playlist_id: str):
|
||||
def get_single_p_info(playlistid: str):
|
||||
for p in api.PLAYLISTS:
|
||||
if p.playlistid == playlistid:
|
||||
print(p)
|
||||
tracks = p.get_tracks()
|
||||
return {"info": serializer.Playlist(p), "tracks": tracks}
|
||||
|
||||
return {"info": {}, "tracks": []}
|
||||
|
||||
|
||||
@playlist_bp.route("/playlist/<playlistid>/update", methods=["PUT"])
|
||||
def update_playlist(playlistid: str):
|
||||
image = None
|
||||
|
||||
if "image" in request.files:
|
||||
image = request.files["image"]
|
||||
|
||||
data = request.form
|
||||
|
||||
print(type(image))
|
||||
print(image.content_type)
|
||||
|
||||
playlist = {
|
||||
"name": str(data.get("name")).strip(),
|
||||
"description": str(data.get("description").strip()),
|
||||
"lastUpdated": str(data.get("lastUpdated")),
|
||||
"image": None,
|
||||
}
|
||||
|
||||
if image:
|
||||
playlist["image"] = playlistlib.save_p_image(image, playlistid)
|
||||
|
||||
for p in api.PLAYLISTS:
|
||||
if p.playlistid == playlistid:
|
||||
p.update_playlist(playlist)
|
||||
instances.playlist_instance.update_playlist(playlistid, playlist)
|
||||
|
||||
return {
|
||||
"data": serializer.Playlist(p),
|
||||
}
|
||||
|
||||
return {"msg": "Something shady happened"}, 500
|
||||
|
||||
|
||||
# @playlist_bp.route("/playlist/<playlist_id>/info")
|
||||
# def get_playlist_track(playlist_id: str):
|
||||
# tracks = playlistlib.get_playlist_tracks(playlist_id)
|
||||
|
||||
@@ -58,3 +58,12 @@ class Playlists(db.Mongo):
|
||||
"""
|
||||
playlist = self.collection.find_one({"name": name})
|
||||
return convert_one(playlist)
|
||||
|
||||
def update_playlist(self, playlistid: str, playlist: dict) -> None:
|
||||
"""
|
||||
Updates a playlist.
|
||||
"""
|
||||
return self.collection.update_one(
|
||||
{"_id": ObjectId(playlistid)},
|
||||
{"$set": playlist},
|
||||
)
|
||||
@@ -1,8 +1,15 @@
|
||||
"""
|
||||
This library contains all the functions related to playlists.
|
||||
"""
|
||||
import base64
|
||||
import io
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
from PIL import Image, ImageSequence
|
||||
from progress.bar import Bar
|
||||
from app import api, instances, models, exceptions, helpers
|
||||
from werkzeug import datastructures
|
||||
from app import api, instances, models, exceptions, settings
|
||||
|
||||
from app.lib import trackslib
|
||||
|
||||
@@ -48,3 +55,28 @@ def create_all_playlists():
|
||||
api.PLAYLISTS.append(models.Playlist(playlist))
|
||||
_bar.next()
|
||||
_bar.finish()
|
||||
|
||||
|
||||
def save_p_image(file: datastructures.FileStorage, pid: str):
|
||||
"""
|
||||
Saves the image of a playlist to the database.
|
||||
"""
|
||||
img = Image.open(file)
|
||||
|
||||
random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5))
|
||||
|
||||
img_path = pid + str(random_str) + ".webp"
|
||||
full_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path)
|
||||
|
||||
if file.content_type == "image/gif":
|
||||
frames = []
|
||||
|
||||
for frame in ImageSequence.Iterator(img):
|
||||
frames.append(frame.copy())
|
||||
|
||||
frames[0].save(full_path, save_all=True, append_images=frames[1:])
|
||||
return img_path
|
||||
|
||||
img.save(full_path, "webp")
|
||||
|
||||
return img_path
|
||||
|
||||
+17
-3
@@ -100,10 +100,10 @@ class Playlist:
|
||||
|
||||
playlistid: str
|
||||
name: str
|
||||
image: str
|
||||
tracks: List[Track]
|
||||
_pre_tracks: list = field(init=False, repr=False)
|
||||
lastUpdated: int
|
||||
image: str
|
||||
description: str = ""
|
||||
count: int = 0
|
||||
"""A list of track objects in the playlist"""
|
||||
@@ -112,7 +112,7 @@ class Playlist:
|
||||
self.playlistid = data["_id"]["$oid"]
|
||||
self.name = data["name"]
|
||||
self.description = data["description"]
|
||||
self.image = ""
|
||||
self.image = self.create_img_link(data["image"])
|
||||
self._pre_tracks = data["pre_tracks"]
|
||||
self.tracks = []
|
||||
self.lastUpdated = data["lastUpdated"]
|
||||
@@ -124,8 +124,14 @@ class Playlist:
|
||||
"""
|
||||
return create_playlist_tracks(self._pre_tracks)
|
||||
|
||||
def create_img_link(self, image: str):
|
||||
if image:
|
||||
return settings.IMG_PLAYLIST_URI + image
|
||||
|
||||
return settings.IMG_PLAYLIST_URI + ""
|
||||
|
||||
def update_count(self):
|
||||
self.count = len(self.tracks)
|
||||
self.count = len(self._pre_tracks)
|
||||
|
||||
def add_track(self, track):
|
||||
if track not in self._pre_tracks:
|
||||
@@ -137,6 +143,14 @@ class Playlist:
|
||||
def update_desc(self, desc):
|
||||
self.description = desc
|
||||
|
||||
def update_playlist(self, data: dict):
|
||||
self.name = data["name"]
|
||||
self.description = data["description"]
|
||||
self.lastUpdated = data["lastUpdated"]
|
||||
|
||||
if data["image"]:
|
||||
self.image = self.create_img_link(data["image"])
|
||||
|
||||
|
||||
@dataclass
|
||||
class Folder:
|
||||
|
||||
+4
-11
@@ -13,27 +13,20 @@ def create_config_dir() -> None:
|
||||
|
||||
_home_dir = os.path.expanduser("~")
|
||||
config_folder = os.path.join(_home_dir, settings.CONFIG_FOLDER)
|
||||
print(config_folder)
|
||||
|
||||
dirs = [
|
||||
"",
|
||||
"images",
|
||||
os.path.join("images", "artists"),
|
||||
os.path.join("images", "thumbnails"),
|
||||
os.path.join("images", "playlists"),
|
||||
]
|
||||
|
||||
for _dir in dirs:
|
||||
path = os.path.join(config_folder, _dir)
|
||||
exists = os.path.exists(path)
|
||||
|
||||
try:
|
||||
os.path.exists(path)
|
||||
except FileNotFoundError:
|
||||
if not exists:
|
||||
os.makedirs(path)
|
||||
os.chmod(path, 0o755)
|
||||
|
||||
if _dir == dirs[3]:
|
||||
default_thumbnails_path = "../setup/default-images/thumbnails"
|
||||
|
||||
try:
|
||||
os.path.exists(os.path.join(path, "defaults"))
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
@@ -9,10 +9,13 @@ HOME_DIR = os.path.expanduser("~")
|
||||
APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER)
|
||||
THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails")
|
||||
|
||||
|
||||
# URL
|
||||
IMG_BASE_URI = "http://127.0.0.1:8900/images/"
|
||||
IMG_ARTIST_URI = IMG_BASE_URI + "artists/"
|
||||
IMG_THUMB_URI = IMG_BASE_URI + "thumbnails/"
|
||||
IMG_PLAYLIST_URI = IMG_BASE_URI + "playlists/"
|
||||
|
||||
|
||||
# defaults
|
||||
DEFAULT_ARTIST_IMG = IMG_ARTIST_URI + "0.webp"
|
||||
|
||||
Reference in New Issue
Block a user