From bcc39c5147fef9694a6db4056d54a742b160e1fa Mon Sep 17 00:00:00 2001 From: Peery Date: Mon, 9 Jan 2023 22:21:35 +0100 Subject: [PATCH] Fixed an Issue with implications and alias editing Fixed a crash when implications or aliases were edited the new dict format of the database caused crashes. --- ArtNet/db/db_adapter.py | 36 ++++++--------- .../tag_modify_dialog/tag_mod_dialog.py | 46 ++++++++++--------- .../tag_select_dialog/tag_select_dialog.py | 21 +++++---- ArtNet/gui/importer_window.py | 13 ++---- 4 files changed, 54 insertions(+), 62 deletions(-) diff --git a/ArtNet/db/db_adapter.py b/ArtNet/db/db_adapter.py index 4e87a18..2da7309 100644 --- a/ArtNet/db/db_adapter.py +++ b/ArtNet/db/db_adapter.py @@ -100,7 +100,7 @@ class DBAdapter: old_tags = [self.get_tag_by_ID(x)["name"].strip() for x in self.get_art_tags_by_ID(ID)] for tag in tags: - if tag in old_tags: # already present + if tag["name"] in old_tags: # already present continue d = {"id": ID, "tag": tag["id"]} try: @@ -600,14 +600,8 @@ class DBAdapter: rows = self.db_cursor.fetchall() new_rows = [] for i in range(len(rows)): - new_rows.append([]) - for j in range(len(rows[i])): - if rows[i][j] is None: - new_rows[i].append("") - elif isinstance(rows[i][j], str): - new_rows[i].append(rows[i][j].strip()) - else: - new_rows[i].append(rows[i][j]) + new_rows.append({"name": rows[i][0].strip(), "description": rows[i][1].strip(), + "category": int(rows[i][2]), "id": int(rows[i][3])}) return new_rows @@ -662,8 +656,9 @@ class DBAdapter: :return: """ name = name.strip().lower() - implications = [self.get_tag_ID(x) for x in implications] - aliases = [self.get_tag_ID(x) for x in aliases] + logging.debug(f"Editing Tag name={name}, desc={description}, aliases={aliases}, implications={implications}, category={category_id}") + #implications = [self.get_tag_ID(x) for x in implications] + #aliases = [self.get_tag_ID(x) for x in aliases] d = { "id": tag_id, "name": name, @@ -681,23 +676,23 @@ class DBAdapter: if aliases is not None: old_aliases = self.get_tag_aliases_by_ID(tag_id) for alias in aliases: - if alias in old_aliases: # is this already set? + if alias["id"] in old_aliases: # is this already set? continue - self.add_alias_by_ID(tag_id, alias) + self.add_alias_by_ID(tag_id, alias["id"]) for old_alias in old_aliases: - if old_alias not in aliases: # got to delete an alias? + if old_alias not in [a["id"] for a in aliases]: # got to delete an alias? self.remove_alias_by_ID(tag_id, old_alias) if implications is not None: old_implicants = self.get_tag_implications_by_ID(tag_id) for implicant in implications: - if implicant in old_implicants: # is this already set? + if implicant["id"] in old_implicants: # is this already set? continue - self.add_implication_by_ID(tag_id, implicant) + self.add_implication_by_ID(tag_id, implicant["id"]) for old_implicant in old_implicants: - if old_implicant not in implications: # got to delete an implicant? + if old_implicant not in [i["id"] for i in implications]: # got to delete an implicant? self.remove_implication_by_ID(tag_id, old_implicant) def add_alias_by_name(self, name: str, alias: str): @@ -1002,7 +997,6 @@ class DBAdapter: """ Search for the tag's implications :param name: - :param output_ID: Don't resolve the tag ids into names in the resulting list :return: List of tag ids """ d = {"ID": self.get_tag_ID(name)} @@ -1021,11 +1015,7 @@ class DBAdapter: result = [self.get_tag_by_ID(v) for v in r] - r = [] - for value in result: - r.append(value[0][1].strip()) - - return r + return result def get_tag_impliers_by_ID(self, ID: int): """ diff --git a/ArtNet/gui/dialogs/tag_modify_dialog/tag_mod_dialog.py b/ArtNet/gui/dialogs/tag_modify_dialog/tag_mod_dialog.py index a315293..705e77c 100644 --- a/ArtNet/gui/dialogs/tag_modify_dialog/tag_mod_dialog.py +++ b/ArtNet/gui/dialogs/tag_modify_dialog/tag_mod_dialog.py @@ -1,3 +1,5 @@ +import logging +from typing import List from PyQt5 import QtWidgets from PyQt5.QtGui import QStandardItemModel, QStandardItem from PyQt5.QtCore import Qt @@ -47,12 +49,12 @@ class TagModifyDialog(QtWidgets.QDialog): item_model = QStandardItemModel(self.ui.tag_alias_search_list) for tag in aliases: - tag = tag[0].strip() - if tag == self.ui.tag_name_line.text(): + if tag["name"] == self.ui.tag_name_line.text(): continue - item = QStandardItem(tag) + item = QStandardItem(tag["name"]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) + item.setData(tag, Qt.UserRole) if set_checked: item.setCheckState(Qt.Checked) @@ -72,12 +74,13 @@ class TagModifyDialog(QtWidgets.QDialog): item_model = QStandardItemModel(self.ui.tag_implication_search_list) for tag in implications: - tag = tag[0].strip() - if tag == self.ui.tag_name_line.text(): + if tag["name"] == self.ui.tag_name_line.text(): continue - item = QStandardItem(tag) + item = QStandardItem(tag["name"]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) + item.setData(tag, Qt.UserRole) + if set_checked: item.setCheckState(Qt.Checked) @@ -119,12 +122,12 @@ class TagModifyDialog(QtWidgets.QDialog): item_model = QStandardItemModel(self.ui.tag_alias_selection_list) for tag in aliases: - tag = tag.strip() - if tag == self.ui.tag_name_line.text(): + if tag["name"] == self.ui.tag_name_line.text(): continue - item = QStandardItem(tag) + item = QStandardItem(tag["name"]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) + item.setData(tag, Qt.UserRole) if set_checked: item.setCheckState(Qt.Checked) @@ -134,7 +137,7 @@ class TagModifyDialog(QtWidgets.QDialog): item_model.itemChanged.connect(self.on_selected_alias_tag_item_changed) self.ui.tag_alias_selection_list.setModel(item_model) - def set_selected_implicated_tags(self, implications: list, set_checked: bool=False): + def set_selected_implicated_tags(self, implications: List[dict], set_checked: bool=False): """ Set the tags in the search result list to tags :param implications: @@ -144,12 +147,12 @@ class TagModifyDialog(QtWidgets.QDialog): item_model = QStandardItemModel(self.ui.tag_implication_search_list) for tag in implications: - tag = tag.strip() - if tag == self.ui.tag_name_line.text(): + if tag["name"] == self.ui.tag_name_line.text(): continue - item = QStandardItem(tag) + item = QStandardItem(tag["name"]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) + item.setData(tag, Qt.UserRole) if set_checked: item.setCheckState(Qt.Checked) @@ -184,13 +187,14 @@ class TagModifyDialog(QtWidgets.QDialog): self.set_search_implicated_tags(tags) def on_search_implicated_tag_item_changed(self, item: QStandardItem): + logging.debug(f"Implication \"{item.text()}\" was clicked with data: "+str(item.data(Qt.UserRole))) if item.checkState() == Qt.Checked: if self.implication_selection is None: self.implication_selection = [] - self.implication_selection.append(item.text()) + self.implication_selection.append(item.data(Qt.UserRole)) elif item.checkState() == Qt.Unchecked: - if item.text() in self.implication_selection: - self.implication_selection.remove(item.text()) + if item.text() in [x["name"] for x in self.implication_selection]: + self.implication_selection.remove(item.data(Qt.UserRole)) self.set_selected_implicated_tags(self.implication_selection, set_checked=True) @@ -198,17 +202,17 @@ class TagModifyDialog(QtWidgets.QDialog): if item.checkState() == Qt.Checked: if self.alias_selection is None: self.alias_selection = [] - self.alias_selection.append(item.text()) + self.alias_selection.append(item.data(Qt.UserRole)) elif item.checkState() == Qt.Unchecked: - if item.text() in self.alias_selection: - self.alias_selection.remove(item.text()) + if item.text() in [x["name"] for x in self.alias_selection]: + self.alias_selection.remove(item.data(Qt.UserRole)) self.set_selected_alias_tags(self.alias_selection, set_checked=True) def on_selected_implicated_tag_item_changed(self, item: QStandardItem): if self.implication_selection is None: self.implication_selection = [] - self.implication_selection.remove(item.text()) + self.implication_selection.remove(item.data(Qt.UserRole)) self.set_selected_implicated_tags(self.implication_selection, set_checked=True) if self.tag_implication_search_result is not None: @@ -217,7 +221,7 @@ class TagModifyDialog(QtWidgets.QDialog): def on_selected_alias_tag_item_changed(self, item: QStandardItem): if self.alias_selection is None: self.alias_selection = [] - self.alias_selection.remove(item.text()) + self.alias_selection.remove(item.data(Qt.UserRole)) self.set_selected_alias_tags(self.alias_selection, set_checked=True) self.set_search_alias_tags(self.tag_alias_search_result) diff --git a/ArtNet/gui/dialogs/tag_select_dialog/tag_select_dialog.py b/ArtNet/gui/dialogs/tag_select_dialog/tag_select_dialog.py index 5c38307..fa31b9f 100644 --- a/ArtNet/gui/dialogs/tag_select_dialog/tag_select_dialog.py +++ b/ArtNet/gui/dialogs/tag_select_dialog/tag_select_dialog.py @@ -1,3 +1,5 @@ +from typing import List + from PyQt5 import QtWidgets from PyQt5.QtGui import QStandardItemModel, QStandardItem from PyQt5.QtCore import Qt @@ -41,7 +43,7 @@ class TagSelectDialog(QtWidgets.QDialog): self.selected_item.setCheckState(Qt.Unchecked) self.selected_item = item - def set_search_result_list(self, tags: list): + def set_search_result_list(self, tags: List[dict]): """ Set the tags in the search result list to tags :param tags: @@ -50,9 +52,10 @@ class TagSelectDialog(QtWidgets.QDialog): item_model = QStandardItemModel(self.ui.search_result_list) for tag in tags: - item = QStandardItem(tag[0]) + item = QStandardItem(tag["name"]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) + item.setData(tag, Qt.UserRole) item_model.appendRow(item) @@ -71,14 +74,14 @@ class TagSelectDialog(QtWidgets.QDialog): self.selection = item_data[0] elif self.selection is None: # nothing selected return None - tag = self.parent.get_tag(self.selection)[0] + tag = self.parent.get_tag(self.selection) tag_data = { - "ID": tag[3], - "name": tag[0], - "description": tag[1], - "aliases": self.parent.get_tag_aliases(tag[0]), - "implications": self.parent.get_tag_implications(tag[0]), - "category_id": tag[2] + "ID": tag["id"], + "name": tag["name"], + "description": tag["description"], + "aliases": self.parent.get_tag_aliases(tag["name"]), + "implications": self.parent.get_tag_implications(tag["name"]), + "category_id": tag["category"], } print("TagSelectDialog selected tag:", tag_data) return tag_data diff --git a/ArtNet/gui/importer_window.py b/ArtNet/gui/importer_window.py index 2355dbc..9deb4dd 100644 --- a/ArtNet/gui/importer_window.py +++ b/ArtNet/gui/importer_window.py @@ -965,7 +965,7 @@ class ImporterWindow(ArtnetMainWindow): if tag_data is None or len(tag_data.keys()) == 0: # got canceled? return - if len(self.get_tag(tag_data["name"])) > 0: + if self.get_tag(tag_data["name"]) is not None: # check if tag exists QtWidgets.QMessageBox.information(self, "Duplicate Tag", "The tag \"{0}\" already exists in the db!" .format(tag_data["name"])) return @@ -1039,8 +1039,8 @@ class ImporterWindow(ArtnetMainWindow): if tag is None or len(tag) == 0: return - tag['aliases'] = self.main.db_connection.get_tag_aliases_by_name(tag["name"]) - tag['implications'] = self.main.db_connection.get_tag_implications(tag["name"]) + #tag['aliases'] = self.main.db_connection.get_tag_aliases_by_name(tag["name"]) + #tag['implications'] = self.main.db_connection.get_tag_implications(tag["name"]) edit_dialog = TagModifyDialog(self, create_tag=False) edit_dialog.ui.tag_name_line.setText(tag["name"]) @@ -1125,12 +1125,7 @@ class ImporterWindow(ArtnetMainWindow): def on_tag_search_change(self): tags = self.main.db_connection.search_fuzzy_tag(self.ui.tag_search_bar.text(), all_if_empty=True) - - result = [] - for tag_name, tag_desc, tag_category, tag_id in tags: - result.append({"name": tag_name, "description": tag_desc, "category": tag_category, "id": tag_id}) - - self.set_tag_search_result_list(result) + self.set_tag_search_result_list(tags) def on_category_creation_clicked(self): dialog = CategoryModDialog(self, delete_category=False)