move imgserver to app/api folder

+ add sqlite methods to configure custom root directories
+ add sqlite.settings module
+ remove date and app name from logger messages
+ add api route to browse directories
This commit is contained in:
geoffrey45
2023-01-21 18:07:20 +03:00
parent 3dc9bc1f15
commit 4e6e1f03dc
9 changed files with 179 additions and 31 deletions
+61 -24
View File
@@ -7,44 +7,81 @@ class SettingsSQLMethods:
Methods for interacting with the settings table.
"""
@staticmethod
def update_root_dirs(dirs: list[str]):
"""
Updates custom root directories in the database.
"""
sql = "UPDATE settings SET root_dirs = ?"
dirs_str = json.dumps(dirs)
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, (dirs_str,))
@staticmethod
def get_root_dirs() -> list[str]:
"""
Gets custom root directories from the database.
"""
sql = "SELECT value FROM settings"
sql = "SELECT root_dirs FROM settings"
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql)
dirs = cur.fetchall()
data = cur.fetchone()
if data is not None:
return json.loads(data[0])
return []
return [dir[0] for dir in dirs]
@staticmethod
def update_exclude_dirs(dirs: list[str]):
def add_root_dirs(dirs: list[str]):
"""
Updates excluded directories in the database.
Add custom root directories to the database.
"""
sql = "UPDATE settings SET exclude_dirs = ?"
dirs_str = json.dumps(dirs)
sql = "INSERT INTO settings (root_dirs) VALUES (?)"
existing_dirs = SettingsSQLMethods.get_root_dirs()
dirs = [dir for dir in dirs if dir not in existing_dirs]
if len(dirs) == 0:
return
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql, (dirs_str,))
for _dir in dirs:
cur.execute(sql, (_dir,))
@staticmethod
def remove_root_dirs(dirs: list[str]):
"""
Remove custom root directories from the database.
"""
sql = "DELETE FROM settings WHERE root_dirs = ?"
with SQLiteManager(userdata_db=True) as cur:
for _dir in dirs:
cur.execute(sql, (_dir,))
@staticmethod
def add_excluded_dirs(dirs: list[str]):
"""
Add custom exclude directories to the database.
"""
sql = "INSERT INTO settings (exclude_dirs) VALUES (?)"
with SQLiteManager(userdata_db=True) as cur:
cur.executemany(sql, dirs)
@staticmethod
def remove_excluded_dirs(dirs: list[str]):
"""
Remove custom exclude directories from the database.
"""
sql = "DELETE FROM settings WHERE exclude_dirs = ?"
with SQLiteManager(userdata_db=True) as cur:
cur.executemany(sql, dirs)
@staticmethod
def get_excluded_dirs() -> list[str]:
"""
Gets custom exclude directories from the database.
"""
sql = "SELECT exclude_dirs FROM settings"
with SQLiteManager(userdata_db=True) as cur:
cur.execute(sql)
dirs = cur.fetchall()
return [dir[0] for dir in dirs]