Made Connection GUI working. Crashes when testing, will fix

This commit is contained in:
crennis 2023-05-04 16:34:34 +02:00
parent d62abec3b8
commit 9afa8bec49
7 changed files with 102 additions and 27 deletions

View File

@ -5,8 +5,8 @@
"ip": "localhost", "ip": "localhost",
"password": "password", "password": "password",
"user": "admin", "user": "admin",
"port": "5432" "port": "5432",
"database": "PostgreSQL", "database": "PostgreSQL"
}, },
"apikey": "sk-abc***" "apikey": "sk-abc***"
} }

View File

@ -67,6 +67,19 @@ class Postgres:
self.conn.commit() self.conn.commit()
cur.close() cur.close()
def test_connection(self) -> bool:
try:
self.conn = psycopg2.connect(
host=self.db_ip,
port=self.db_port,
user=self.db_username,
password=self.db_password,
database=self.db_name
)
return True
except:
return False
def close(self): def close(self):
self.conn.close() self.conn.close()

View File

@ -55,6 +55,9 @@ class Ui_MainWindow(object):
self.statementOutput = QtWidgets.QTextEdit(self.centralwidget) self.statementOutput = QtWidgets.QTextEdit(self.centralwidget)
self.statementOutput.setEnabled(True) self.statementOutput.setEnabled(True)
self.statementOutput.setGeometry(QtCore.QRect(20, 80, 375, 111)) self.statementOutput.setGeometry(QtCore.QRect(20, 80, 375, 111))
self.statementOutput.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.statementOutput.setFrameShadow(QtWidgets.QFrame.Sunken)
self.statementOutput.setTextInteractionFlags(QtCore.Qt.TextSelectableByKeyboard|QtCore.Qt.TextSelectableByMouse)
self.statementOutput.setObjectName("statementOutput") self.statementOutput.setObjectName("statementOutput")
MainWindow.setCentralWidget(self.centralwidget) MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar = QtWidgets.QMenuBar(MainWindow)

View File

@ -160,6 +160,12 @@
<height>111</height> <height>111</height>
</rect> </rect>
</property> </property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="html"> <property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt; <string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt; &lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
@ -167,6 +173,9 @@ p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;&quot;&gt; &lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> &lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget> </widget>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">

View File

@ -66,7 +66,6 @@ class Ui_Connection(object):
self.dbtypeCombo = QtWidgets.QComboBox(Connection) self.dbtypeCombo = QtWidgets.QComboBox(Connection)
self.dbtypeCombo.setGeometry(QtCore.QRect(100, 10, 131, 22)) self.dbtypeCombo.setGeometry(QtCore.QRect(100, 10, 131, 22))
self.dbtypeCombo.setObjectName("dbtypeCombo") self.dbtypeCombo.setObjectName("dbtypeCombo")
self.dbtypeCombo.addItem("")
self.retranslateUi(Connection) self.retranslateUi(Connection)
QtCore.QMetaObject.connectSlotsByName(Connection) QtCore.QMetaObject.connectSlotsByName(Connection)
@ -83,4 +82,3 @@ class Ui_Connection(object):
self.saveButton.setText(_translate("Connection", "Save")) self.saveButton.setText(_translate("Connection", "Save"))
self.returnLabel.setText(_translate("Connection", "Connection ... / Saved...")) self.returnLabel.setText(_translate("Connection", "Connection ... / Saved..."))
self.label_6.setText(_translate("Connection", "IP")) self.label_6.setText(_translate("Connection", "IP"))
self.dbtypeCombo.setItemText(0, _translate("Connection", "PostgrSQL"))

View File

@ -207,11 +207,6 @@
<height>22</height> <height>22</height>
</rect> </rect>
</property> </property>
<item>
<property name="text">
<string>PostgrSQL</string>
</property>
</item>
</widget> </widget>
</widget> </widget>
<resources/> <resources/>

View File

@ -5,6 +5,7 @@ import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
import modules.aisql as aisql import modules.aisql as aisql
from gui.apikey import Ui_ApiKey as ApiKeyForm from gui.apikey import Ui_ApiKey as ApiKeyForm
from gui.connection import Ui_Connection as ConnectionForm from gui.connection import Ui_Connection as ConnectionForm
from gui.gui.Window import Ui_MainWindow from gui.gui.Window import Ui_MainWindow
@ -12,6 +13,12 @@ from gui.gui.Window import Ui_MainWindow
configfolder = "config" configfolder = "config"
configfile = "config.json" configfile = "config.json"
dbtypes = {
0: "PostgreSQL",
1: "MySQL",
2: "SQLite"
}
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self): def __init__(self):
@ -41,44 +48,94 @@ class MainWindow(QMainWindow):
print(self.ui.textInput.text()) print(self.ui.textInput.text())
### Connection Window ###
class ConnectionWindow(QDialog): class ConnectionWindow(QDialog):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.ui = ConnectionForm() self.ui = ConnectionForm()
self.ui.setupUi(self) self.ui.setupUi(self)
print('1') self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
self.ui.returnLabel.setText("")
# Load DB Config from file # Load DB Config from file
with open(os.path.join(configfolder, configfile), "r") as f: with open(os.path.join(configfolder, configfile), "r") as f:
self.config = json.load(f) self.config = json.load(f)
self.db_type = self.config["db_type"] self.dbtype = self.config["database"]["dbtype"]
self.db_ip = self.config["db_ip"] self.dbip = self.config["database"]["ip"]
self.db_port = self.config["db_port"] self.dbport = self.config["database"]["port"]
self.db_username = self.config["db_username"] self.dbuser = self.config["database"]["user"]
self.db_password = self.config["db_password"] self.dbpass = self.config["database"]["password"]
self.db_name = self.config["db_name"] self.dbname = self.config["database"]["database"]
print('2') self.ui.dbtypeCombo.addItems(dbtypes.values())
self.ui.dbtypeCombo.setCurrentIndex(self.dbtype)
self.ui.ipInput.setText(self.dbip)
self.ui.portInput.setText(self.dbport)
self.ui.usernameInput.setText(self.dbuser)
self.ui.passwordInput.setText(self.dbpass)
self.ui.databaseInput.setText(self.dbname)
self.ui.dbtypeCombo.setText(self.db_type) # Unlock Buttons if ip, port and database is not empty
self.ui.ipInput.setText(self.db_ip)
self.ui.portInput.setText(self.db_port)
self.ui.usernameInput.setText(self.db_username)
self.ui.passwordInput.setText(self.db_password)
self.ui.databaseInput.setText(self.db_name)
print('3') self.ui.ipInput.textChanged.connect(self.on_text_changed)
self.ui.portInput.textChanged.connect(self.on_text_changed)
self.ui.databaseInput.textChanged.connect(self.on_text_changed)
# If IP Port and Database are empty, disable Buttons # Pressed Save Button
if self.db_ip == "" or self.db_port == "" or self.db_name == "": self.ui.saveButton.clicked.connect(self.on_save_button_clicked)
# Pressed Test Button
self.ui.testButton.clicked.connect(self.on_test_button_clicked)
def on_text_changed(self):
if self.ui.ipInput.text() == "" or self.ui.portInput.text() == "" or self.ui.databaseInput.text() == "":
self.ui.saveButton.setEnabled(False) self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False) self.ui.testButton.setEnabled(False)
else: else:
self.ui.saveButton.setEnabled(True) self.ui.saveButton.setEnabled(True)
self.ui.testButton.setEnabled(True) self.ui.testButton.setEnabled(True)
print('4') def on_save_button_clicked(self):
self.dbtype = self.ui.dbtypeCombo.currentIndex()
self.dbip = self.ui.ipInput.text()
self.dbport = self.ui.portInput.text()
self.dbuser = self.ui.usernameInput.text()
self.dbpass = self.ui.passwordInput.text()
self.dbname = self.ui.databaseInput.text()
self.config["database"]["dbtype"] = self.dbtype
self.config["database"]["ip"] = self.dbip
self.config["database"]["port"] = self.dbport
self.config["database"]["user"] = self.dbuser
self.config["database"]["password"] = self.dbpass
self.config["database"]["database"] = self.dbname
with open(os.path.join(configfolder, configfile), "w") as f:
json.dump(self.config, f, indent=4)
self.close()
# FIXME: Crash when testing connection
def on_test_button_clicked(self):
self.dbtype = self.ui.dbtypeCombo.currentIndex()
self.dbip = self.ui.ipInput.text()
self.dbport = self.ui.portInput.text()
self.dbuser = self.ui.usernameInput.text()
self.dbpass = self.ui.passwordInput.text()
self.dbname = self.ui.databaseInput.text()
if self.dbtype == 0: # PostgreSQL
import database.postgresql as postgresql
db = postgresql.Postgre(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
if db.test_connection():
self.ui.returnLabel.setText("Connection successful")
self.ui.returnLabel.setStyleSheet("color: green")
else:
self.ui.returnLabel.setText("Connection failed")
self.ui.returnLabel.setStyleSheet("color: red")
### Api Key Window ###
class ApiKeyWindow(QDialog): class ApiKeyWindow(QDialog):
def __init__(self, parent=None): def __init__(self, parent=None):