From 7747dba026d8b934acc4f11b256b46db43fdaa35 Mon Sep 17 00:00:00 2001 From: Lea Laux Date: Mon, 22 Feb 2021 11:01:00 +0100 Subject: [PATCH] Add csv import dialog to main window Add the csv import dialog to the main window: Import CSV now available in the edit menu and in the tool bar. Change the required database connection in the csv import dialog to database connection parameters and establish the connection there with help of the global connection factory. --- pygadmin/icons/csv.svg | 439 ++++++++++++++++++++++++++++++++ pygadmin/widgets/csv_import.py | 17 +- pygadmin/widgets/main_window.py | 27 +- 3 files changed, 475 insertions(+), 8 deletions(-) create mode 100644 pygadmin/icons/csv.svg diff --git a/pygadmin/icons/csv.svg b/pygadmin/icons/csv.svg new file mode 100644 index 0000000..a3d917c --- /dev/null +++ b/pygadmin/icons/csv.svg @@ -0,0 +1,439 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + csv + diff --git a/pygadmin/widgets/csv_import.py b/pygadmin/widgets/csv_import.py index f6cff94..5b745df 100644 --- a/pygadmin/widgets/csv_import.py +++ b/pygadmin/widgets/csv_import.py @@ -1,11 +1,7 @@ from PyQt5.QtWidgets import QDialog, QGridLayout, QLabel, QPushButton, QMessageBox, QLineEdit, \ QInputDialog -from pygadmin.csv_importer import CSVImporter -from pygadmin.widgets.widget_icon_adder import IconAdder -from PyQt5.QtWidgets import QDialog, QGridLayout, QLabel, QPushButton, QMessageBox, QLineEdit, \ - QInputDialog - +from pygadmin.connectionfactory import global_connection_factory from pygadmin.csv_importer import CSVImporter from pygadmin.widgets.widget_icon_adder import IconAdder @@ -16,12 +12,21 @@ class CSVImportDialog(QDialog): file. User interaction is necessary for editing the names and data types of the column. """ - def __init__(self, database_connection, csv_file): + def __init__(self, host, user, database, port, csv_file): super().__init__() # Add the pygadmin icon as window icon. icon_adder = IconAdder() icon_adder.add_icon_to_widget(self) + # Get the database connection out of the connection factory with the given parameters. + database_connection = global_connection_factory.get_database_connection(host, user, database, port) + + # In an error case, the returned database connection is False or None, so an error is shown to the user and the + # function ends here. + if database_connection is False or database_connection is None: + QMessageBox.critical(self, "Connection Error", "A database connection could not be established.") + return + # Get the result of an input dialog for getting the delimiter of the csv file. delimiter_result = self.init_delimiter_question(csv_file) diff --git a/pygadmin/widgets/main_window.py b/pygadmin/widgets/main_window.py index 834e3ae..759dfbd 100644 --- a/pygadmin/widgets/main_window.py +++ b/pygadmin/widgets/main_window.py @@ -8,6 +8,7 @@ from PyQt5.QtCore import Qt, pyqtSlot import pygadmin from pygadmin.widgets.command_history import CommandHistoryDialog +from pygadmin.widgets.csv_import import CSVImportDialog from pygadmin.widgets.mdi_area import MdiArea from pygadmin.widgets.dock import DockWidget from pygadmin.widgets.connection_dialog import ConnectionDialogWidget @@ -209,6 +210,7 @@ class MainWindow(QMainWindow): self.add_action_to_tool_bar("New Editor", "editor.svg", self.activate_new_editor_tab) # Add a function for activating a new history command dialog. self.add_action_to_tool_bar("Show History", "history.svg", self.activate_command_history_dialog) + self.add_action_to_tool_bar("Import CSV", "csv.svg", self.activate_csv_import) def add_action_to_tool_bar(self, action_description, action_icon_file, connected_function): """ @@ -469,10 +471,20 @@ class MainWindow(QMainWindow): def activate_csv_import(self): """ Activate the necessary steps for starting the csv import dialog. This process includes getting the csv file by - a file dialog and getting the current database connection. TODO: Think about a better place for the importer or - error for missing database connection. + a file dialog and getting the current database connection parameters by the selected node in the tree. """ + # Get the current selected node out of the tree for using the database connection parameters of the node in the + # import dialog. + current_node = self.dock_widget.tree.get_selected_element_by_current_selection() + + # If the current node is None, a node is not chosen, so the connection for the csv importer is missing. + if current_node is None: + # Warn the user about the missing connection and end the function. + QMessageBox.warning(self, "Missing Connection", "Please choose a database connection in the tree for " + "proceeding with the CSV import.") + return + file_name_and_type = QFileDialog.getOpenFileName(self, "Open CSV", "", "CSV (*.csv)") file_name = file_name_and_type[0] @@ -480,6 +492,17 @@ class MainWindow(QMainWindow): if file_name == "": return + # Get the database connection parameters of the given node. + database_connection_parameters = current_node.database_connection_parameters + + # Use the database connection parameters of the node and the given file name by the user for initiating the + # import dialog. + self.csv_import_dialog = CSVImportDialog(database_connection_parameters["host"], + database_connection_parameters["user"], + database_connection_parameters["database"], + database_connection_parameters["port"], + file_name) + @pyqtSlot(str) def show_status_bar_message(self, message): """