-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
381 lines (319 loc) · 13.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from PySide6 import QtWidgets, QtUiTools, QtCore
import sys
import pymongo
import socket
from resources import nspc
def show_main_window():
windowmain.showMaximized()
def exitfcn():
save()
global project_names # Access the global project_names variable
project_names = []
windowmain.close() # Close the main window
window.show() # Show the login window again
windowmain.projectlabel.setText("No Projects to show")
windowmain.projectlabel.repaint()
windowmain.n1.hide()
windowmain.n2.hide()
windowmain.n3.hide()
windowmain.n4.hide()
windowmain.n1.hide()
windowmain.n5.hide()
windowmain.n6.hide()
windowmain.n7.hide()
windowmain.n8.hide()
windowmain.n9.hide()
windowmain.n10.hide()
windowmain.n11.hide()
windowmain.n12.hide()
windowmain.n13.hide()
windowmain.n14.hide()
windowmain.n15.hide()
windowmain.pbar.hide()
int_list=[0] * 67
set_checkbox_states(checkboxes, int_list)
def update_project_names():
global currentproject # Add this line to access the global variable
user_data = collection.find_one({"username": username})
if user_data:
project_names = get_project_names(user_data["data"])
print("Project names:", project_names)
# Set the project name according to the first project in the list
first_project_name = project_names[0] if project_names else None
if first_project_name:
windowmain.n1.setText(first_project_name)
windowmain.n1.show()
int_list = user_data["data"][first_project_name]
set_checkbox_states(checkboxes, int_list)
currentproject = first_project_name # Set the current project to the first project name
n = 1
for names in project_names:
label_name = f"n{n}"
label = getattr(windowmain, label_name)
label.setText(names)
label.show()
n += 1
def register_clicked():
global username, password, collection
username = window.username.text()
password = window.password.text()
data = {"p1": [0]*67}
register_success, register_msg = register_user(collection, username, password, data)
print(register_msg)
if register_success:
window.close()
setusername()
regiterproject() # Initialize new project registration
show_main_window()
def login_clicked():
global username, password, collection, project_names
username = window.username.text()
password = window.password.text()
login_success, login_msg = login_user(collection, username, password)
print(login_msg)
if login_success:
window.close()
setusername()
user_data = collection.find_one({"username": username})
if user_data:
project_names = get_project_names(user_data["data"])
print("Project names:", project_names)
update_project_names()
windowmain.projectlabel.setText(currentproject)
windowmain.projectlabel.repaint() # Initialize project names
show_main_window()
def get_project_names(data):
# Extract the keys from the "data" dictionary to get the project names
project_names = list(data.keys())
return project_names
def checkboxcheck():
checkboxlist = []
for a in range(1, 68):
checkbox_name = f"c{a}"
checkbox = getattr(windowmain, checkbox_name, None)
if checkbox is not None and isinstance(checkbox, checkbox.__class__):
if checkbox.isChecked():
checkboxlist.append(1)
else:
checkboxlist.append(0)
print(checkboxlist)
return checkboxlist
# Set the checkbox states based on the list of integers
def set_checkbox_states(checkboxes, int_list):
if len(checkboxes) != len(int_list):
raise ValueError("The number of checkboxes and the length of the integer list must be the same.")
for checkbox, value in zip(checkboxes, int_list):
if isinstance(checkbox, checkbox.__class__):
checkbox.setChecked(bool(value))
else:
raise ValueError("Input list should only contain PySide6 QCheckBox objects.")
def connect_to_mongodb():
global collection # Make 'collection' a global variable so you can use it outside this function
try:
# Attempt to establish a connection to the MongoDB database
client = pymongo.MongoClient("mongodb+srv://neerajlovecyber:[email protected]/?retryWrites=true&w=majority")
db = client["records"]
collection = db["records"]
window.logmsg.setText("Connected to MongoDB and internet")
print("Connected to MongoDB")
return collection
except pymongo.errors.ConnectionFailure:
window.logmsg.setText("Failed to connect to MongoDB. Please check your internet connection.")
print("Failed to connect to MongoDB. Please check your internet connection.")
except socket.gaierror:
window.logmsg.setText("Failed to resolve host. Please check your internet connection and host name.")
print("Failed to resolve host. Please check your internet connection and host name.")
return None
def register_user(collection, username, password, data):
# Check if the username is already taken
global currentproject
if collection.find_one({"username": username}):
window.logmsg.setText("Username already taken")
currentproject = windowmain.newpname.text()
return False, "Username already taken"
# Insert user data into the collection
collection.insert_one({
"username": username,
"password": password,
"data": data
})
window.logmsg.setText("Registration successful")
return True, "Registration successful"
def login_user(collection, username, password):
# Find the user by username
user = collection.find_one({"username": username})
if user:
# Check if the provided password matches
if user["password"] == password:
window.logmsg.setText("Login Successful")
return True, "Login successful"
else:
window.logmsg.setText("Invalid password")
return False, "Invalid password"
else:
return False, "User not found"
def setusername():
windowmain.usernamefield.setText("Hello, "+username)
def regiterproject():
windowmain.pbar.show()
global currentproject
def hidebtn():
global username # Access the global username variable
projectname = windowmain.newpname.text()
# Update the project name in the data dictionary
user_data = collection.find_one({"username": username})
if user_data:
data = user_data["data"]
if "p1" in data:
new_data = {projectname: data["p1"]}
data.pop("p1")
# Delete the "p1" key from the MongoDB document
collection.update_one({"username": username}, {"$unset": {"data.p1": 1}})
# Update the MongoDB document with the new project name and data
collection.update_one({"username": username}, {"$set": {"data": new_data}})
global currentproject
currentproject=windowmain.newpname.text()
set_checkbox_states(checkboxes, new_data[projectname]) # Set checkbox states for the updated project
currentproject=windowmain.newpname.text()
windowmain.n1.setText(projectname)
windowmain.n1.show()
windowmain.pbar.hide()
windowmain.newpbtn.clicked.connect(hidebtn)
def addnewproject():
global currentproject # Access the global currentproject variable
print("Adding a new project")
windowmain.pbar.show()
# Check the number of existing projects
user_data = collection.find_one({"username": username})
if user_data:
total_projects = len(user_data.get("data", {}))
if total_projects >= 14:
print("Maximum projects limit reached (15)")
windowmain.projectlabel.setText("Max projects limit reached")
windowmain.projectlabel.repaint()
windowmain.pbar.hide()
return
def hidebtn():
global currentproject # Access the global currentproject variable
user_data = collection.find_one({"username": username})
if user_data:
total_projects = len(user_data.get("data", {}))
print("Total projects:", total_projects)
total_projects = total_projects + 1
projectname = windowmain.newpname.text()
label_name = f"n{total_projects}"
currentproject = projectname
print(currentproject) # Update the current project name
windowmain.projectlabel.setText(currentproject)
windowmain.projectlabel.repaint()
# Set checkbox values to 0 for the new project
new_project_data = [0] * 67 # Initialize with 67 zeros
set_checkbox_states(checkboxes, new_project_data)
# Trigger layout update
windowmain.layout().update()
# Update the database with the new project data
update_query = {"username": username}
update_data = {"$set": {f"data.{projectname}": new_project_data}}
collection.update_one(update_query, update_data)
windowmain.pbar.hide()
update_project_names()
projectname = windowmain.newpname.text()
currentproject=projectname
print(currentproject)
set_checkbox_states(checkboxes,new_project_data)
windowmain.newpbtn.clicked.connect(hidebtn)
def project_menu(btn):
global currentproject
global project_names
print("pmenu")
user_data = collection.find_one({"username": username})
if user_data:
project_names = get_project_names(user_data["data"])
print("Project names:", project_names)
name=project_names[btn-1]
currentproject=name
user_data = collection.find_one({"username": username})
int_list = user_data["data"][name]
set_checkbox_states(checkboxes, int_list)
windowmain.projectlabel.setText(currentproject)
windowmain.projectlabel.repaint()
def save():
global currentproject # Access the global currentproject variable
checkbox_values = checkboxcheck()
if collection is not None and currentproject:
try:
query = {"username": username, "password": password}
update = {"$set": {f"data.{currentproject}": checkbox_values}} # Use the current project name in the update query
collection.update_one(query, update)
print(f"Data for project '{currentproject}' updated successfully")
except Exception as e:
print("Error updating data:", str(e))
else:
print("Not connected to MongoDB or no current project selected")
if __name__ == "__main__":
print("Program start.")
username = ""
password = ""
project_names = []
currentproject=""
app = QtWidgets.QApplication([]) # Create the application instance
loader = QtUiTools.QUiLoader()
ui_file = QtCore.QFile(".\\resources\\new.ui")
ui_file.open(QtCore.QFile.ReadOnly)
windowmain = loader.load(ui_file)
ui_file.close()
if windowmain is None:
print("Failed to load UI file.")
sys.exit(1)
window = loader.load(".\\resources\\loginpage.ui", None)
window.show()
checkboxes = [getattr(windowmain, f"c{a}", None) for a in range(1, 68)]
windowmain.n2.hide()
windowmain.n3.hide()
windowmain.n4.hide()
windowmain.n1.hide()
windowmain.n5.hide()
windowmain.n6.hide()
windowmain.n7.hide()
windowmain.n8.hide()
windowmain.n9.hide()
windowmain.n10.hide()
windowmain.n11.hide()
windowmain.n12.hide()
windowmain.n13.hide()
windowmain.n14.hide()
windowmain.n15.hide()
windowmain.pbar.hide()
#windowmain.main_body.setEnabled(False)
collection = connect_to_mongodb()
if collection is not None:
print("Connected to MongoDB")
window.registerbtn.clicked.connect(register_clicked)
window.loginbtn.clicked.connect(login_clicked)
window.registerbtn.clicked.connect(register_clicked)
window.loginbtn.clicked.connect(login_clicked)
windowmain.savebtn.clicked.connect(save)
windowmain.newbtn.clicked.connect(addnewproject)
################################3
windowmain.n1.clicked.connect(lambda: project_menu(1))
windowmain.n2.clicked.connect(lambda: project_menu(2))
windowmain.n3.clicked.connect(lambda: project_menu(3))
windowmain.n4.clicked.connect(lambda: project_menu(4))
windowmain.n5.clicked.connect(lambda: project_menu(5))
windowmain.n6.clicked.connect(lambda: project_menu(6))
windowmain.n7.clicked.connect(lambda: project_menu(7))
windowmain.n8.clicked.connect(lambda: project_menu(8))
windowmain.n9.clicked.connect(lambda: project_menu(9))
windowmain.n10.clicked.connect(lambda: project_menu(10))
windowmain.n11.clicked.connect(lambda: project_menu(11))
windowmain.n12.clicked.connect(lambda: project_menu(12))
windowmain.n13.clicked.connect(lambda: project_menu(13))
windowmain.n14.clicked.connect(lambda: project_menu(14))
windowmain.n15.clicked.connect(lambda: project_menu(15))
windowmain.next1.clicked.connect(lambda: windowmain.stackedWidget.setCurrentWidget(windowmain.page2))
windowmain.next2.clicked.connect(lambda: windowmain.stackedWidget.setCurrentWidget(windowmain.page1))
windowmain.logout.clicked.connect(exitfcn)
#############333333333
sys.exit(app.exec_())
else:
print("Exiting due to connection error.")