refactor database and media handling to use icon paths instead of flags
This commit is contained in:
@@ -39,7 +39,7 @@ class DatabaseManager:
|
|||||||
CREATE TABLE IF NOT EXISTS colors (
|
CREATE TABLE IF NOT EXISTS colors (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
name TEXT UNIQUE NOT NULL,
|
name TEXT UNIQUE NOT NULL,
|
||||||
has_icon INTEGER DEFAULT 0
|
icon_path TEXT NOT NULL
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@@ -62,13 +62,13 @@ class DatabaseManager:
|
|||||||
# -------------------------
|
# -------------------------
|
||||||
# Operacje na kolorach
|
# Operacje na kolorach
|
||||||
# -------------------------
|
# -------------------------
|
||||||
def add_color(self, name: str, has_icon: bool = False):
|
def add_color(self, name: str, icon_path: str):
|
||||||
if self.conn is None:
|
if self.conn is None:
|
||||||
raise RuntimeError("Database not connected")
|
raise RuntimeError("Database not connected")
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT OR IGNORE INTO colors (name, has_icon) VALUES (?, ?)",
|
"INSERT OR IGNORE INTO colors (name, icon_path) VALUES (?, ?)",
|
||||||
(name, int(has_icon)),
|
(name, icon_path),
|
||||||
)
|
)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
@@ -95,11 +95,11 @@ class DatabaseManager:
|
|||||||
cur.execute("UPDATE colors SET name = ? WHERE name = ?", (new_name, old_name))
|
cur.execute("UPDATE colors SET name = ? WHERE name = ?", (new_name, old_name))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def update_color_icon_flag(self, name: str, has_icon: bool):
|
def update_color_icon(self, name: str, icon_path: str):
|
||||||
if self.conn is None:
|
if self.conn is None:
|
||||||
raise RuntimeError("Database not connected")
|
raise RuntimeError("Database not connected")
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
cur.execute("UPDATE colors SET has_icon = ? WHERE name = ?", (int(has_icon), name))
|
cur.execute("UPDATE colors SET icon_path = ? WHERE name = ?", (icon_path, name))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def delete_color(self, name: str):
|
def delete_color(self, name: str):
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class MediaRepository:
|
|||||||
continue
|
continue
|
||||||
color_dir = MEDIA_DIR / color
|
color_dir = MEDIA_DIR / color
|
||||||
icon_file = color_dir / "icon.jpg"
|
icon_file = color_dir / "icon.jpg"
|
||||||
self.db.update_color_icon_flag(color, icon_file.exists())
|
self.db.update_color_icon(color, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
|
||||||
|
|
||||||
disk_files = {f.name for f in color_dir.iterdir() if f.is_file() and f.name != "icon.jpg"}
|
disk_files = {f.name for f in color_dir.iterdir() if f.is_file() and f.name != "icon.jpg"}
|
||||||
db_files = {m["filename"] for m in self.db.get_media_for_color(color_id)}
|
db_files = {m["filename"] for m in self.db.get_media_for_color(color_id)}
|
||||||
@@ -53,7 +53,7 @@ class MediaRepository:
|
|||||||
if icon_path and icon_path.exists():
|
if icon_path and icon_path.exists():
|
||||||
shutil.copy(icon_path, icon_file)
|
shutil.copy(icon_path, icon_file)
|
||||||
|
|
||||||
self.db.add_color(name, icon_file.exists())
|
self.db.add_color(name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
|
||||||
|
|
||||||
def remove_color(self, name: str):
|
def remove_color(self, name: str):
|
||||||
if (MEDIA_DIR / name).exists():
|
if (MEDIA_DIR / name).exists():
|
||||||
@@ -72,7 +72,7 @@ class MediaRepository:
|
|||||||
shutil.copy(icon_path, icon_file)
|
shutil.copy(icon_path, icon_file)
|
||||||
|
|
||||||
self.db.update_color_name(old_name, new_name)
|
self.db.update_color_name(old_name, new_name)
|
||||||
self.db.update_color_icon_flag(new_name, icon_file.exists())
|
self.db.update_color_icon(new_name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
|
||||||
|
|
||||||
def add_media(self, color: str, file_path: Path):
|
def add_media(self, color: str, file_path: Path):
|
||||||
target_dir = MEDIA_DIR / color
|
target_dir = MEDIA_DIR / color
|
||||||
@@ -83,13 +83,13 @@ class MediaRepository:
|
|||||||
ftype = "photo" if file_path.suffix.lower() in [".jpg", ".png"] else "video"
|
ftype = "photo" if file_path.suffix.lower() in [".jpg", ".png"] else "video"
|
||||||
color_id = self.db.get_color_id(color)
|
color_id = self.db.get_color_id(color)
|
||||||
if color_id is not None:
|
if color_id is not None:
|
||||||
self.db.add_media(color_id, file_path.name, ftype)
|
self.db.add_media(color_id, target_file.as_posix(), ftype)
|
||||||
|
|
||||||
def remove_media(self, color: str, filename: str):
|
def remove_media(self, color: str, file_path: Path):
|
||||||
file_path = MEDIA_DIR / color / filename
|
# file_path = MEDIA_DIR / color / filename
|
||||||
if file_path.exists():
|
if file_path.exists():
|
||||||
file_path.unlink()
|
file_path.unlink()
|
||||||
|
|
||||||
color_id = self.db.get_color_id(color)
|
color_id = self.db.get_color_id(color)
|
||||||
if color_id is not None:
|
if color_id is not None:
|
||||||
self.db.delete_media(color_id, filename)
|
self.db.delete_media(color_id, file_path.as_posix())
|
||||||
|
|||||||
Reference in New Issue
Block a user