From e77f2b9f2b67f947d61274e8d7ead87d29fdfc5e Mon Sep 17 00:00:00 2001 From: "restyled-io[bot]" <32688539+restyled-io[bot]@users.noreply.github.com> Date: Wed, 6 Jul 2022 17:41:01 +0300 Subject: [PATCH] Restyle Remove global lists and read everything from the database. (#71) --- server/app/api/__init__.py | 1 - server/app/api/artist.py | 1 - server/app/db/mongodb/playlists.py | 18 ++++++--- server/app/db/mongodb/tracks.py | 60 +++++++++++++++++++++--------- server/app/lib/folderslib.py | 21 ++++++----- server/app/lib/playlistlib.py | 16 ++++---- server/app/lib/trackslib.py | 3 +- server/app/prep.py | 16 +++----- src/router/index.js | 23 +++++------- todo | 4 +- vite.config.js | 4 +- 11 files changed, 97 insertions(+), 70 deletions(-) diff --git a/server/app/api/__init__.py b/server/app/api/__init__.py index 38416d8..5a12808 100644 --- a/server/app/api/__init__.py +++ b/server/app/api/__init__.py @@ -3,7 +3,6 @@ This module contains all the Flask Blueprints and API routes. It also contains a that are used through-out the app. It handles the initialization of the watchdog, checking and creating config dirs and starting the re-indexing process using a background thread. """ - from app import functions from app import helpers from app import prep diff --git a/server/app/api/artist.py b/server/app/api/artist.py index e82d02c..08f60ab 100644 --- a/server/app/api/artist.py +++ b/server/app/api/artist.py @@ -10,7 +10,6 @@ from flask import Blueprint artist_bp = Blueprint("artist", __name__, url_prefix="/") - # @artist_bp.route("/artist/") # @cache.cached() # def get_artist_data(artist: str): diff --git a/server/app/db/mongodb/playlists.py b/server/app/db/mongodb/playlists.py index 9b730c4..78941f2 100644 --- a/server/app/db/mongodb/playlists.py +++ b/server/app/db/mongodb/playlists.py @@ -1,10 +1,10 @@ """ This file contains the Playlists class for interacting with the playlist documents in MongoDB. """ +from app import helpers from app.db.mongodb import convert_many from app.db.mongodb import convert_one from app.db.mongodb import MongoPlaylists -from app import helpers from bson import ObjectId @@ -18,8 +18,12 @@ class Playlists(MongoPlaylists): Inserts a new playlist object into the database. """ return self.collection.update_one( - {"name": playlist["name"]}, - {"$set": playlist}, + { + "name": playlist["name"] + }, + { + "$set": playlist + }, upsert=True, ).upserted_id @@ -45,7 +49,9 @@ class Playlists(MongoPlaylists): return self.collection.update_one( {"_id": ObjectId(playlistid)}, - {"$set": {"lastUpdated": date}}, + {"$set": { + "lastUpdated": date + }}, ) def add_track_to_playlist(self, playlistid: str, track: dict) -> None: @@ -56,7 +62,9 @@ class Playlists(MongoPlaylists): { "_id": ObjectId(playlistid), }, - {"$push": {"pre_tracks": track}}, + {"$push": { + "pre_tracks": track + }}, ) self.set_last_updated(playlistid) diff --git a/server/app/db/mongodb/tracks.py b/server/app/db/mongodb/tracks.py index a7c3b1b..a3596d0 100644 --- a/server/app/db/mongodb/tracks.py +++ b/server/app/db/mongodb/tracks.py @@ -2,7 +2,9 @@ This file contains the AllSongs class for interacting with track documents in MongoDB. """ import pymongo -from app.db.mongodb import MongoTracks, convert_many, convert_one +from app.db.mongodb import convert_many +from app.db.mongodb import convert_one +from app.db.mongodb import MongoTracks from bson import ObjectId @@ -18,9 +20,12 @@ class Tracks(MongoTracks): """ Inserts a new track object into the database. """ - return self.collection.update_one( - {"filepath": song_obj["filepath"]}, {"$set": song_obj}, upsert=True - ).upserted_id + return self.collection.update_one({ + "filepath": song_obj["filepath"] + }, { + "$set": song_obj + }, + upsert=True).upserted_id def insert_many(self, songs: list): """ @@ -52,21 +57,33 @@ class Tracks(MongoTracks): """ Returns all the songs matching the albums in the query params (using regex). """ - songs = self.collection.find({"album": {"$regex": query, "$options": "i"}}) + songs = self.collection.find( + {"album": { + "$regex": query, + "$options": "i" + }}) return convert_many(songs) def search_songs_by_artist(self, query: str) -> list: """ Returns all the songs matching the artists in the query params. """ - songs = self.collection.find({"artists": {"$regex": query, "$options": "i"}}) + songs = self.collection.find( + {"artists": { + "$regex": query, + "$options": "i" + }}) return convert_many(songs) def find_song_by_title(self, query: str) -> list: """ Finds all the tracks matching the title in the query params. """ - song = self.collection.find({"title": {"$regex": query, "$options": "i"}}) + song = self.collection.find( + {"title": { + "$regex": query, + "$options": "i" + }}) return convert_many(song) def find_songs_by_album(self, name: str, artist: str) -> list: @@ -80,7 +97,9 @@ class Tracks(MongoTracks): """ Returns a sorted list of all the tracks exactly matching the folder in the query params """ - songs = self.collection.find({"folder": query}).sort("title", pymongo.ASCENDING) + songs = self.collection.find({ + "folder": query + }).sort("title", pymongo.ASCENDING) return convert_many(songs) def find_songs_by_filenames(self, filenames: list) -> list: @@ -102,8 +121,10 @@ class Tracks(MongoTracks): Returns a list of all the tracks matching the path in the query params. """ return self.collection.count_documents( - {"filepath": {"$regex": f"^{path}", "$options": "i"}} - ) + {"filepath": { + "$regex": f"^{path}", + "$options": "i" + }}) def find_songs_by_artist(self, query: str) -> list: """ @@ -117,8 +138,10 @@ class Tracks(MongoTracks): Returns a list of all the tracks containing the albumartist in the query params. """ songs = self.collection.find( - {"albumartist": {"$regex": query, "$options": "i"}} - ) + {"albumartist": { + "$regex": query, + "$options": "i" + }}) return convert_many(songs) def get_song_by_path(self, path: str) -> dict: @@ -155,13 +178,14 @@ class Tracks(MongoTracks): songs = self.collection.find({"albumhash": hash}) return convert_many(songs) - def find_track_by_title_artists_album( - self, title: str, artist: str, album: str - ) -> dict: + def find_track_by_title_artists_album(self, title: str, artist: str, + album: str) -> dict: """ Returns a single track matching the title, artist, and album in the query params. """ - song = self.collection.find_one( - {"title": title, "artists": artist, "album": album} - ) + song = self.collection.find_one({ + "title": title, + "artists": artist, + "album": album + }) return convert_one(song) diff --git a/server/app/lib/folderslib.py b/server/app/lib/folderslib.py index 1632b66..0cf2d38 100644 --- a/server/app/lib/folderslib.py +++ b/server/app/lib/folderslib.py @@ -1,13 +1,12 @@ +import time +from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass from os import scandir -import time from typing import Tuple -from concurrent.futures import ThreadPoolExecutor - -from app.models import Folder -from app.models import Track from app import instances +from app.models import Folder +from app.models import Track @dataclass @@ -27,10 +26,14 @@ def get_folder_track_count(path: str) -> int: def create_folder(dir: Dir) -> Folder: """Create a single Folder object""" folder = { - "name": dir.path.split("/")[-1], - "path": dir.path, - "is_sym": dir.is_sym, - "trackcount": instances.tracks_instance.find_tracks_inside_path_regex(dir.path), + "name": + dir.path.split("/")[-1], + "path": + dir.path, + "is_sym": + dir.is_sym, + "trackcount": + instances.tracks_instance.find_tracks_inside_path_regex(dir.path), } return Folder(folder) diff --git a/server/app/lib/playlistlib.py b/server/app/lib/playlistlib.py index a1d9f4d..95fda11 100644 --- a/server/app/lib/playlistlib.py +++ b/server/app/lib/playlistlib.py @@ -11,14 +11,13 @@ from app import exceptions from app import instances from app import models from app import settings +from app.helpers import Get +from app.lib import trackslib +from app.logger import get_logger from PIL import Image from PIL import ImageSequence from werkzeug import datastructures -from app.lib import trackslib -from app.helpers import Get -from app.logger import get_logger - TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist logg = get_logger() @@ -53,7 +52,8 @@ def create_thumbnail(image: any, img_path: str) -> str: Creates a 250 x 250 thumbnail from a playlist image """ thumb_path = "thumb_" + img_path - full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists", thumb_path) + full_thumb_path = os.path.join(settings.APP_DIR, "images", "playlists", + thumb_path) aspect_ratio = image.width / image.height @@ -71,11 +71,13 @@ def save_p_image(file: datastructures.FileStorage, pid: str): """ img = Image.open(file) - random_str = "".join(random.choices(string.ascii_letters + string.digits, k=5)) + random_str = "".join( + random.choices(string.ascii_letters + string.digits, k=5)) img_path = pid + str(random_str) + ".webp" - full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", img_path) + full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", + img_path) if file.content_type == "image/gif": frames = [] diff --git a/server/app/lib/trackslib.py b/server/app/lib/trackslib.py index f48847f..61e3913 100644 --- a/server/app/lib/trackslib.py +++ b/server/app/lib/trackslib.py @@ -22,5 +22,4 @@ def validate_tracks() -> None: def get_p_track(ptrack): return instances.tracks_instance.find_track_by_title_artists_album( - ptrack["title"], ptrack["artists"], ptrack["album"] - ) + ptrack["title"], ptrack["artists"], ptrack["album"]) diff --git a/server/app/prep.py b/server/app/prep.py index bb80b71..2ddde83 100644 --- a/server/app/prep.py +++ b/server/app/prep.py @@ -11,13 +11,11 @@ class CopyFiles: """Copies assets to the app directory.""" def __init__(self) -> None: - files = [ - { - "src": "assets", - "dest": os.path.join(settings.APP_DIR, "assets"), - "is_dir": True, - } - ] + files = [{ + "src": "assets", + "dest": os.path.join(settings.APP_DIR, "assets"), + "is_dir": True, + }] for entry in files: src = os.path.join(os.getcwd(), entry["src"]) @@ -26,9 +24,7 @@ class CopyFiles: shutil.copytree( src, entry["dest"], - ignore=shutil.ignore_patterns( - "*.pyc", - ), + ignore=shutil.ignore_patterns("*.pyc", ), copy_function=shutil.copy2, dirs_exist_ok=True, ) diff --git a/src/router/index.js b/src/router/index.js index 38d5824..d8cf036 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,20 +1,17 @@ -import { createRouter, createWebHashHistory } from "vue-router"; -import Home from "@/views/Home.vue"; -import FolderView from "@/views/FolderView.vue"; -import PlaylistView from "@/views/PlaylistView.vue"; -import Playlists from "@/views/Playlists.vue"; - +import state from "@/composables/state"; +import useAStore from "@/stores/pages/album"; +import useFStore from "@/stores/pages/folder"; +import usePTrackStore from "@/stores/pages/playlist"; +import usePStore from "@/stores/pages/playlists"; import AlbumsExplorer from "@/views/AlbumsExplorer.vue"; import AlbumView from "@/views/AlbumView.vue"; - import ArtistsExplorer from "@/views/ArtistsExplorer.vue"; +import FolderView from "@/views/FolderView.vue"; +import Home from "@/views/Home.vue"; +import Playlists from "@/views/Playlists.vue"; +import PlaylistView from "@/views/PlaylistView.vue"; import SettingsView from "@/views/SettingsView.vue"; - -import usePStore from "@/stores/pages/playlists"; -import usePTrackStore from "@/stores/pages/playlist"; -import useFStore from "@/stores/pages/folder"; -import useAStore from "@/stores/pages/album"; -import state from "@/composables/state"; +import { createRouter, createWebHashHistory } from "vue-router"; const routes = [ { diff --git a/todo b/todo index 439903a..3c8d111 100644 --- a/todo +++ b/todo @@ -7,7 +7,7 @@ - [ ] Add and deploy demo branch ### Client -- [ ] Add processes tab to show running tasks, eg. when tagging files. I have no idea on how to go about it so far. Web sockets? +- [ ] Add processes tab to show running tasks, eg. when tagging files. I have no idea on how to go about it so far. Web sockets? - [ ] Responsiveness, especially the track list. - [ ] Make dummy buttons functional. - [ ] Add settings page (or modal) @@ -21,4 +21,4 @@ -- Resolve album page using albumhash instead of album title and artist \ No newline at end of file +- Resolve album page using albumhash instead of album title and artist diff --git a/vite.config.js b/vite.config.js index 05579d0..baea30d 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,6 @@ -import { defineConfig } from "vite"; -import svgLoader from 'vite-svg-loader' import vue from "@vitejs/plugin-vue"; +import { defineConfig } from "vite"; +import svgLoader from "vite-svg-loader"; const path = require("path");