mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
🔶 add update_playlist route
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Contains all the playlist routes.
|
Contains all the playlist routes.
|
||||||
"""
|
"""
|
||||||
from copy import deepcopy
|
|
||||||
from typing import List
|
|
||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
from app import instances, api
|
from app import instances, api
|
||||||
from app.lib import playlistlib
|
from app.lib import playlistlib
|
||||||
@@ -68,13 +67,46 @@ def add_track_to_playlist(playlist_id: str):
|
|||||||
def get_single_p_info(playlistid: str):
|
def get_single_p_info(playlistid: str):
|
||||||
for p in api.PLAYLISTS:
|
for p in api.PLAYLISTS:
|
||||||
if p.playlistid == playlistid:
|
if p.playlistid == playlistid:
|
||||||
print(p)
|
|
||||||
tracks = p.get_tracks()
|
tracks = p.get_tracks()
|
||||||
return {"info": serializer.Playlist(p), "tracks": tracks}
|
return {"info": serializer.Playlist(p), "tracks": tracks}
|
||||||
|
|
||||||
return {"info": {}, "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")
|
# @playlist_bp.route("/playlist/<playlist_id>/info")
|
||||||
# def get_playlist_track(playlist_id: str):
|
# def get_playlist_track(playlist_id: str):
|
||||||
# tracks = playlistlib.get_playlist_tracks(playlist_id)
|
# tracks = playlistlib.get_playlist_tracks(playlist_id)
|
||||||
|
|||||||
@@ -58,3 +58,12 @@ class Playlists(db.Mongo):
|
|||||||
"""
|
"""
|
||||||
playlist = self.collection.find_one({"name": name})
|
playlist = self.collection.find_one({"name": name})
|
||||||
return convert_one(playlist)
|
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.
|
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 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
|
from app.lib import trackslib
|
||||||
|
|
||||||
@@ -48,3 +55,28 @@ def create_all_playlists():
|
|||||||
api.PLAYLISTS.append(models.Playlist(playlist))
|
api.PLAYLISTS.append(models.Playlist(playlist))
|
||||||
_bar.next()
|
_bar.next()
|
||||||
_bar.finish()
|
_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
|
playlistid: str
|
||||||
name: str
|
name: str
|
||||||
image: str
|
|
||||||
tracks: List[Track]
|
tracks: List[Track]
|
||||||
_pre_tracks: list = field(init=False, repr=False)
|
_pre_tracks: list = field(init=False, repr=False)
|
||||||
lastUpdated: int
|
lastUpdated: int
|
||||||
|
image: str
|
||||||
description: str = ""
|
description: str = ""
|
||||||
count: int = 0
|
count: int = 0
|
||||||
"""A list of track objects in the playlist"""
|
"""A list of track objects in the playlist"""
|
||||||
@@ -112,7 +112,7 @@ class Playlist:
|
|||||||
self.playlistid = data["_id"]["$oid"]
|
self.playlistid = data["_id"]["$oid"]
|
||||||
self.name = data["name"]
|
self.name = data["name"]
|
||||||
self.description = data["description"]
|
self.description = data["description"]
|
||||||
self.image = ""
|
self.image = self.create_img_link(data["image"])
|
||||||
self._pre_tracks = data["pre_tracks"]
|
self._pre_tracks = data["pre_tracks"]
|
||||||
self.tracks = []
|
self.tracks = []
|
||||||
self.lastUpdated = data["lastUpdated"]
|
self.lastUpdated = data["lastUpdated"]
|
||||||
@@ -124,8 +124,14 @@ class Playlist:
|
|||||||
"""
|
"""
|
||||||
return create_playlist_tracks(self._pre_tracks)
|
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):
|
def update_count(self):
|
||||||
self.count = len(self.tracks)
|
self.count = len(self._pre_tracks)
|
||||||
|
|
||||||
def add_track(self, track):
|
def add_track(self, track):
|
||||||
if track not in self._pre_tracks:
|
if track not in self._pre_tracks:
|
||||||
@@ -137,6 +143,14 @@ class Playlist:
|
|||||||
def update_desc(self, desc):
|
def update_desc(self, desc):
|
||||||
self.description = 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
|
@dataclass
|
||||||
class Folder:
|
class Folder:
|
||||||
|
|||||||
+4
-11
@@ -13,27 +13,20 @@ def create_config_dir() -> None:
|
|||||||
|
|
||||||
_home_dir = os.path.expanduser("~")
|
_home_dir = os.path.expanduser("~")
|
||||||
config_folder = os.path.join(_home_dir, settings.CONFIG_FOLDER)
|
config_folder = os.path.join(_home_dir, settings.CONFIG_FOLDER)
|
||||||
|
print(config_folder)
|
||||||
|
|
||||||
dirs = [
|
dirs = [
|
||||||
"",
|
"",
|
||||||
"images",
|
"images",
|
||||||
os.path.join("images", "artists"),
|
os.path.join("images", "artists"),
|
||||||
os.path.join("images", "thumbnails"),
|
os.path.join("images", "thumbnails"),
|
||||||
|
os.path.join("images", "playlists"),
|
||||||
]
|
]
|
||||||
|
|
||||||
for _dir in dirs:
|
for _dir in dirs:
|
||||||
path = os.path.join(config_folder, _dir)
|
path = os.path.join(config_folder, _dir)
|
||||||
|
exists = os.path.exists(path)
|
||||||
|
|
||||||
try:
|
if not exists:
|
||||||
os.path.exists(path)
|
|
||||||
except FileNotFoundError:
|
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
os.chmod(path, 0o755)
|
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)
|
APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER)
|
||||||
THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails")
|
THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails")
|
||||||
|
|
||||||
|
|
||||||
# URL
|
# URL
|
||||||
IMG_BASE_URI = "http://127.0.0.1:8900/images/"
|
IMG_BASE_URI = "http://127.0.0.1:8900/images/"
|
||||||
IMG_ARTIST_URI = IMG_BASE_URI + "artists/"
|
IMG_ARTIST_URI = IMG_BASE_URI + "artists/"
|
||||||
IMG_THUMB_URI = IMG_BASE_URI + "thumbnails/"
|
IMG_THUMB_URI = IMG_BASE_URI + "thumbnails/"
|
||||||
|
IMG_PLAYLIST_URI = IMG_BASE_URI + "playlists/"
|
||||||
|
|
||||||
|
|
||||||
# defaults
|
# defaults
|
||||||
DEFAULT_ARTIST_IMG = IMG_ARTIST_URI + "0.webp"
|
DEFAULT_ARTIST_IMG = IMG_ARTIST_URI + "0.webp"
|
||||||
|
|||||||
Reference in New Issue
Block a user