Skip to content

Commit

Permalink
Replace custom delete_basket method by a call to Ili2dbUtils in Model…
Browse files Browse the repository at this point in the history
… Baker lib
  • Loading branch information
gacarrillor authored and signedav committed Nov 4, 2024
1 parent d26fba0 commit b8b8bd0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 58 deletions.
64 changes: 7 additions & 57 deletions QgisModelBaker/gui/basket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ def _delete_basket(self) -> None:
== QMessageBox.Yes
):
basket_config = self.baskets_panel.selected_basket_settings()
res, msg = self._do_delete_basket(basket_config)
ili2db_utils = Ili2DbUtils()
ili2db_utils.log_on_error.connect(self._log_on_delete_baskets_error)
res, msg = ili2db_utils.delete_baskets(
basket_config["bid_value"], self.configuration
)

if res:
# After deletion, make sure canvas is refreshed
Expand All @@ -169,59 +173,5 @@ def _refresh_map_layers(self):
layer.dataProvider().reloadData()
layer_tree_view.refreshLayerSymbology(layer.id())

def _do_delete_basket(self, basket_config):
# Keep original values just in case we need to go back to them
original_dataset_id = basket_config["dataset_t_id"]
original_dataset_name = basket_config["datasetname"]

# Create temporary dataset
tmp_dataset_id, tmp_dataset_name = "", "_tmp_dataset_tmp_"
res, msg = self.db_connector.create_dataset(tmp_dataset_name)
for _dataset in self.db_connector.get_datasets_info():
if _dataset["datasetname"] == tmp_dataset_name:
tmp_dataset_id = _dataset["t_id"]
break

if tmp_dataset_id == "":
return False, self.tr(
"Delete basket failed! Internal error modifying dataset/basket tables."
)

# Move basket to temporary dataset
basket_config["dataset_t_id"] = tmp_dataset_id
basket_config["datasetname"] = tmp_dataset_name

res, msg = self.db_connector.edit_basket(basket_config)
if not res:
return False, self.tr(
"Delete basket failed! Internal error modifying basket."
)

# Remove temporary dataset
ili2db_utils = Ili2DbUtils()
ili2db_utils.log_on_error.connect(self._log_on_delete_dataset_error)
res, msg = ili2db_utils.delete_dataset(tmp_dataset_name, self.configuration)

# If anything went bad, leave everything as the original status,
# i.e., move the basket to its original dataset
if not res:
msg = self.tr(
"Delete basket failed! Internal error deleting dataset/basket records."
)
basket_config["dataset_t_id"] = original_dataset_id
basket_config["datasetname"] = original_dataset_name
_res, _msg = self.db_connector.edit_basket(basket_config)
if not _res:
# We shouldn't reach this, the basket is in another dataset!
msg = self.tr("The basket (t_id: {}) couldn't be deleted!").format(
basket_config["basket_t_id"]
)
else:
msg = self.tr("Basket (t_id: {}) successfully deleted!").format(
basket_config["basket_t_id"]
)

return res, msg

def _log_on_delete_dataset_error(self, log):
QgsMessageLog.logMessage(log, self.tr("Delete dataset from DB"), Qgis.Critical)
def _log_on_delete_baskets_error(self, log):
QgsMessageLog.logMessage(log, self.tr("Delete basket from DB"), Qgis.Critical)
2 changes: 1 addition & 1 deletion QgisModelBaker/gui/panel/summary_basket_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def data(self, index, role):
return None

def basket_config_by_index(self, index: QModelIndex) -> dict:
# Return the basket config for the row corrsponding to the given index.
# Return the basket config for the row corresponding to the given index.
# This includes the whole basket configuration (t_id, dataset_id,
# topic, etc.)
return list(self.basket_settings.values())[index.row()]
Expand Down

0 comments on commit b8b8bd0

Please sign in to comment.