Skip to content

Commit

Permalink
fix tag field types, add unique entry constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed Aug 18, 2024
1 parent 30cbcad commit 206e4df
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
10 changes: 7 additions & 3 deletions tagstudio/src/core/library/alchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum
from typing import TYPE_CHECKING, Union, Any

from sqlalchemy import ForeignKey
from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

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

class TagBoxField(Base):
__tablename__ = "tag_box_fields"
__table_args__ = (UniqueConstraint("entry_id", "type", name="uq_entry_id_type"),)

id: Mapped[int] = mapped_column(primary_key=True)
type: Mapped[TagBoxTypes] = mapped_column(default=TagBoxTypes.tag_box)
Expand Down Expand Up @@ -186,9 +187,12 @@ class DefaultField:
DefaultField(name="Description", class_=TextField, type=TextFieldTypes.text_box),
DefaultField(name="Notes", class_=TextField, type=TextFieldTypes.text_box),
DefaultField(name="Comments", class_=TextField, type=TextFieldTypes.text_box),
# 11
DefaultField(name="Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
DefaultField(name="Content Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
DefaultField(name="Meta Tags", class_=TagBoxField, type=TagBoxTypes.tag_box),
DefaultField(
name="Content Tags", class_=TagBoxField, type=TagBoxTypes.tag_content_box
),
DefaultField(name="Meta Tags", class_=TagBoxField, type=TagBoxTypes.meta_tag_box),
DefaultField(name="Date", class_=DatetimeField, type=DateTimeTypes.datetime),
DefaultField(
name="Date Created", class_=DatetimeField, type=DateTimeTypes.datetime
Expand Down
59 changes: 27 additions & 32 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,41 +506,36 @@ def update_field(
raise NotImplementedError

def add_field_to_entry(self, entry: Entry, field_id: int) -> bool:
logger.info("adding field to entry", entry=entry, field_id=field_id)
# TODO - using entry here directly doesnt work, as it's expunged from session
# so the session tries to insert it again which fails

default_field = DEFAULT_FIELDS[field_id]

logger.info("found field type", field_type=default_field.class_)

field: Any
with Session(self.engine) as session, session.begin():
if default_field.class_ == TextField:
field = TextField(
name=default_field.name,
type=default_field.type,
value="",
entry_id=entry.id,
)
# entry.text_fields.append(field)
elif default_field.class_ == TagBoxField:
field = TagBoxField(
name=default_field.name,
type=default_field.type,
entry_id=entry.id,
)
# entry.tag_box_fields.append(field)
elif default_field.class_ == DatetimeField:
field = DatetimeField(
name=default_field.name,
type=default_field.type,
entry_id=entry.id,
)
# entry.datetime_fields.append(field)
else:
raise ValueError("Unknown field.")
logger.info(
"found field type",
entry=entry,
field_id=field_id,
field_type=default_field.class_,
)

if default_field.class_ == TextField:
field = TextField(
name=default_field.name,
type=default_field.type,
value="",
entry_id=entry.id,
)
elif default_field.class_ == TagBoxField:
field = TagBoxField(

Check failure on line 526 in tagstudio/src/core/library/alchemy/library.py

View workflow job for this annotation

GitHub Actions / Run MyPy

[mypy] reported by reviewdog 🐶 Incompatible types in assignment (expression has type "TagBoxField", variable has type "TextField") [assignment] Raw Output: /home/runner/work/TagStudio/TagStudio/tagstudio/src/core/library/alchemy/library.py:526:21: error: Incompatible types in assignment (expression has type "TagBoxField", variable has type "TextField") [assignment]
name=default_field.name,
type=default_field.type,
entry_id=entry.id,
)
elif default_field.class_ == DatetimeField:
field = DatetimeField(

Check failure on line 532 in tagstudio/src/core/library/alchemy/library.py

View workflow job for this annotation

GitHub Actions / Run MyPy

[mypy] reported by reviewdog 🐶 Incompatible types in assignment (expression has type "DatetimeField", variable has type "TextField") [assignment] Raw Output: /home/runner/work/TagStudio/TagStudio/tagstudio/src/core/library/alchemy/library.py:532:21: error: Incompatible types in assignment (expression has type "DatetimeField", variable has type "TextField") [assignment]
name=default_field.name,
type=default_field.type,
entry_id=entry.id,
)

with Session(self.engine) as session:
try:
session.add(field)
session.commit()
Expand Down
1 change: 1 addition & 0 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ def show_file_extension_modal(self):
"File Extensions",
has_save=True,
)

self.modal.saved.connect(
lambda: (
panel.save(),
Expand Down
3 changes: 2 additions & 1 deletion tagstudio/src/qt/widgets/preview_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from src.qt.widgets.text_line_edit import EditTextLine
from src.qt.helpers.qbutton_wrapper import QPushButtonWrapper
from src.qt.widgets.video_player import VideoPlayer
from src.core.library.alchemy.library import Library

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

tags_updated = Signal()

def __init__(self, library, driver: "QtDriver"):
def __init__(self, library: Library, driver: "QtDriver"):
super().__init__()
self.is_connected = False
self.lib = library
Expand Down

0 comments on commit 206e4df

Please sign in to comment.