refactor database and media handling to use icon paths instead of flags

This commit is contained in:
2025-09-07 13:21:29 +02:00
parent 63e6386239
commit 9d60843ec5
2 changed files with 13 additions and 13 deletions

View File

@@ -39,7 +39,7 @@ class DatabaseManager:
CREATE TABLE IF NOT EXISTS colors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
has_icon INTEGER DEFAULT 0
icon_path TEXT NOT NULL
)
"""
)
@@ -62,13 +62,13 @@ class DatabaseManager:
# -------------------------
# 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:
raise RuntimeError("Database not connected")
cur = self.conn.cursor()
cur.execute(
"INSERT OR IGNORE INTO colors (name, has_icon) VALUES (?, ?)",
(name, int(has_icon)),
"INSERT OR IGNORE INTO colors (name, icon_path) VALUES (?, ?)",
(name, icon_path),
)
self.conn.commit()
@@ -95,11 +95,11 @@ class DatabaseManager:
cur.execute("UPDATE colors SET name = ? WHERE name = ?", (new_name, old_name))
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:
raise RuntimeError("Database not connected")
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()
def delete_color(self, name: str):

View File

@@ -31,7 +31,7 @@ class MediaRepository:
continue
color_dir = MEDIA_DIR / color
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"}
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():
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):
if (MEDIA_DIR / name).exists():
@@ -72,7 +72,7 @@ class MediaRepository:
shutil.copy(icon_path, icon_file)
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):
target_dir = MEDIA_DIR / color
@@ -83,13 +83,13 @@ class MediaRepository:
ftype = "photo" if file_path.suffix.lower() in [".jpg", ".png"] else "video"
color_id = self.db.get_color_id(color)
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):
file_path = MEDIA_DIR / color / filename
def remove_media(self, color: str, file_path: Path):
# file_path = MEDIA_DIR / color / filename
if file_path.exists():
file_path.unlink()
color_id = self.db.get_color_id(color)
if color_id is not None:
self.db.delete_media(color_id, filename)
self.db.delete_media(color_id, file_path.as_posix())