mirror of
https://github.com/TagStudioDev/TagStudio.git
synced 2026-02-01 15:49:09 +00:00
refactoring: centralize field IDs (#157)
* use enum with named fields instead of ad-hoc numbers * move tag ids into constants file
This commit is contained in:
@@ -163,3 +163,6 @@ TAG_COLORS = [
|
||||
"cool gray",
|
||||
"olive",
|
||||
]
|
||||
|
||||
TAG_FAVORITE = 1
|
||||
TAG_ARCHIVED = 0
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
"""The Library object and related methods for TagStudio."""
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
@@ -18,6 +17,7 @@ from pathlib import Path
|
||||
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
|
||||
@@ -1948,48 +1948,44 @@ class Library:
|
||||
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:
|
||||
@@ -2038,7 +2034,7 @@ class Library:
|
||||
# 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)
|
||||
@@ -2055,45 +2051,40 @@ class Library:
|
||||
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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -18,6 +18,7 @@ from PySide6.QtWidgets import (
|
||||
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
|
||||
@@ -73,7 +74,7 @@ def folders_to_tags(library: Library):
|
||||
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")
|
||||
|
||||
|
||||
@@ -64,6 +64,8 @@ from src.core.constants import (
|
||||
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
|
||||
@@ -1252,8 +1254,8 @@ class QtDriver(QObject):
|
||||
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.
|
||||
|
||||
@@ -7,7 +7,6 @@ import logging
|
||||
import os
|
||||
import time
|
||||
import typing
|
||||
from types import FunctionType
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -23,9 +22,15 @@ from PySide6.QtWidgets import (
|
||||
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
|
||||
@@ -38,9 +43,6 @@ ERROR = f"[ERROR]"
|
||||
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)
|
||||
|
||||
@@ -405,8 +407,12 @@ class ItemThumb(FlowWidget):
|
||||
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):
|
||||
"""
|
||||
@@ -475,7 +481,7 @@ class ItemThumb(FlowWidget):
|
||||
entry.add_tag(
|
||||
self.panel.driver.lib,
|
||||
tag_id,
|
||||
field_id=DEFAULT_META_TAG_FIELD,
|
||||
field_id=FieldID.META_TAGS,
|
||||
field_index=-1,
|
||||
)
|
||||
else:
|
||||
|
||||
@@ -10,6 +10,7 @@ import typing
|
||||
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
|
||||
@@ -141,7 +142,7 @@ class TagBoxWidget(FieldWidget):
|
||||
# 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(
|
||||
@@ -154,7 +155,7 @@ class TagBoxWidget(FieldWidget):
|
||||
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):
|
||||
@@ -180,7 +181,7 @@ class TagBoxWidget(FieldWidget):
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user