Skip to content

Commit

Permalink
Add validation for connection parameters when editing or creating (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ecuberojimenez authored Nov 20, 2024
1 parent 0089b06 commit 45cc468
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
58 changes: 54 additions & 4 deletions ui/sf_connection_string_dialog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from PyQt5.QtWidgets import QWidget
from qgis.core import Qgis
from qgis.gui import QgsMessageBar
from qgis.gui import QgsMessageBar, QgsAuthSettingsWidget
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QSettings, pyqtSignal
from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtWidgets import (
QDialog,
QDialogButtonBox,
QLineEdit,
QMessageBox,
QComboBox,
)
import os
import typing
Expand Down Expand Up @@ -35,14 +37,23 @@ def __init__(
connection_name (str, optional): The name of the connection. Defaults to "".
"""
super().__init__(parent)
self.txtName: QLineEdit
self.txtWarehouse: QLineEdit
self.txtAccount: QLineEdit
self.txtRole: QLineEdit
self.btnConnect: QDialogButtonBox
self.buttonBox: QDialogButtonBox
self.cbxConnectionType: QComboBox
self.txtDatabase: QLineEdit
self.mAuthSettings: QgsAuthSettingsWidget
self.setupUi(self)
self.btnConnect.clicked.connect(self.test_connection_clicked)
ok_button = self.buttonBox.button(QDialogButtonBox.Ok)
ok_button.clicked.connect(self.button_box_ok_clicked)
self.buttonBox.clicked.connect(self.button_box_ok_clicked)
self.settings = get_qsettings()
self.cbxConnectionType.addItem("Default Authentication")
self.cbxConnectionType.addItem("Single sign-on (SSO)")
self.connection_name = connection_name
self._sf_connection_manager = SFConnectionManager.get_instance()
self.deactivate_temp()

def deactivate_temp(self) -> None:
Expand All @@ -69,6 +80,33 @@ def deactivate_temp(self) -> None:
self.cb_projectsInDatabase.setVisible(False)
self.cb_metadataInDatabase.setVisible(False)

def get_unfilled_required_fields(self) -> bool:
"""
Checks for unfilled required fields in the connection dialog.
This method verifies if the required fields in the connection dialog are filled.
It returns a list of unfilled required fields with their corresponding names.
Returns:
list: A list of strings representing the names of unfilled required fields.
"""
fields = [
(self.txtName, "Connection Name"),
(self.txtWarehouse, "Warehouse"),
(self.txtAccount, "Account"),
(self.txtDatabase, "Database"),
]

unfilled_required_fields = [
f"- {field_name}\n" for widget, field_name in fields if widget.text() == ""
]

if self.mAuthSettings.username() == "":
unfilled_required_fields.append(
"- Username (Under the Basic Authentification Tab)\n"
)
return unfilled_required_fields

def button_box_ok_clicked(self) -> None:
"""
Save the connection settings when the OK button is clicked.
Expand All @@ -84,6 +122,14 @@ def button_box_ok_clicked(self) -> None:
update_connections_signal: Emitted after the connection settings have been saved.
"""
try:
fields_not_verified = self.get_unfilled_required_fields()
if len(fields_not_verified) > 0:
QMessageBox.critical(
self,
"Error message in New/Edit connection",
f"Please specify all mandatory fields:\n{''.join(fields_not_verified)}",
)
return
conn_settings = {
"name": self.txtName.text(),
"warehouse": self.txtWarehouse.text(),
Expand All @@ -100,7 +146,11 @@ def button_box_ok_clicked(self) -> None:
if self.connection_name != self.txtName.text():
if self.connection_name is not None and self.connection_name != "":
remove_connection(self.settings, self.connection_name)

if self.txtName.text() in self._sf_connection_manager.opened_connections:
del self._sf_connection_manager.opened_connections[self.txtName.text()]
self.update_connections_signal.emit()
super().accept()
except Exception as e:
QMessageBox.information(
None,
Expand Down
16 changes: 0 additions & 16 deletions ui/sf_connection_string_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,5 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QgsPgNewConnectionBase</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>395</x>
<y>501</y>
</hint>
<hint type="destinationlabel">
<x>450</x>
<y>287</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 45cc468

Please sign in to comment.