load settings from db, use api to change settings

+ add route to get all settings
+ add route to set any setting
+ add untested migration to add settings into settings db
+ compress json in api responses using FlaskCompress
+ serve gziped assets if browser accepts encoded files
+ misc
This commit is contained in:
mungai-njoroge
2023-08-24 15:52:09 +03:00
parent e3a61c109b
commit 71cab5f5ea
22 changed files with 437 additions and 163 deletions
+6 -1
View File
@@ -26,7 +26,12 @@ CREATE TABLE IF NOT EXISTS settings (
id integer PRIMARY KEY,
root_dirs text NOT NULL,
exclude_dirs text,
artist_separators text
artist_separators text NOT NULL default '/,;,&',
extract_feat integer NOT NULL DEFAULT 1,
remove_prod integer NOT NULL DEFAULT 1,
clean_album_title integer NOT NULL DEFAULT 1,
remove_remaster integer NOT NULL DEFAULT 1,
merge_albums integer NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS lastfm_similar_artists (
+52 -18
View File
@@ -1,5 +1,8 @@
from pprint import pprint
from typing import Any
from app.db.sqlite.utils import SQLiteManager
from app.utils.wintools import win_replace_slash
from app.settings import FromFlags
class SettingsSQLMethods:
@@ -7,6 +10,28 @@ class SettingsSQLMethods:
Methods for interacting with the settings table.
"""
@staticmethod
def get_all_settings():
"""
Gets all settings from the database.
"""
sql = "SELECT * FROM settings WHERE id = 1"
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql)
settings = cur.fetchone()
cur.close()
# if root_dirs not set
if settings is None:
return []
# print
# omit id, root_dirs, and exclude_dirs
return settings[3:]
@staticmethod
def get_root_dirs() -> list[str]:
"""
@@ -90,25 +115,34 @@ class SettingsSQLMethods:
return [_dir[0] for _dir in dirs]
@staticmethod
def add_artist_separators(seps: set[str]):
"""
Adds a set of artist separators to the userdata table.
"""
# TODO: Implement
def get_settings() -> dict[str, Any]:
pass
@staticmethod
def get_artist_separators() -> set[str]:
"""
Gets a set of artist separators from the userdata table.
"""
# TODO: Implement
pass
def set_setting(key: str, value: Any):
sql = f"UPDATE settings SET {key} = :value WHERE id = 1"
@staticmethod
def remove_artist_separators(seps: set[str]):
"""
Removes a set of artist separators from the userdata table.
"""
# TODO: Implement
pass
if type(value) == bool:
value = str(int(value))
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, {"value": value})
def load_settings():
s = SettingsSQLMethods.get_all_settings()
# artist separators
db_separators: str = s[0]
db_separators = db_separators.replace(" ", "")
separators = db_separators.split(",")
separators = set(db_separators)
FromFlags.ARTIST_SEPARATORS = separators
# boolean settings
FromFlags.EXTRACT_FEAT = bool(s[1])
FromFlags.REMOVE_PROD = bool(s[2])
FromFlags.CLEAN_ALBUM_TITLE = bool(s[3])
FromFlags.REMOVE_REMASTER_FROM_TRACK = bool(s[4])
FromFlags.MERGE_ALBUM_VERSIONS = bool(s[5])
+3 -3
View File
@@ -8,7 +8,7 @@ import time
from typing import Optional
from app.models import Album, Playlist, Track
from app.settings import Db
from app import settings
def tuple_to_track(track: tuple):
@@ -88,10 +88,10 @@ class SQLiteManager:
if self.test_db_path:
db_path = self.test_db_path
else:
db_path = Db.get_app_db_path()
db_path = settings.Db.get_app_db_path()
if self.userdata_db:
db_path = Db.get_userdata_db_path()
db_path = settings.Db.get_userdata_db_path()
self.conn = sqlite3.connect(
db_path,