Skip to content

Commit

Permalink
refactoring: centralize field IDs (#157)
Browse files Browse the repository at this point in the history
* use enum with named fields instead of ad-hoc numbers

* move tag ids into constants file
  • Loading branch information
yedpodtrzitko committed Jun 16, 2024
1 parent 5c25666 commit 4c6ebec
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 50 deletions.
3 changes: 3 additions & 0 deletions tagstudio/src/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@
"cool gray",
"olive",
]

TAG_FAVORITE = 1
TAG_ARCHIVED = 0
13 changes: 13 additions & 0 deletions tagstudio/src/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ class SearchMode(int, enum.Enum):

AND = 0
OR = 1


class FieldID(int, enum.Enum):
TITLE = 0
AUTHOR = 1
ARTIST = 2
DESCRIPTION = 4
NOTES = 5
TAGS = 6
CONTENT_TAGS = 7
META_TAGS = 8
DATE_PUBLISHED = 14
SOURCE = 21
61 changes: 26 additions & 35 deletions tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""The Library object and related methods for TagStudio."""

import datetime
import json
import logging
import os
import time
Expand All @@ -18,6 +17,7 @@
from typing import cast, Generator
from typing_extensions import Self

from src.core.enums import FieldID
from src.core.json_typing import JsonCollation, JsonEntry, JsonLibary, JsonTag
from src.core.utils.str import strip_punctuation
from src.core.utils.web import strip_web_protocol
Expand Down Expand Up @@ -1948,48 +1948,44 @@ def add_generic_data_to_entry(self, data, entry_id: int):
if data:
# Add a Title Field if the data doesn't already exist.
if data.get("title"):
field_id = 0 # Title Field ID
if not self.does_field_content_exist(entry_id, field_id, data["title"]):
self.add_field_to_entry(entry_id, field_id)
if not self.does_field_content_exist(
entry_id, FieldID.TITLE, data["title"]
):
self.add_field_to_entry(entry_id, FieldID.TITLE)
self.update_entry_field(entry_id, -1, data["title"], "replace")

# Add an Author Field if the data doesn't already exist.
if data.get("author"):
field_id = 1 # Author Field ID
if not self.does_field_content_exist(
entry_id, field_id, data["author"]
entry_id, FieldID.AUTHOR, data["author"]
):
self.add_field_to_entry(entry_id, field_id)
self.add_field_to_entry(entry_id, FieldID.AUTHOR)
self.update_entry_field(entry_id, -1, data["author"], "replace")

# Add an Artist Field if the data doesn't already exist.
if data.get("artist"):
field_id = 2 # Artist Field ID
if not self.does_field_content_exist(
entry_id, field_id, data["artist"]
entry_id, FieldID.ARTIST, data["artist"]
):
self.add_field_to_entry(entry_id, field_id)
self.add_field_to_entry(entry_id, FieldID.ARTIST)
self.update_entry_field(entry_id, -1, data["artist"], "replace")

# Add a Date Published Field if the data doesn't already exist.
if data.get("date_published"):
field_id = 14 # Date Published Field ID
date = str(
datetime.datetime.strptime(
data["date_published"], "%Y-%m-%d %H:%M:%S"
)
)
if not self.does_field_content_exist(entry_id, field_id, date):
self.add_field_to_entry(entry_id, field_id)
if not self.does_field_content_exist(
entry_id, FieldID.DATE_PUBLISHED, date
):
self.add_field_to_entry(entry_id, FieldID.DATE_PUBLISHED)
# entry = self.entries[entry_id]
self.update_entry_field(entry_id, -1, date, "replace")

# Process String Tags if the data doesn't already exist.
if data.get("tags"):
tags_field_id = 6 # Tags Field ID
content_tags_field_id = 7 # Content Tags Field ID
meta_tags_field_id = 8 # Meta Tags Field ID
notes_field_id = 5 # Notes Field ID
tags: list[str] = data["tags"]
# extra: list[str] = []
# for tag in tags:
Expand Down Expand Up @@ -2038,7 +2034,7 @@ def add_generic_data_to_entry(self, data, entry_id: int):
# tag_field_indices = self.get_field_index_in_entry(
# entry_index, tags_field_id)
content_tags_field_indices = self.get_field_index_in_entry(
self.get_entry(entry_id), content_tags_field_id
self.get_entry(entry_id), FieldID.CONTENT_TAGS
)
# meta_tags_field_indices = self.get_field_index_in_entry(
# entry_index, meta_tags_field_id)
Expand All @@ -2055,45 +2051,40 @@ def add_generic_data_to_entry(self, data, entry_id: int):
entry_id, priority_field_index, [matching[0]], "append"
)
else:
self.add_field_to_entry(entry_id, content_tags_field_id)
self.add_field_to_entry(entry_id, FieldID.CONTENT_TAGS)
self.update_entry_field(
entry_id, -1, [matching[0]], "append"
)

# Add all original string tags as a note.
str_tags = f"Original Tags: {tags}"
if not self.does_field_content_exist(
entry_id, notes_field_id, str_tags
):
self.add_field_to_entry(entry_id, notes_field_id)
if not self.does_field_content_exist(entry_id, FieldID.NOTES, str_tags):
self.add_field_to_entry(entry_id, FieldID.NOTES)
self.update_entry_field(entry_id, -1, str_tags, "replace")

# Add a Description Field if the data doesn't already exist.
if "description" in data.keys() and data["description"]:
field_id = 4 # Description Field ID
if data.get("description"):
if not self.does_field_content_exist(
entry_id, field_id, data["description"]
entry_id, FieldID.DESCRIPTION, data["description"]
):
self.add_field_to_entry(entry_id, field_id)
self.add_field_to_entry(entry_id, FieldID.DESCRIPTION)
self.update_entry_field(
entry_id, -1, data["description"], "replace"
)
if "content" in data.keys() and data["content"]:
field_id = 4 # Description Field ID
if data.get("content"):
if not self.does_field_content_exist(
entry_id, field_id, data["content"]
entry_id, FieldID.DESCRIPTION, data["content"]
):
self.add_field_to_entry(entry_id, field_id)
self.add_field_to_entry(entry_id, FieldID.DESCRIPTION)
self.update_entry_field(entry_id, -1, data["content"], "replace")
if "source" in data.keys() and data["source"]:
field_id = 21 # Source Field ID
if data.get("source"):
for source in data["source"].split(" "):
if source and source != " ":
source = strip_web_protocol(string=source)
if not self.does_field_content_exist(
entry_id, field_id, source
entry_id, FieldID.SOURCE, source
):
self.add_field_to_entry(entry_id, field_id)
self.add_field_to_entry(entry_id, FieldID.SOURCE)
self.update_entry_field(entry_id, -1, source, "replace")

def add_field_to_entry(self, entry_id: int, field_id: int) -> None:
Expand Down
1 change: 1 addition & 0 deletions tagstudio/src/core/ts_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os
from pathlib import Path
from enum import Enum

from src.core.library import Entry, Library
from src.core.constants import TS_FOLDER_NAME, TEXT_FIELDS
Expand Down
3 changes: 2 additions & 1 deletion tagstudio/src/qt/modals/folders_to_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
QFrame,
)

from src.core.enums import FieldID
from src.core.library import Library, Tag
from src.core.palette import ColorType, get_tag_color
from src.qt.flowlayout import FlowLayout
Expand Down Expand Up @@ -73,7 +74,7 @@ def add_folders_to_tree(items: list[str]) -> Tag:
tag = add_folders_to_tree(folders)
if tag:
if not entry.has_tag(library, tag.id):
entry.add_tag(library, tag.id, 6)
entry.add_tag(library, tag.id, FieldID.TAGS)

logging.info("Done")

Expand Down
6 changes: 4 additions & 2 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
TS_FOLDER_NAME,
VERSION_BRANCH,
VERSION,
TAG_FAVORITE,
TAG_ARCHIVED,
)
from src.core.utils.web import strip_web_protocol
from src.qt.flowlayout import FlowLayout
Expand Down Expand Up @@ -1252,8 +1254,8 @@ def update_thumbs(self):
filepath = self.lib.library_dir / entry.path / entry.filename

item_thumb.set_item_id(entry.id)
item_thumb.assign_archived(entry.has_tag(self.lib, 0))
item_thumb.assign_favorite(entry.has_tag(self.lib, 1))
item_thumb.assign_archived(entry.has_tag(self.lib, TAG_ARCHIVED))
item_thumb.assign_favorite(entry.has_tag(self.lib, TAG_FAVORITE))
# ctrl_down = True if QGuiApplication.keyboardModifiers() else False
# TODO: Change how this works. The click function
# for collations a few lines down should NOT be allowed during modifier keys.
Expand Down
24 changes: 15 additions & 9 deletions tagstudio/src/qt/widgets/item_thumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time
import typing
from types import FunctionType
from pathlib import Path
from typing import Optional

Expand All @@ -23,9 +22,15 @@
QCheckBox,
)


from src.core.enums import FieldID
from src.core.library import ItemType, Library, Entry
from src.core.constants import AUDIO_TYPES, VIDEO_TYPES, IMAGE_TYPES
from src.core.constants import (
AUDIO_TYPES,
VIDEO_TYPES,
IMAGE_TYPES,
TAG_FAVORITE,
TAG_ARCHIVED,
)
from src.qt.flowlayout import FlowWidget
from src.qt.helpers.file_opener import FileOpenerHelper
from src.qt.widgets.thumb_renderer import ThumbRenderer
Expand All @@ -38,9 +43,6 @@
WARNING = f"[WARNING]"
INFO = f"[INFO]"

DEFAULT_META_TAG_FIELD = 8
TAG_FAVORITE = 1
TAG_ARCHIVED = 0

logging.basicConfig(format="%(message)s", level=logging.INFO)

Expand Down Expand Up @@ -405,8 +407,12 @@ def update_badges(self):
if self.mode == ItemType.ENTRY:
# logging.info(f'[UPDATE BADGES] ENTRY: {self.lib.get_entry(self.item_id)}')
# logging.info(f'[UPDATE BADGES] ARCH: {self.lib.get_entry(self.item_id).has_tag(self.lib, 0)}, FAV: {self.lib.get_entry(self.item_id).has_tag(self.lib, 1)}')
self.assign_archived(self.lib.get_entry(self.item_id).has_tag(self.lib, 0))
self.assign_favorite(self.lib.get_entry(self.item_id).has_tag(self.lib, 1))
self.assign_archived(
self.lib.get_entry(self.item_id).has_tag(self.lib, TAG_ARCHIVED)
)
self.assign_favorite(
self.lib.get_entry(self.item_id).has_tag(self.lib, TAG_FAVORITE)
)

def set_item_id(self, id: int):
"""
Expand Down Expand Up @@ -475,7 +481,7 @@ def toggle_tag(entry: Entry):
entry.add_tag(
self.panel.driver.lib,
tag_id,
field_id=DEFAULT_META_TAG_FIELD,
field_id=FieldID.META_TAGS,
field_index=-1,
)
else:
Expand Down
7 changes: 4 additions & 3 deletions tagstudio/src/qt/widgets/tag_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from PySide6.QtCore import Signal, Qt
from PySide6.QtWidgets import QPushButton

from src.core.constants import TAG_FAVORITE, TAG_ARCHIVED
from src.core.library import Library, Tag
from src.qt.flowlayout import FlowLayout
from src.qt.widgets.fields import FieldWidget
Expand Down Expand Up @@ -141,7 +142,7 @@ def edit_tag(self, tag_id: int):
# panel.tag_updated.connect(lambda tag: self.lib.update_tag(tag))
self.edit_modal.show()

def add_tag_callback(self, tag_id):
def add_tag_callback(self, tag_id: int):
# self.base_layout.addWidget(TagWidget(self.lib, self.lib.get_tag(tag), True))
# self.tags.append(tag)
logging.info(
Expand All @@ -154,7 +155,7 @@ def add_tag_callback(self, tag_id):
self.driver.lib, tag_id, field_id=id, field_index=-1
)
self.updated.emit()
if tag_id == 0 or tag_id == 1:
if tag_id in (TAG_FAVORITE, TAG_ARCHIVED):
self.driver.update_badges()

# if type((x[0]) == ThumbButton):
Expand All @@ -180,7 +181,7 @@ def remove_tag(self, tag_id: int):
self.driver.lib, tag_id, field_index=index[0]
)
self.updated.emit()
if tag_id == 0 or tag_id == 1:
if tag_id in (TAG_FAVORITE, TAG_ARCHIVED):
self.driver.update_badges()

# def show_add_button(self, value:bool):
Expand Down

0 comments on commit 4c6ebec

Please sign in to comment.