def login_handler(self): """ To limit access of individual options in a project, to specific users, require that users login with valid passwords, based on glb.admin_list. If login is valid, set glb.current_user to user_name. Then, at the begining of each restricted option, check if current_user is authorized or not to continue ... For example, see admin_option_handler() above """ user, ok_ = W.QInputDialog().getText(self, "Enter", "User name:".ljust(60), W.QLineEdit.Normal) if ok_ and user: passwd, ok_ = W.QInputDialog().getText(self, "Enter", "Password:"******"color: green;">Login and/or password are accepted - \nlogged in as \n{}</h3>'.format( user) notify_("VALID", msg) return glb.current_user = "******" msg = '<h3 style="color: red;">Login and/or password are invalid - \nlogged in as </h3>\nGuest' notify_("INVALID", msg)
def delete_row(self): """ """ if confirm_("Update:"): sql_str = """DELETE FROM table_1 WHERE id = "{}" """.format( glb.list_id) query = S.QSqlQuery() query.exec_(sql_str) query.first() notify_("Delete:", "Row was deleted") glb.menu_win.show() self.hide() else: notify_("Delete:", "Delete row was canceled")
def show_table_1_data(self, list_id_parameter): """following correct for table_1, update for a different table """ self.form_groupbox.setTitle("ID: " + str(list_id_parameter)) sql_str = """SELECT * FROM table_1 WHERE id = {}""".format( str(list_id_parameter)) query = S.QSqlQuery() query.exec_(sql_str) if not query.isActive(): notify_("Invalid Query", sql_str) return query.first() self.led01.setText(str(query.value(1))) self.led02.setText(str(query.value(2))) self.led03.setText(str(query.value(3))) self.led04.setText(str(query.value(4)))
def insert_new_row(self): """ """ if self.validate_data(): if self.confirm_update(): sql_str = """INSERT INTO table_1 VALUES ("{}","{}", "{}", "{}", "{}") """.format( glb.list_id, self.led01.text(), self.led02.text(), self.led03.text(), self.led04.text()) query = S.QSqlQuery() query.exec_(sql_str) query.first() else: notify_("Save Canceled", "Any input was discarded") self.cancel_add() self.set_default_form_mode() glb.menu_win.show() glb.table_1_list.hide() self.hide() else: notify_("Data Invalid:", self.invalid_msg)
def update_table_1_list(self): """new and updated records do not show, unless window updated ...""" query_1 = S.QSqlQuery() if "get list data": query_1.exec_(get_table_1_count()) if not query_1.isActive(): notify_("Error", "Invalid_query") return query_1.first() total_rows = query_1.value(0) if "update table size": self.table.setRowCount(total_rows) if "place data in table ...": query_2 = S.QSqlQuery() query_2.exec_(get_table_1_rows()) query_2.first() if not query_2.isActive(): notify_("Error", "Invalid_query") return self.table.setSortingEnabled( False ) # need to disable automatic sorting when filling table! for row in range(total_rows): for col in range(self.total_columns): item = W.QTableWidgetItem(str(query_2.value(col))) if col != 1: item.setTextAlignment(int(Qt.AlignCenter)) self.table.setItem(row, col, item) query_2.next() self.table.sortItems(1, order=Qt.AscendingOrder) self.table.setSortingEnabled(True)
def save_update(self): """ """ if self.validate_data(): """ save updated data back to db """ if confirm_("Update"): self.update_data(glb.list_id) notify_("Saved", "Updates were saved") self.set_default_form_mode() glb.form_win.hide() glb.table_1_list.hide() glb.menu_win.show() else: notify_("Save canceled:", "Updates discarded") self.cancel_update() else: notify_("Invalid_input:", self.invalid_msg)
def run_search_handler(self): """following needs to be updated for table other than table_1 """ glb.table_1_list.filter_str = self.create_table_1_where() if not glb.table_1_list.filter_str: notify_("Empty filter string") return if "get list data": str_sqlcount = """SELECT COUNT(*) FROM table_1 {} """.format( glb.table_1_list.filter_str) # to get total database rows query_1 = S.QSqlQuery() query_1.exec_(str_sqlcount) query_1.first() total_rows = query_1.value(0) if not query_1.isActive(): notify_("Invalid Query", str_sqlcount) return if not total_rows: notify_("Empty Query", str_sqlcount) return if "update table size": glb.table_1_list.table.setRowCount(total_rows) if "place data in table ...": glb.table_1_list.table.setSortingEnabled(False) #Note: need to turn sorting off while folling table, or data gets scrambled !!! str_sqldata = """SELECT id, text_1, integer_1, real_1, date_1 FROM table_1 {} """.format( glb.table_1_list.filter_str) query_2 = S.QSqlQuery() query_2.exec_(str_sqldata) query_2.first() for row in range(total_rows): for col in range(glb.table_1_list.total_columns): item = W.QTableWidgetItem(str(query_2.value(col))) if col != 1: item.setTextAlignment(int(Qt.AlignCenter)) glb.table_1_list.table.setItem(row, col, item) query_2.next() glb.table_1_list.table.sortItems(1, order=Qt.AscendingOrder) glb.table_1_list.table.setSortingEnabled(True) glb.table_1_list.show() self.hide()
def __init__(self): W.QWidget.__init__(self) if "attributes": self.setWindowTitle("Main Window") self.setMinimumSize(200, 200) self.setGeometry(glb.win_placement) if "menu bar options": if "QActions": self.login_act = W.QAction( "Enter Admin password to enable admin menus", self) self.login_act.setIcon( G.QIcon(glb.icons + '/apps/preferences-desktop-user-password.png')) self.login_act.triggered.connect(self.login_handler) self.about_qt_act = W.QAction( "Show the Qt library's About box", self) self.about_qt_act.setIcon( G.QIcon(glb.icons + '/actions/help-about.png')) self.about_qt_act.triggered.connect( W.QApplication.instance().aboutQt ) #self.about_qt_act.triggered.connect(qApp.aboutQt) # also works self.about_app_act = W.QAction( "Show the application's About box", self) self.about_app_act.setShortcut("Ctrl+A") self.about_app_act.setIcon( G.QIcon(glb.icons + '/actions/help-about.png')) self.about_app_act.triggered.connect( lambda: notify_("About MenuWindow", __doc__)) self.option_1_act = W.QAction("Tool Window", self) self.option_1_act.setShortcut("Ctrl+1") self.option_1_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.option_1_act.triggered.connect( self.open_toolwindow_handler) self.option_2_act = W.QAction("Option message", self) self.option_2_act.setShortcut("Ctrl+2") self.option_2_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.option_2_act.triggered.connect(pending) self.option_3_act = W.QAction("Option message", self) self.option_3_act.setShortcut("Ctrl+3") self.option_3_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.option_3_act.triggered.connect(pending) self.admin_1_act = W.QAction("Admin option 1", self) self.admin_1_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.admin_1_act.triggered.connect(self.admin_option_handler) self.admin_2_act = W.QAction("Admin option 2", self) self.admin_2_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.admin_2_act.triggered.connect(self.admin_option_handler) self.admin_3_act = W.QAction("Admin option 3", self) self.admin_3_act.setIcon( G.QIcon(glb.icons + '/categories/system-help.png')) self.admin_3_act.triggered.connect(self.admin_option_handler) if "menu bar layout": self.menu_mbr = W.QMenuBar() if "Menu": self.main_mnu = self.menu_mbr.addMenu("&Menu") self.main_mnu.addAction(self.option_1_act) self.main_mnu.addAction(self.option_2_act) self.main_mnu.addAction(self.option_3_act) if "Admin": self.admin_mnu = self.menu_mbr.addMenu("&Admin") self.admin_mnu.addAction(self.login_act) self.admin_mnu.addAction(self.admin_1_act) self.admin_mnu.addAction(self.admin_2_act) self.admin_mnu.addAction(self.admin_3_act) if "About": self.about_mnu = self.menu_mbr.addMenu("About") self.about_mnu.addAction(self.about_qt_act) self.about_mnu.addAction(self.about_app_act) if "window_layout": self.window_layout = W.QGridLayout(self) self.setLayout(self.window_layout) self.window_layout.setMenuBar(self.menu_mbr)
def cancel_update(self): """ """ notify_("Save Canceled", "Any updates were discarded") glb.table_1_list.show() self.set_default_form_mode() self.hide()
def cancel_add(self): """ """ notify_("Canceled", "New row was discarded") self.set_default_form_mode() self.hide() glb.table_1_list.show()