forked from ckh08045/AutoPercenty
133 lines
4.9 KiB
Python
133 lines
4.9 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QCheckBox
|
|
from pymongo import MongoClient
|
|
from modules.db.mongo_config import MongoConfig
|
|
from configparser import ConfigParser
|
|
from pymongo.errors import ConnectionFailure
|
|
from PyQt5.QtCore import Qt
|
|
import logging
|
|
|
|
# 로거 인스턴스 가져오기
|
|
logger = logging.getLogger('default_logger')
|
|
class MongoConnectionWidget(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.mongoConfig = MongoConfig()
|
|
self.initUI()
|
|
|
|
def initUI(self):
|
|
self.setGeometry(780, 420, 360, 240)
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
self.infoLabel = QLabel("사용자 로그인 정보를 입력하세요")
|
|
|
|
self.infoLabel = QLabel("서버 정보를 입력하세요:")
|
|
self.addressInput = QLineEdit("qcy2.duckdns.org")
|
|
self.addressInput.setPlaceholderText("서버 주소 (예: www.naver.com)")
|
|
self.addressInput.returnPressed.connect(self.focus_to_port_input)
|
|
|
|
self.portInput = QLineEdit("27017")
|
|
self.portInput.setPlaceholderText("서버 포트 (예: 27017)")
|
|
self.portInput.returnPressed.connect(self.focus_to_user_input)
|
|
|
|
self.userInput = QLineEdit("root")
|
|
self.userInput.setPlaceholderText("사용자 이름")
|
|
self.userInput.returnPressed.connect(self.focus_to_password_input)
|
|
|
|
self.passwordInput = QLineEdit("1234")
|
|
self.passwordInput.setPlaceholderText("비밀번호")
|
|
self.passwordInput.setEchoMode(QLineEdit.Password)
|
|
self.passwordInput.returnPressed.connect(self.try_connect)
|
|
|
|
# 비밀번호 표시 토글 버튼
|
|
self.showPasswordCheckBox = QCheckBox("비밀번호 표시")
|
|
self.showPasswordCheckBox.stateChanged.connect(self.togglePasswordVisibility)
|
|
|
|
self.connectButton = QPushButton("접속하기")
|
|
self.connectButton.clicked.connect(self.try_connect)
|
|
|
|
layout.addWidget(self.infoLabel)
|
|
layout.addWidget(self.addressInput)
|
|
layout.addWidget(self.portInput)
|
|
layout.addWidget(self.userInput)
|
|
layout.addWidget(self.passwordInput)
|
|
layout.addWidget(self.showPasswordCheckBox)
|
|
layout.addWidget(self.connectButton)
|
|
|
|
|
|
self.setLayout(layout)
|
|
self.setWindowTitle('데이터베이스 연결 설정')
|
|
|
|
def focus_to_user_input(self):
|
|
self.userInput.setFocus()
|
|
|
|
def focus_to_port_input(self):
|
|
self.portInput.setFocus()
|
|
|
|
def focus_to_password_input(self):
|
|
self.passwordInput.setFocus()
|
|
|
|
def togglePasswordVisibility(self, state):
|
|
if state == Qt.Checked:
|
|
self.passwordInput.setEchoMode(QLineEdit.Normal)
|
|
else:
|
|
self.passwordInput.setEchoMode(QLineEdit.Password)
|
|
|
|
def load_config_and_try_connect(self):
|
|
config = ConfigParser()
|
|
config.read('config.ini')
|
|
if config.has_section('MongoDB'):
|
|
address = config.get('MongoDB', 'address')
|
|
port = config.get('MongoDB', 'port')
|
|
user = config.get('MongoDB', 'user')
|
|
password = config.get('MongoDB', 'password')
|
|
return self.try_connect_with_config(address, port, user, password)
|
|
return False
|
|
|
|
def try_connect_with_config(self, address, port, user, password):
|
|
try:
|
|
client = MongoClient(f'mongodb://{user}:{password}@{address}:{port}/')
|
|
client.admin.command('ping') # 접속 확인
|
|
return True, "접속 성공"
|
|
except ConnectionFailure as e:
|
|
# ConnectionFailure 예외 발생 시 에러 메시지 포착
|
|
error_message = str(e.details) if hasattr(e, 'details') else str(e)
|
|
return False, f"접속 실패: {error_message}"
|
|
except Exception as e:
|
|
# 기타 예외 처리
|
|
return False, f"접속 실패: {str(e)}"
|
|
|
|
def try_connect(self):
|
|
address = self.addressInput.text()
|
|
port = self.portInput.text()
|
|
user = self.userInput.text()
|
|
password = self.passwordInput.text()
|
|
success, message = self.try_connect_with_config(address, port, user, password)
|
|
if success:
|
|
QMessageBox.information(self, "성공", message)
|
|
self.save_config(address, port, user, password)
|
|
self.proceed_to_main_program()
|
|
else:
|
|
QMessageBox.warning(self, "실패", message)
|
|
|
|
def save_config(self, address, port, user, password):
|
|
config = ConfigParser()
|
|
config['MongoDB'] = {
|
|
'address': address,
|
|
'port': port,
|
|
'user': user,
|
|
'password': password
|
|
}
|
|
with open('config.ini', 'w') as configfile:
|
|
config.write(configfile)
|
|
|
|
#def main():
|
|
# app = QApplication(sys.argv)
|
|
# ex = MongoConnectionWidget()
|
|
# ex.show()
|
|
# sys.exit(app.exec_())
|
|
|
|
#if __name__ == '__main__':
|
|
# main()
|