Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from ui.login import Ui_LoginDialog
- from PyQt4.QtGui import QApplication, QDialog
- class Model:
- users = [{"id": 1, "username": "Admin", "password": "1", "role": "admin"},
- {"id": 2, "username": "tester", "password": "test", "role": "user"}]
- def __init__(self, controller):
- self.controller = controller
- def login(self, username, password):
- for user in self.users:
- if user['username'] == username and user['password'] == password:
- return {"id": user['id'], "username": user['username'], "role": user['role']}
- return {"error": "Invalid username or password."}
- class View(QDialog, Ui_LoginDialog):
- def __init__(self, controller):
- QDialog.__init__(self)
- self.setupUi(self)
- self.controller = controller
- self.errorLabel.setStyleSheet("color: red")
- self.errorLabel.hide()
- self.loginButton.clicked.connect(self.onLogin)
- def onLogin(self):
- self.errorLabel.hide()
- username = unicode(self.usernameEdit.text())
- password = unicode(self.passwordEdit.text())
- self.controller.login(username, password)
- def showError(self, text):
- self.errorLabel.show()
- self.errorLabel.setText(text)
- class Controller:
- def __init__(self):
- self.model = Model(self)
- self.view = View(self)
- self.view.show()
- def login(self, username, password):
- username = username.strip()
- password = password.strip()
- if not username:
- self.view.showError(u"Введите имя пользователя.")
- elif not password:
- self.view.showError(u"Введите пароль.")
- else:
- result = self.model.login(username, password)
- if 'error' in result:
- self.view.showError(result['error'])
- else:
- print u"Вы авторизовались как {username}.".format(**result)
- self.view.close()
- if __name__ == '__main__':
- a = QApplication([])
- c = Controller()
- a.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement