def renew_attributes(self): # Select attributes for good when user clicked to cells if not self.tableWidget.currentRow() == -1: current_good = self.tableWidget.item(self.tableWidget.currentRow(), 0).text() conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT g.Id, g.goods,g.name, g.value FROM\ attributes as g WHERE g.goods = {}" .\ format(current_good) data_in = sql_result(conn, sql_text) self.tableWidget_2.setRowCount(len(data_in)) self.tableWidget_2.setColumnCount(4) row = 0 for items_line in data_in: for col in range(0, 4): item_value = str(items_line[col]) self.tableWidget_2.setItem( row, col, QtWidgets.QTableWidgetItem(item_value)) row += 1 self.tableWidget_2.setColumnHidden(0, True) self.tableWidget_2.setColumnHidden(1, True) self.tableWidget_2.setHorizontalHeaderLabels( self.__labels_attributes) else: QtWidgets.QMessageBox.warning(self, 'Information', 'Select ' ' good!')
def renew_item(self, filtered=False): conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT g.Id, g.name,c.name, g.density, g.width \ FROM goods as g left join color c on g.color = c.id " if self.lineEdit.text() and not self.tableWidget.currentRow() == -1 \ and filtered: field_name = { 1: "g.name", 2: "c.name", 3: "g.density", 4: "g.width" } sql_text += "WHERE {} = '{}'".format( field_name[self.tableWidget.currentColumn()], self.lineEdit.text()) data_in = sql_result(conn, sql_text) self.tableWidget.setRowCount(len(data_in)) self.tableWidget.setColumnCount(5) row = 0 for items_line in data_in: for col in range(0, 5): item_value = str(items_line[col]) self.tableWidget.setItem( row, col, QtWidgets.QTableWidgetItem(item_value)) row += 1 self.tableWidget.setColumnHidden(0, True) self.tableWidget.setHorizontalHeaderLabels(self.__labels)
def push_create(self): # For creating/ updating item in database if self.id_good: conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT c.Id FROM color c WHERE c.NAME = '{}';".format( self.combo.currentText()) color_id = [i[0] for i in sql_result(conn, sql_text)] conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "UPDATE \"goods\" SET \"name\" = '{}', \"color\" = {},\ \"density\" = {}, \"width\" = {} WHERE \"id\" = {}".\ format( self.textName.text(), color_id[0], self.text_density.text(), self.text_width.text(), self.id_good) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Data don`t ' ' insert!') self.accept() else: conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT c.Id FROM color c WHERE c.NAME = '{}';".format( self.combo.currentText()) color_id = [i[0] for i in sql_result(conn, sql_text)] conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "INSERT INTO goods (\"id\", \"name\", \"color\", \ \"density\",\"width\") VALUES (DEFAULT, \ '{}', {}, {},{} );".format( self.textName.text(), color_id[0], self.text_density.text(), self.text_width.text()) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Data don`t ' ' insert!') else: self.accept()
def push_create(self): # Action creating color conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "INSERT INTO color (\"id\", \"name\") VALUES \ (DEFAULT, '{}')".format(self.textName.text()) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Data don`t ' ' insert!') else: self.accept()
def push_create(self): # Action for creating conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "INSERT INTO attributes (\"id\", \"name\", \"value\", \ \"goods\") VALUES (DEFAULT, '{}', {}, {});".format( self.text_name.text(), self.text_value.text(), self.id_goods) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Data don`t ' ' insert!') else: self.accept()
def push_create(self): # Create user and check permission for this if self.textPass.text() == self.textPass2.text(): conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) if not is_add_user(conn, self.textName.text(), self.textPass.text(), self.combo.currentText()): QtWidgets.QMessageBox.warning(self, 'Error', 'You don`t have' ' permission!') self.accept() else: QtWidgets.QMessageBox.warning(self, 'Error', 'Password not equal')
def select_item(self): # set value to window for changing conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT g.Id, g.name,c.name, g.density, g.width \ FROM goods as g left join color c on g.color = c.id WHERE \ g.id = {};".format(self.id_good) data_in = sql_result(conn, sql_text) self.textName.setText(data_in[0][1]) index = self.combo.findText(data_in[0][2]) if index >= 0: self.combo.setCurrentIndex(index) self.text_density.setText(str(data_in[0][3])) self.text_width.setText(str(data_in[0][4]))
def ban(self): # Ban users if not self.table_users.currentRow() == -1: current_user = self.table_users.item( self.table_users.currentRow(), 0).text() conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "ALTER ROLE {} VALID UNTIL '01/01/1000';".\ format(current_user) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Don`t ban!') else: self.accept() else: QtWidgets.QMessageBox.warning(self, 'Error', 'Choose a user!')
def draw_table(self): # Pull all users to table conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "select rolname from pg_catalog.pg_roles where" \ " rolcanlogin = TRUE" data_in = sql_result(conn, sql_text) self.table_users.setRowCount(len(data_in)) self.table_users.setColumnCount(1) row = 0 for items_line in data_in: item_value = str(items_line[0]) self.table_users.setItem(row, 0, QtWidgets.QTableWidgetItem(item_value)) row += 1 self.table_users.setHorizontalHeaderLabels(['Name'])
def del_item(self): # Delete current good and its attributes if not self.tableWidget.currentRow() == -1: conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "DELETE FROM \"goods\" WHERE \"id\" = {};".format( self.tableWidget.item(self.tableWidget.currentRow(), 0).text()) if not is_sql_no_result(conn, sql_text): QtWidgets.QMessageBox.warning(self, 'Error', 'Data don`t ' ' delete!') else: self.renew_item() QtWidgets.QMessageBox.warning(self, 'Information', 'OK!') else: QtWidgets.QMessageBox.warning(self, 'Information', 'Select ' ' good!')
def __init__(self, db_login, db_password, db_location, id_good=None, parent=None): super(AddItem, self).__init__(parent) self.db_login = db_login self.db_password = db_password self.db_location = db_location self.id_good = id_good self.setWindowTitle('Add item') self.label_name = QtWidgets.QLabel(self) self.label_name.setText('Name') self.label_density = QtWidgets.QLabel(self) self.label_density.setText('density') self.label_width = QtWidgets.QLabel(self) self.label_width.setText('width') self.label_color = QtWidgets.QLabel(self) self.label_color.setText('Color') self.textName = QtWidgets.QLineEdit(self) self.text_density = QtWidgets.QLineEdit(self) self.text_width = QtWidgets.QLineEdit(self) self.combo = QtWidgets.QComboBox(self) self.combo.setEditable(False) conn = connect_to_db('things', self.db_login, self.db_password, self.db_location) sql_text = "SELECT c.name FROM color c" data_in = sql_result(conn, sql_text) items_for_combo = [i[0] for i in data_in] self.combo.addItems(items_for_combo) if self.id_good: self.button_create = QtWidgets.QPushButton('Update', self) else: self.button_create = QtWidgets.QPushButton('Add', self) self.button_create.clicked.connect(self.push_create) layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.label_name) layout.addWidget(self.textName) layout.addWidget(self.label_color) layout.addWidget(self.combo) layout.addWidget(self.label_density) layout.addWidget(self.text_density) layout.addWidget(self.label_width) layout.addWidget(self.text_width) layout.addWidget(self.button_create) if self.id_good: self.select_item()