Skip to content

Commit fd738d9

Browse files
committed
fix tag field types, add unique entry constraint
1 parent 30cbcad commit fd738d9

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

tagstudio/src/core/library/alchemy/fields.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from enum import Enum
66
from typing import TYPE_CHECKING, Union, Any
77

8-
from sqlalchemy import ForeignKey
8+
from sqlalchemy import ForeignKey, UniqueConstraint
99
from sqlalchemy.orm import Mapped, mapped_column, relationship
1010

1111
from .db import Base
@@ -79,6 +79,7 @@ def __eq__(self, value: object) -> bool:
7979

8080
class TagBoxField(Base):
8181
__tablename__ = "tag_box_fields"
82+
__table_args__ = (UniqueConstraint("entry_id", "type", name="uq_entry_id_type"),)
8283

8384
id: Mapped[int] = mapped_column(primary_key=True)
8485
type: Mapped[TagBoxTypes] = mapped_column(default=TagBoxTypes.tag_box)
@@ -186,9 +187,12 @@ class DefaultField:
186187
DefaultField(name="Description", class_=TextField, type=TextFieldTypes.text_box),
187188
DefaultField(name="Notes", class_=TextField, type=TextFieldTypes.text_box),
188189
DefaultField(name="Comments", class_=TextField, type=TextFieldTypes.text_box),
190+
# 11
189191
DefaultField(name="Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
190-
DefaultField(name="Content Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
191-
DefaultField(name="Meta Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
192+
DefaultField(
193+
name="Content Tags", class_=TagBoxField, type=TagBoxTypes.tag_content_box
194+
),
195+
DefaultField(name="Meta Tags", class_=TagBoxField, type=TagBoxTypes.meta_tag_box),
192196
DefaultField(name="Date", class_=DatetimeField, type=DateTimeTypes.datetime),
193197
DefaultField(
194198
name="Date Created", class_=DatetimeField, type=DateTimeTypes.datetime

tagstudio/src/core/library/alchemy/library.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -506,41 +506,37 @@ def update_field(
506506
raise NotImplementedError
507507

508508
def add_field_to_entry(self, entry: Entry, field_id: int) -> bool:
509-
logger.info("adding field to entry", entry=entry, field_id=field_id)
510-
# TODO - using entry here directly doesnt work, as it's expunged from session
511-
# so the session tries to insert it again which fails
512-
513509
default_field = DEFAULT_FIELDS[field_id]
514510

515-
logger.info("found field type", field_type=default_field.class_)
516-
517-
field: Any
518-
with Session(self.engine) as session, session.begin():
519-
if default_field.class_ == TextField:
520-
field = TextField(
521-
name=default_field.name,
522-
type=default_field.type,
523-
value="",
524-
entry_id=entry.id,
525-
)
526-
# entry.text_fields.append(field)
527-
elif default_field.class_ == TagBoxField:
528-
field = TagBoxField(
529-
name=default_field.name,
530-
type=default_field.type,
531-
entry_id=entry.id,
532-
)
533-
# entry.tag_box_fields.append(field)
534-
elif default_field.class_ == DatetimeField:
535-
field = DatetimeField(
536-
name=default_field.name,
537-
type=default_field.type,
538-
entry_id=entry.id,
539-
)
540-
# entry.datetime_fields.append(field)
541-
else:
542-
raise ValueError("Unknown field.")
511+
logger.info(
512+
"found field type",
513+
entry=entry,
514+
field_id=field_id,
515+
field_type=default_field.class_,
516+
)
517+
518+
field: Any # make mypy happy
519+
if default_field.class_ == TextField:
520+
field = TextField(
521+
name=default_field.name,
522+
type=default_field.type,
523+
value="",
524+
entry_id=entry.id,
525+
)
526+
elif default_field.class_ == TagBoxField:
527+
field = TagBoxField(
528+
name=default_field.name,
529+
type=default_field.type,
530+
entry_id=entry.id,
531+
)
532+
elif default_field.class_ == DatetimeField:
533+
field = DatetimeField(
534+
name=default_field.name,
535+
type=default_field.type,
536+
entry_id=entry.id,
537+
)
543538

539+
with Session(self.engine) as session:
544540
try:
545541
session.add(field)
546542
session.commit()

tagstudio/src/qt/ts_qt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ def show_file_extension_modal(self):
707707
"File Extensions",
708708
has_save=True,
709709
)
710+
710711
self.modal.saved.connect(
711712
lambda: (
712713
panel.save(),

tagstudio/src/qt/widgets/preview_panel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from src.qt.widgets.text_line_edit import EditTextLine
4343
from src.qt.helpers.qbutton_wrapper import QPushButtonWrapper
4444
from src.qt.widgets.video_player import VideoPlayer
45+
from src.core.library.alchemy.library import Library
4546

4647
# Only import for type checking/autocompletion, will not be imported at runtime.
4748
if typing.TYPE_CHECKING:
@@ -95,7 +96,7 @@ class PreviewPanel(QWidget):
9596

9697
tags_updated = Signal()
9798

98-
def __init__(self, library, driver: "QtDriver"):
99+
def __init__(self, library: Library, driver: "QtDriver"):
99100
super().__init__()
100101
self.is_connected = False
101102
self.lib = library

0 commit comments

Comments
 (0)