refactor: consolidate reserved tag values as ints

This commit is contained in:
Travis Abendshien
2024-12-20 16:24:04 -08:00
parent d6280f7ead
commit 36c1c180b3
3 changed files with 12 additions and 8 deletions

View File

@@ -13,4 +13,5 @@ FONT_SAMPLE_SIZES: list[int] = [10, 15, 20]
TAG_FAVORITE = 1
TAG_ARCHIVED = 0
RESERVED_TAG_IDS = range(0, 999)
RESERVED_TAG_START = 0
RESERVED_TAG_END = 999

View File

@@ -3,6 +3,7 @@ from pathlib import Path
import structlog
from sqlalchemy import Dialect, Engine, String, TypeDecorator, create_engine, text
from sqlalchemy.orm import DeclarativeBase
from src.core.constants import RESERVED_TAG_END
logger = structlog.getLogger(__name__)
@@ -41,9 +42,11 @@ def make_tables(engine: Engine) -> None:
with engine.connect() as conn:
result = conn.execute(text("SELECT SEQ FROM sqlite_sequence WHERE name='tags'"))
autoincrement_val = result.scalar()
if not autoincrement_val or autoincrement_val < 1000:
conn.execute(text("INSERT INTO tags (id, name, color) VALUES (999, 'temp', 1)"))
conn.execute(text("DELETE FROM tags WHERE id = 999"))
if not autoincrement_val or autoincrement_val <= RESERVED_TAG_END:
conn.execute(
text(f"INSERT INTO tags (id, name, color) VALUES ({RESERVED_TAG_END}, 'temp', 1)")
)
conn.execute(text(f"DELETE FROM tags WHERE id = {RESERVED_TAG_END}"))
conn.commit()

View File

@@ -15,7 +15,7 @@ from PySide6.QtWidgets import (
QVBoxLayout,
QWidget,
)
from src.core.constants import RESERVED_TAG_IDS
from src.core.constants import RESERVED_TAG_END, RESERVED_TAG_START
from src.core.library import Library, Tag
from src.qt.modals.build_tag import BuildTagPanel
from src.qt.widgets.panel import PanelModal, PanelWidget
@@ -117,7 +117,7 @@ class TagDatabasePanel(PanelWidget):
row.setContentsMargins(0, 0, 0, 0)
row.setSpacing(3)
if tag.id in RESERVED_TAG_IDS:
if tag.id in range(RESERVED_TAG_START, RESERVED_TAG_END):
tag_widget = TagWidget(tag, has_edit=False, has_remove=False)
else:
tag_widget = TagWidget(tag, has_edit=True, has_remove=True)
@@ -130,7 +130,7 @@ class TagDatabasePanel(PanelWidget):
self.search_field.setFocus()
def remove_tag(self, tag: Tag):
if tag.id in RESERVED_TAG_IDS:
if tag.id in range(RESERVED_TAG_START, RESERVED_TAG_END):
return
message_box = QMessageBox()
@@ -148,7 +148,7 @@ class TagDatabasePanel(PanelWidget):
self.update_tags()
def edit_tag(self, tag: Tag):
if tag.id in RESERVED_TAG_IDS:
if tag.id in range(RESERVED_TAG_START, RESERVED_TAG_END):
return
build_tag_panel = BuildTagPanel(self.lib, tag=tag)