-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
345 lines (267 loc) · 12.3 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import qdarktheme
import sys
pass_key = "auavzkcycnhdmjmk"
#TODO :Create a button to open a matplotlib graph out of the pyqt5 app
# TODO: Reliability of a client
#TODO :add a new tenant, make the combo box of the status readonly
#TODO :Update/remove a tenant, Add an icon to the app and the email of the teacher to the combo box
#TODO : Add the school icon and my name on the login page
#TODO: Clean the code
from login import main_login, toggle_theme
import MySQLdb
from PyQt5.uic import loadUiType
ui, _ = loadUiType("library.ui")
light = True
tenant_id=0
# draw_graph()
class MainApp(QMainWindow, ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
# self.fill_book_db()
self.handle_ui_changes()
self.handle_buttons()
self.show_emails()
self.show_subjects()
def handle_ui_changes(self):
self.tabWidget.tabBar().setVisible(False)
def handle_buttons(self):
self.seeTheGraph.clicked.connect(self.draw_graph)
self.dayBtn.clicked.connect(self.open_tenants_tab)
self.themeBtn.clicked.connect(toggle_theme)
# view_tenant_btns = [self.view_tenant_btn,self.view_tenant_btn_2,self.view_tenant_btn_3,self.view_tenant_btn_4,self.view_tenant_btn_5,self.view_tenant_btn_6,self.view_tenant_btn_7,self.view_tenant_btn_8,self.view_tenant_btn_9,self.view_tenant_btn_10]
# for view_tenant in view_tenant_btns:
# view_tenant.clicked.connect(lambda : self.open_tenant_view(view_tenant_btns.index(view_tenant)))
view_tenant_btns = [self.view_tenant_btn, self.view_tenant_btn_2, self.view_tenant_btn_3,
self.view_tenant_btn_4, self.view_tenant_btn_5, self.view_tenant_btn_6,
self.view_tenant_btn_7, self.view_tenant_btn_8, self.view_tenant_btn_9,
self.view_tenant_btn_10]
[view_tenant.clicked.connect(lambda _, index=view_tenant_btns.index(view_tenant): self.open_tenant_view(index)) for view_tenant in view_tenant_btns]
self.bookBtn.clicked.connect(self.open_tenant_details)
self.userBtn.clicked.connect(self.open_users_tab)
self.settingBtn.clicked.connect(self.open_email_tab)
self.send_email.clicked.connect(self.open_email_tab)
# operations
self.addUserBtn.clicked.connect(self.add_new_user)
self.saveChangesUser.clicked.connect(self.update_user)
self.loginBtn.clicked.connect(self.edit_user)
self.sendEmailBtn_2.clicked.connect(self.send_email_func)
self.quitMailBtn_2.clicked.connect(self.open_tenants_tab)
self.backBtn.clicked.connect(self.open_tenants_tab)
#####################################################
############### opening tabs ######################
def open_tenants_tab(self):
self.tabWidget.setCurrentIndex(0)
def open_tenant_details(self):
self.tabWidget.setCurrentIndex(1)
def open_users_tab(self):
self.tabWidget.setCurrentIndex(2)
def open_email_tab(self):
self.tabWidget.setCurrentIndex(3)
##### Differents functionalities ######
def send_email_func(self):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Email configuration
sender_email = self.sender_email.text()
sender_password = self.my_password.text()
recipient_email = self.emailCombo.currentText()
subject = self.subjectCombo.currentText()
body = self.contentEmail.toPlainText()
# SMTP server settings (for Gmail)
smtp_server = "smtp.gmail.com"
smtp_port = 587
# Create the MIME message
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = recipient_email
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))
# Establish a secure connection and send the email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
QMessageBox.information(self, "Info", "Email sent successfully!", QMessageBox.Ok)
def draw_graph(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
import matplotlib.pyplot as plt
global tenant_id
room_id = tenant_id
self.cursor.execute('''
SELECT payement_history FROM tenants WHERE room_id = (%s)
''', (room_id,))
data = self.cursor.fetchone()
print("data")
print(data)
import ast
payment_status = ast.literal_eval(data[0])
# Months (x-axis)
months = ["Month 1", "Month 2", "Month 3", "Month 4", "Month 5", "Month 6"]
# Create a bar graph
plt.bar(months, payment_status, color=['green' if status == 'Yes' else 'red' for status in payment_status])
# Set labels and title
plt.xlabel("Months")
plt.ylabel("Payment Status")
plt.title("Client Payment Status Over 6 Months")
# Display the graph
plt.show()
def is_reliable(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
global tenant_id
room_id = tenant_id
self.cursor.execute('''
SELECT payement_history FROM tenants WHERE room_id = (%s)
''', (room_id,))
payment_status = self.cursor.fetchone()
print(payment_status)
reliability_criteria = 5
import ast
# Calculate the client's reliability based on payment history
num_payments = ast.literal_eval(payment_status[0]).count("Yes")
# Make a determination
if num_payments >= reliability_criteria:
self.reliability.setText("Reliable")
else:
self.reliability.setText("Unreliable")
def open_tenant_view(self,id):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
global tenant_id
tenant_id=id+1
room_id = id+1
self.is_reliable()
try:
data= self.cursor.execute('''
SELECT * FROM tenants WHERE room_id = (%s)
''', (room_id,))
except Exception as e:
print(e)
data1 = self.cursor.fetchone()
print(data1)
self.name_label.setText("Name : "+data1[1])
self.email_label.setText("Email : "+data1[2])
# print(id)
self.open_tenant_details()
#####################################################
############### user operations ######################
def add_new_user(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
username = self.username.text()
email = self.email.text()
payement_history = self.payement_history.text()
move_in = self.move_in.date().toString("yyyy-MM-dd")
room_id = self.spinBox.value()
tenant = self.cursor.execute('''
SELECT * FROM tenants WHERE name = (%s) AND email=(%s) and room_id =(%s)
''', (username,email,room_id))
# print(tenant)
if tenant:
# open a dialog box to say that the username is already taken
QMessageBox.warning(self, "Info", "The user has been already added! \n Please try another username.", QMessageBox.Ok)
elif username==" " or email==" " or not room_id or not payement_history:
QMessageBox.warning(self, "Info", "Don't play with me and fill all those fields!", QMessageBox.Ok)
else:
if username and email :
self.cursor.execute('''
INSERT INTO tenants (name, email, move_in, room_id, payement_history)
VALUES (%s, %s, %s, %s, %s)
''', (username, email, move_in, room_id, payement_history))
self.db.commit()
self.db.close()
QMessageBox.information(self, "Info", "The user has been added successfully!", QMessageBox.Ok)
else:
# Open the dialog box with the message
print("Please don't create problem")
# TODO Finish with the login functionality
def show_emails(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
self.cursor.execute('''
SELECT email FROM tenants
''')
data = self.cursor.fetchall()
for email in data:
self.emailCombo.addItem(email[0])
# print(email)
self.db.commit()
self.db.close()
def show_subjects(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
self.cursor.execute('''
SELECT subject FROM bills
''')
data = self.cursor.fetchall()
self.db.commit()
self.db.close()
def edit_user(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
username = self.username2.text()
email = self.email_2.text()
self.cursor.execute('''
SELECT * FROM tenants WHERE name = (%s) AND email= (%s)
''', (username, email,))
tenant = self.cursor.fetchone()
#print(user)
if tenant:
change_visibility = [self.username3, self.email_3,
self.saveChangesUser,self.payement_history_2]
for obj in change_visibility:
obj.setEnabled(True)
self.username3.setText(tenant[1])
self.email_3.setText(tenant[2])
self.payement_history_2.setText(tenant[5])
else:
print("Please don't create problem")
def update_user(self):
self.db = MySQLdb.connect(host='localhost', user='root', password='123456789', db='apartement')
self.cursor = self.db.cursor()
new_username = self.username3.text()
new_email = self.email_3.text()
# new_move_in = move_in.date().toString("yyyy-MM-dd")
payement_history_2 = self.payement_history_2.text()
# date.fromString(date_string, "yyyy-MM-dd")
username = self.username2.text()
email = self.email.text()
update_query = '''UPDATE tenants
SET name = %s , email=%s, payement_history= %s
WHERE name = %s'''
if new_username and new_email and payement_history_2:
print("Here-------------------->")
print(username,new_username,new_email,payement_history_2)
self.cursor.execute(update_query, (new_username, new_email, payement_history_2, username,))
self.db.commit()
self.db.close()
change_visibility = [self.username3, self.email_3, self.payement_history_2,
self.saveChangesUser]
for obj in change_visibility:
obj.setEnabled(False)
self.statusBar().showMessage("The user has been updated successfully!")
else:
print("Please don't create problem")
#####################################################
############### opening tabs ######################
def main():
app = QApplication(sys.argv)
qdarktheme.setup_theme("dark")
window = MainApp()
window.show()
app.exec_()
if __name__ == '__main__':
login_=main_login()
if login_:
main()
else:
pass
# run this inside the terminal to avoid the error caused by the .qrc file :
# pyrcc5 icons.qrc -o icons_rc.py