-
Notifications
You must be signed in to change notification settings - Fork 0
/
usuario.py
227 lines (208 loc) · 10 KB
/
usuario.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import sqlite3
import sys
import bcrypt
from PyQt5.QtCore import QTime
from Utils.readOnly import ReadOnlyDelegate
from view.userUi import Ui_UserMain
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget, QMessageBox, QTableWidgetItem
class UserWindow(QMainWindow, Ui_UserMain):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setupUi(self)
# centralizar tela
self.center()
# popular tabela
self.on_btn_pes_usu_pressed()
# conectar botões
self.btnSal_usu.clicked.connect(self.on_btn_sal_usu_pressed)
self.btnAtu_usu.clicked.connect(self.on_btn_atu_usu_pressed)
self.btnDel_usu.clicked.connect(self.on_btn_del_usu_pressed)
self.btnLim_usu.clicked.connect(self.on_btn_clear_usu_pressed)
self.btnPesUsu_usu.clicked.connect(self.on_btn_pes_usu_pressed)
self.txtPesUsu_usu.textChanged.connect(self.on_btn_pes_usu_pressed)
self.table_user.clicked.connect(self.setar_campos_usu)
# deixar as tabelas somente leitura
delegate = ReadOnlyDelegate(self.table_user)
for i in range(8):
self.table_user.setItemDelegateForColumn(i, delegate)
# largura padrao da tabela de usuarios
self.table_user.setColumnWidth(0, 50)
self.table_user.setColumnWidth(1, 160)
self.table_user.setColumnWidth(2, 110)
self.table_user.setColumnWidth(5, 60)
self.table_user.setColumnWidth(6, 60)
self.table_user.setColumnWidth(7, 60)
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
# Adicionar novo usuario
def on_btn_sal_usu_pressed(self):
db = sqlite3.connect('dbmehsys.db')
cursor = db.cursor()
sql = 'insert into tbusuarios(usuario, fone, login, senha, perfil, hora_in, hora_out) values(?,?,?,?,?,?,?)'
nome = self.txtNom_usu.text()
fone = self.txtTel_usu.text()
login = self.txtLog_usu.text()
senha = self.txtSen_usu.text()
hashed = bcrypt.hashpw(senha.encode('utf-8'), bcrypt.gensalt(7))
perfil = self.cbbPer_usu.currentText()
hora_in = self.timInicio.text()
hora_out = self.timFim.text()
try:
if len(nome.strip()) < 4 or len(fone.strip()) < 14 or len(fone.strip()) < 14:
QMessageBox.warning(self, 'ERRO!!!', 'Preencha os campos obrigatórios para adicionar novo usuário!')
elif len(senha.strip()) < 4 or len(login.strip()) < 4:
QMessageBox.warning(self, 'ERRO!!!', 'Login e senha precisam ter pelo menos 4 caracteres!')
else:
cursor.execute(sql, [nome, fone, login, hashed, perfil, hora_in, hora_out])
db.commit()
QMessageBox.information(
self, 'SUCESSO!!!', 'Usuário ADICIONADO com Sucesso!')
self.on_btn_pes_usu_pressed()
self.on_btn_clear_usu_pressed()
db.close()
except Exception as e:
db.close()
QMessageBox.warning(self, 'ERRO!!!', str(e))
# atualizar dados de usuario
def on_btn_atu_usu_pressed(self):
db = sqlite3.connect('dbmehsys.db')
cursor = db.cursor()
sql = 'update tbusuarios set usuario=?, fone=?, login=?, senha=?, perfil=?, hora_in=?, hora_out=? where id=?'
sql2 = 'update tbhorarios set profissional=? where profissional=?'
sql3 = 'update horarios_agendados set funcionario=? where funcionario=?'
nome = self.txtNom_usu.text()
old_nome = self.old_nome_usu
fone = self.txtTel_usu.text()
login = self.txtLog_usu.text()
senha = self.txtSen_usu.text()
hashed = bcrypt.hashpw(senha.encode('utf-8'), bcrypt.gensalt(7))
perfil = self.cbbPer_usu.currentText()
hora_in = self.timInicio.text()
hora_out = self.timFim.text()
id = self.txtID_usu.text()
try:
if len(nome.strip()) < 4 or len(fone.strip()) < 14 or len(fone.strip()) < 14:
QMessageBox.warning(self, 'ERRO!!!', 'Preencha os campos obrigatórios para atualizar usuário!')
elif len(senha.strip()) < 4 or len(login.strip()) < 4:
QMessageBox.warning(self, 'ERRO!!!', 'Login e senha precisam ter pelo menos 4 caracteres!')
else:
cursor.execute(sql, [nome, fone, login, hashed, perfil, hora_in, hora_out, id])
if nome != old_nome:
cursor.execute(sql2, [nome, old_nome])
cursor.execute(sql3, [nome, old_nome])
db.commit()
QMessageBox.information(
self, 'SUCESSO!!!', 'Usuário ATUALIZADO com Sucesso!')
self.on_btn_pes_usu_pressed()
self.on_btn_clear_usu_pressed()
db.close()
except Exception as e:
db.close()
QMessageBox.warning(self, 'ERRO!!!', str(e))
# deletar usuarios do banco de dados
def on_btn_del_usu_pressed(self):
db = sqlite3.connect('dbmehsys.db')
cursor = db.cursor()
sql = 'delete from tbusuarios where id=?'
id = self.txtID_usu.text()
confirm = QMessageBox.question(self, 'REMOVER USUÁRIO?',
f'Tem certeza que quer REMOVER o Usuário ({self.txtNom_usu.text()}) do sistema?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if confirm == QMessageBox.Yes:
try:
cursor.execute(sql, [id])
db.commit()
QMessageBox.information(
self, 'SUCESSO!!!', f'Usuário ({self.txtNom_usu.text()}) EXCLUIDO com Sucesso!')
self.on_btn_pes_usu_pressed()
self.on_btn_clear_usu_pressed()
db.close()
except Exception as e:
db.close()
QMessageBox.warning(self, 'ERRO!!!', str(e))
else:
db.close()
def on_btn_pes_usu_pressed(self):
db = sqlite3.connect('dbmehsys.db')
cursor = db.cursor()
sql = 'select id as ID,usuario as Nome,fone as Fone,login as Login,senha as Senha,perfil as Perfil,hora_in as ' \
'Entrada,hora_out as Saída from tbusuarios where usuario like ? '
search = '%' + self.txtPesUsu_usu.text() + '%'
try:
cursor.execute(sql, [search])
rs = cursor.fetchall()
row = 0
self.table_user.setRowCount(len(rs))
for r in rs:
self.table_user.setItem(row, 0, QTableWidgetItem(str(r[0])))
self.table_user.setItem(row, 1, QTableWidgetItem(r[1]))
self.table_user.setItem(row, 2, QTableWidgetItem(r[2]))
self.table_user.setItem(row, 3, QTableWidgetItem(r[3]))
self.table_user.setItem(row, 4, QTableWidgetItem(str(r[4])[:-7] + '...'))
self.table_user.setItem(row, 5, QTableWidgetItem(str(r[5])))
self.table_user.setItem(row, 6, QTableWidgetItem(str(r[6])))
self.table_user.setItem(row, 7, QTableWidgetItem(str(r[7])))
row += 1
db.close()
except Exception as e:
db.close()
QMessageBox.warning(self, 'ERRO!!!', str(e))
# popular campos do formulário selecionando uma linha da tabela
def setar_campos_usu(self):
self.cbbPer_usu.setEnabled(True)
self.btnSal_usu.setEnabled(False)
self.btnSal_usu.setStyleSheet(None)
self.btnAtu_usu.setEnabled(True)
self.btnDel_usu.setEnabled(True)
rows = sorted(set(index.row() for index in self.table_user.selectedIndexes()))
for r in rows:
db = sqlite3.connect('dbmehsys.db')
cursor = db.cursor()
id_clicked = self.table_user.item(r, 0).text()
sql = 'SELECT u.id, u.usuario, c.current_user FROM tbusuarios as u inner join utils as c on (? = ' \
'c.current_user) limit 2'
cursor.execute(sql, [id_clicked])
rs = cursor.fetchall()
if len(rs) >= 1:
self.cbbPer_usu.setEnabled(False)
self.btnDel_usu.setEnabled(False)
self.txtID_usu.setText(self.table_user.item(r, 0).text())
self.txtNom_usu.setText(self.table_user.item(r, 1).text())
self.old_nome_usu = self.table_user.item(r, 1).text()
self.txtTel_usu.setText(self.table_user.item(r, 2).text())
self.txtLog_usu.setText(self.table_user.item(r, 3).text())
# self.txtSen_usu.setText(self.table_user.item(r, 4).text())
self.cbbPer_usu.setCurrentText(self.table_user.item(r, 5).text())
if self.table_user.item(r, 6).text() != '':
self.timInicio.setTime(
QTime(int(self.table_user.item(r, 6).text()[:2]), int(self.table_user.item(r, 6).text()[3:])))
else:
self.timInicio.setTime(QTime(0, 0))
if self.table_user.item(r, 7).text() != '':
self.timFim.setTime(
QTime(int(self.table_user.item(r, 7).text()[:2]), int(self.table_user.item(r, 7).text()[3:])))
else:
self.timFim.setTime(QTime(0, 0))
def on_btn_clear_usu_pressed(self):
self.txtID_usu.setText(None)
self.txtNom_usu.setText(None)
self.txtTel_usu.setText(None)
self.cbbPer_usu.setEnabled(True)
self.cbbPer_usu.setCurrentIndex(0)
self.txtLog_usu.setText(None)
self.txtSen_usu.setText(None)
self.timInicio.setTime(QTime(0, 0))
self.timFim.setTime(QTime(0, 0))
self.btnSal_usu.setEnabled(True)
self.btnSal_usu.setStyleSheet("background-color: rgb(170, 0, 0);\n"
"color: rgb(255, 255, 255);")
self.btnAtu_usu.setEnabled(False)
self.btnDel_usu.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
user = UserWindow()
user.show()
app.exec_()