-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
589 lines (475 loc) · 27.4 KB
/
backend.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import mysql.connector as sql
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import messagebox
from functools import partial
from prettytable import PrettyTable
import re
import hashlib
import getpass
import datetime
global conn_obj
global cursr
# class VotingSystemGUI:
# def __init__(self, root):
# self.root = root
# self.root.title("Voting System")
# self.create_login_screen()
# def create_login_screen(self):
# self.login_frame = tk.Frame(self.root)
# self.login_frame.pack(padx=20, pady=20)
# tk.Label(self.login_frame, text="Welcome to Voting System!", font=("Helvetica", 16)).grid(row=0, column=0, columnspan=2, pady=10)
# tk.Label(self.login_frame, text="Login as:").grid(row=1, column=0, sticky="e", pady=5)
# self.login_type_var = tk.StringVar()
# self.login_type_var.set("ADMIN")
# tk.OptionMenu(self.login_frame, self.login_type_var, "ADMIN", "VOTER").grid(row=1, column=1, sticky="w", pady=5)
# tk.Label(self.login_frame, text="ID:").grid(row=2, column=0, sticky="e", pady=5)
# self.id_entry = tk.Entry(self.login_frame)
# self.id_entry.grid(row=2, column=1, pady=5)
# tk.Label(self.login_frame, text="Password:").grid(row=3, column=0, sticky="e", pady=5)
# self.password_entry = tk.Entry(self.login_frame, show="*")
# self.password_entry.grid(row=3, column=1, pady=5)
# login_button = tk.Button(self.login_frame, text="Login", command=self.login)
# login_button.grid(row=4, column=1, pady=10)
# def login(self):
# login_type = self.login_type_var.get().upper()
# user_id = self.id_entry.get()
# password = self.password_entry.get()
# if login_type == "ADMIN":
# name, user_data = login_admin(user_id, password)
# # Call your login_admin function with user_id and password
# # If login is successful, show admin dashboard
# # else show an error message
# if user_data:
# messagebox.showinfo("Login Successful", f"Welcome {name}!")
# self.show_admin_dashboard()
# else:
# messagebox.showerror("Login Failed", "Invalid credentials. Please try again.")
# elif login_type == "VOTER":
# name, user_data = login_voter(user_id, password)
# # Call your login_voter function with user_id and password
# # If login is successful, show voter dashboard
# # else show an error message
# messagebox.showinfo("Login Successful", "Welcome Voter!")
# self.show_voter_dashboard()
# else:
# messagebox.showerror("Invalid Login Type", "Please select a valid login type.")
# def show_admin_dashboard(self):
# admin_dashboard_window = tk.Toplevel(self.root)
# admin_dashboard_window.title("Admin Dashboard")
# create_election_button = tk.Button(admin_dashboard_window, text="Create Election", command=self.create_election)
# create_election_button.pack(pady=10)
# view_elections_button = tk.Button(admin_dashboard_window, text="View Elections", command=self.view_elections)
# view_elections_button.pack(pady=10)
# view_candidates_button = tk.Button(admin_dashboard_window, text="View Candidates of an Election", command=self.view_candidates)
# view_candidates_button.pack(pady=10)
# view_details_button = tk.Button(admin_dashboard_window, text="View Election Details", command=self.view_details)
# view_details_button.pack(pady=10)
# make_changes_button = tk.Button(admin_dashboard_window, text="Make Changes in an Election", command=self.make_changes)
# make_changes_button.pack(pady=10)
# generate_results_button = tk.Button(admin_dashboard_window, text="Generate Election Results", command=self.generate_results)
# generate_results_button.pack(pady=10)
# logout_button = tk.Button(admin_dashboard_window, text="LOG OUT", command=self.logout)
# logout_button.pack(pady=10)
# def view_candidates(self):
# # Implement the functionality to view candidates of an election
# # You may prompt the user for input, perform necessary operations, and display the results
# pass
# def view_details(self):
# # Implement the functionality to view election details
# # You may prompt the user for input, perform necessary operations, and display the results
# pass
# def make_changes(self):
# # Implement the functionality to make changes in an election
# # You may prompt the user for input, perform necessary operations, and display the results
# pass
# def generate_results(self):
# # Implement the functionality to generate election results
# # You may prompt the user for input, perform necessary operations, and display the results
# pass
# def logout(self):
# # Implement the logout functionality
# # You may close the admin dashboard window and show the login screen again
# pass
# def show_voter_dashboard(self):
# # Add logic to show voter dashboard
# # For example, create a new window for voter dashboard
# pass
def dbms_connect(password):
global conn_obj
global cursr
try:
conn_obj = sql.connect(host='localhost', user='root',passwd=password)
cursr = conn_obj.cursor()
print("Connection Successful.")
except:
print("Connection failed.")
def close_connection():
global conn_obj, cursr
conn_obj.commit()
conn_obj.close()
def create_database():
try:
try:
cursr.execute("USE ELECTIONS")
except:
cursr.execute("CREATE DATABASE ELECTIONS")
# cursr.execute("CREATE DATABASE IF NOT EXISTS ELECTIONS")
cursr.execute("USE ELECTIONS")
print("Database creation successful.")
create_tables()
except Exception:
print("Database creation failed due to the following Exception: \n"+Exception.__name__)
def create_tables():
table_creation_queries = ["CREATE TABLE ADMIN (DEPARTMENT VARCHAR(5), NAME VARCHAR(30), ADMIN_ID INT , PHONE VARCHAR(11) , PASSWD CHAR(64) NOT NULL, PRIMARY KEY(ADMIN_ID,PHONE))",
"CREATE TABLE ELECTION (YEAR INT(4),ELECTION_ID INT PRIMARY KEY,ADMINID INT,FOREIGN KEY (ADMINID) REFERENCES ADMIN(ADMIN_ID) ON DELETE CASCADE)",
"CREATE TABLE POSTS (NO_OF_CANDIDATES INT, POST_ID INT, ELECTIONID INT, POSTNAME VARCHAR(15), PRIMARY KEY(POST_ID, ELECTIONID), FOREIGN KEY (ELECTIONID) REFERENCES ELECTION(ELECTION_ID) ON DELETE CASCADE)",
"CREATE TABLE VOTERS (USN VARCHAR(10), DEPARTMENT VARCHAR(5), NAME VARCHAR(30), PHONE VARCHAR(11) , EMAIL VARCHAR(30), SEMESTER INT(1), PASSWD CHAR(64), PRIMARY KEY(USN,PHONE))",
"CREATE TABLE CANDIDATES (USN VARCHAR(10), SEMESTER INT(1), POSTID INT, DEPARTMENT VARCHAR(5), NAME VARCHAR(30), PHONE VARCHAR(11) , EMAIL VARCHAR(30), ELECTIONID INT, GENDER CHAR, PRIMARY KEY(USN,PHONE), FOREIGN KEY(ELECTIONID) REFERENCES ELECTION(ELECTION_ID) ON DELETE SET NULL, FOREIGN KEY (POSTID) REFERENCES POSTS(POST_ID) ON DELETE SET NULL)",
"CREATE TABLE VOTES (VOTERID VARCHAR(10), ELECTIONID INT, POSTID INT, CANDIDATEID VARCHAR(10), PRIMARY KEY(ELECTIONID, POSTID, VOTERID), FOREIGN KEY(VOTERID) REFERENCES VOTERS(USN) ON DELETE CASCADE, FOREIGN KEY(ELECTIONID) REFERENCES ELECTION(ELECTION_ID) ON DELETE CASCADE, FOREIGN KEY (POSTID) REFERENCES POSTS(POST_ID) ON DELETE CASCADE, FOREIGN KEY (CANDIDATEID) REFERENCES CANDIDATES(USN))",
"CREATE TABLE VOTE_COUNT (CANDIDATEID VARCHAR(10), ELECTIONID INT, POSTID INT, TOTAL_VOTES INT, PRIMARY KEY (CANDIDATEID, ELECTIONID, POSTID), FOREIGN KEY(CANDIDATEID) REFERENCES CANDIDATES(USN) ON DELETE CASCADE, FOREIGN KEY(ELECTIONID) REFERENCES ELECTION(ELECTION_ID) ON DELETE CASCADE, FOREIGN KEY(POSTID) REFERENCES POSTS(POST_ID) ON DELETE CASCADE)",
"CREATE TRIGGER UPDATE_NO_OF_CANDIDATES AFTER INSERT ON CANDIDATES FOR EACH ROW UPDATE POSTS SET NO_OF_CANDIDATES = NO_OF_CANDIDATES + 1 WHERE POST_ID = NEW.POSTID AND ELECTIONID = NEW.ELECTIONID",
"CREATE TRIGGER candidate_insert_trigger AFTER INSERT ON CANDIDATES FOR EACH ROW INSERT INTO VOTE_COUNT (CANDIDATEID, ELECTIONID, POSTID, TOTAL_VOTES) VALUES (NEW.USN, NEW.ELECTIONID, NEW.POSTID, 0)",
"CREATE TRIGGER update_vote_count AFTER INSERT ON VOTES FOR EACH ROW UPDATE VOTE_COUNT SET TOTAL_VOTES = TOTAL_VOTES + 1 WHERE CANDIDATEID= NEW.CANDIDATEID AND ELECTIONID = NEW.ELECTIONID AND POSTID = NEW.POSTID"]
print("\nCreating all tables...")
for i in table_creation_queries:
try:
cursr.execute(i)
except:
print("Error craeting table ",i[12:i.find('(')])
break
else:
print("\nTable creation successful.")
def insert_values_to_table(table, values):
try:
cursr.execute(f'INSERT INTO {table} VALUES ({values})')
print("\n Values successfully inserted.\n")
conn_obj.commit()
cursr.execute(f"SELECT * FROM {table}")
res = cursr.fetchall()
print(res)
conn_obj.commit()
except Exception as e:
print(f"Error occurred while inserting values: {e}")
def validate_input(string, regex_pattern):
while True:
user_input = input(string)
if re.match(regex_pattern, user_input):
return user_input
else:
print("Invalid input. Please try again.")
def validate_election_id(election_id, admin_id):
cursr.execute(f"SELECT * FROM ELECTION WHERE election_id={election_id} AND ADMINID={admin_id}")
res = cursr.fetchall()
return True if res else False
# def update_posts(post_id, election_id):
# cursr.execute(f"UPDATE POSTS SET NO_OF_CANDIDATES = NO_OF_CANDIDATES + 1 WHERE POST_ID = {post_id} AND ELECTIONID = {election_id}")
# print('Posts_updated')
def insert_admin_values():
department = validate_input("Enter department: ", r'^[A-Za-z]{1,5}$')
name = validate_input("Enter name: ", r'^[A-Za-z ]{1,30}$')
admin_id = validate_input("Enter admin ID: ", r'^\d+$')
phone = validate_input("Enter phone (10 digits): ", r'^\d{10}$')
passwd = getpass.getpass("Enter your password: ")
pass_bytes = passwd.encode('utf-8')
passwd = hashlib.sha256(pass_bytes).hexdigest()
insert_values_to_table('ADMIN', f'\"{department.upper()}\", \"{name}\", {admin_id}, \"{phone}\", \"{passwd}\" ')
def insert_election_values(admin_id):
year = validate_input("Enter election year: ", r'^\d{4}$')
if int(year) <= datetime.date.today().year:
print("Invalid Year of election")
return
election_id = validate_input("Enter election ID: ", r'^\d+$')
# admin_id = validate_input("Enter admin ID: ", r'^\d+$')
insert_values_to_table("ELECTION", f' {year}, {election_id}, {admin_id}')
def insert_posts_values():
# no_of_candidates = validate_input("Enter number of candidates: ", r'^\d+$')
post_id = validate_input("Enter post ID: ", r'^\d+$')
election_id = validate_input("Enter election ID: ", r'^\d+$')
post_name = validate_input("Enter post name: ", r'^[A-Za-z]{1,15}$')
insert_values_to_table("POSTS", f'0, {post_id}, {election_id}, \"{post_name}\"')
def insert_voters_values():
usn = validate_input("Enter USN (10 characters): ", r'^[A-Za-z0-9]{10}$')
department = validate_input("Enter department: ", r'^[A-Za-z]{1,5}$')
name = validate_input("Enter name : ", r'^[A-Za-z ]{1,30}$')
phone = validate_input("Enter phone (10 digits): ", r'^\d{10}$')
email = validate_input("Enter email: ", r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
semester = validate_input("Enter semester: ", r'^\d{1}$')
passwd = getpass.getpass("Enter your password: ")
pass_bytes = passwd.encode('utf-8')
passwd = hashlib.sha256(pass_bytes).hexdigest()
try:
insert_values_to_table("VOTERS",f'\"{usn.upper()}\", \"{department.upper()}\", \"{name}\", \"{phone}\", \"{email}\", {semester}, \"{passwd}\"')
print("Sign up successfully completed. Please go back to login.")
except:
print("Voter already exists. Please Log In.")
def insert_candidates_values(admin_id):
usn = validate_input("Enter USN (10 characters): ", r'^[A-Za-z0-9]{10}$')
semester = validate_input("Enter semester: ", r'^\d{1}$')
post_id = validate_input("Enter post ID: ", r'^\d+$')
department = validate_input("Enter department: ", r'^[A-Za-z0-9]{1,5}$')
name = validate_input("Enter name: ", r'^[A-Za-z0-9 ]{1,30}$')
phone = validate_input("Enter phone (10 digits): ", r'^\d{10}$')
email = validate_input("Enter email: ", r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
election_id = validate_input("Enter election ID: ", r'^\d+$')
gender = validate_input("Enter gender (M/F): ", r'^[MFmf]$')
if validate_election_id(election_id, admin_id):
insert_values_to_table("CANDIDATES", f'\"{usn.upper()}\", {semester}, {post_id},\"{department.upper()}\", \"{name}\", \"{phone}\", \"{email}\", {election_id}, \"{gender}\"')
# update_posts(post_id,election_id)
else:
print("You cannot add Candidate to other admin's election.")
def show_elections(admin_id):
cursr.execute(f"SELECT * FROM ELECTION WHERE ADMINID={admin_id} ORDER BY YEAR")
res = cursr.fetchall()
for i in res:
print("Election ID: ", i[1])
print("Year: ", i[0])
def show_election_details(admin_id, election_id):
cursr.execute(f"SELECT * FROM ELECTION WHERE ADMINID={admin_id} ORDER BY YEAR")
res = cursr.fetchall()
if res:
for i in res:
print("Election ID: ", i[1])
print("Year: ", i[0])
cursr.execute(f"SELECT POST_ID, NO_OF_CANDIDATES, POSTNAME FROM POSTS WHERE ELECTIONID={election_id} ORDER BY POST_ID")
res1 = cursr.fetchall()
print("Posts: ")
myTable = PrettyTable(["Post ID", "No. of Candidates", "Post Name"])
for i in res1:
myTable.add_row(i)
print(myTable)
# for j in res1:
# print("No. of Candidates \t Post ID \t Post Name")
# print("\t "+str(j[0])+" \t \t "+str(j[2])+" \t "+j[3])
else:
print("Empty Set.")
def show_election_candidates(election_id):
cursr.execute(f"SELECT * FROM CANDIDATES WHERE ELECTIONID={election_id}")
# cursr.execute(f"SELECT * FROM CANDIDATES WHERE ELECTIONID IN (SELECT ELECTION_ID FROM ELECTION WHERE ELECTION_ID={election_id} AND ADMINID={admin_id}) ")
res = cursr.fetchall()
if res:
myTable = PrettyTable(["USN","SEMESTER", "POSTID", "DEPARTMENT", "NAME", "PHONE", "EMAIL", "ELECTIONID", "GENDER"])
for i in res:
myTable.add_row(i)
print(myTable)
else:
print("No Candidate available for the given election.")
def remove_candidate(admin_id):
usn = validate_input("Enter USN to remove(10 characters): ", r'^[A-Za-z0-9]{10}$')
cursr.execute(f"SELECT * FROM CANDIDATES WHERE USN = {usn} AND ELECTIONID IN (SELECT ELECTION_ID FROM ELECTION WHERE ADMINID={admin_id})")
res = cursr.fetchall()
if res:
try:
cursr.execute(f"DELETE FROM CANDIDATES WHERE USN = '{usn}'")
print("Candidate removed successfully.")
except:
print("USN not found.")
else:
print("You cannot delete candidate from other Admin's elections.")
def login_admin(id, password):
pass_bytes = password.encode('utf-8')
password = hashlib.sha256(pass_bytes).hexdigest()
# cursr.execute(f"SELECT * FROM ADMIN WHERE PASSWD=\'{password}\' AND ADMIN_ID IN (SELECT ADMIN_ID FROM ADMIN WHERE ADMIN_ID ={id}")
cursr.execute(f"SELECT * FROM ADMIN WHERE ADMIN_ID=\'{id}\' AND PASSWD=\'{password}\'")
result = cursr.fetchall()
if not result:
print("Login failed. Check your credentials and retry.")
return None,None
else:
print("Login successful.")
return result[0][1], result[0]
def login_voter(id, password):
pass_bytes = password.encode('utf-8')
password = hashlib.sha256(pass_bytes).hexdigest()
# cursr.execute(f"SELECT * FROM VOTERS WHERE PASSWD=\'{password}\' AND USN IN (SELECT USN FROM VOTERS WHERE USN=\'{id}\'")
cursr.execute(f"SELECT * FROM VOTERS WHERE USN=\'{id}\' AND PASSWD=\'{password}\'")
result = cursr.fetchall()
if not result:
print("Login failed. Check your credentials and retry.")
return None,None
else:
print("Login successful.")
return result[0][2], result[0]
def generate_bar_graph(candidates, total_votes, election_id, post_id):
fig = plt.figure(figsize=(10, 6))
plt.bar(candidates, total_votes, color='blue')
plt.legend()
plt.xlabel('Candidate ID')
plt.ylabel('Total Votes')
plt.title(f'Results for Post {post_id} of Election {election_id}')
plt.savefig(f'results/bar_e{election_id}_p{post_id}.png')
plt.show()
def generate_pie_chart(candidates, total_votes, election_id, post_id):
# fig = plt.figure(figsize=(10, 6))
# plt.pie(total_votes, labels=candidates, autopct='%1.1f%%', startangle=120)
# plt.axis('equal')
# plt.title('Percentage of Votes for Each Candidate')
# plt.savefig(f'results/pie_e{election_id}_p{post_id}.png')
# plt.show()
fig, ax = plt.subplots(figsize=(10, 6))
wedges, texts, autotexts = ax.pie(total_votes, labels=candidates, autopct='%1.1f%%', startangle=120)
ax.legend(wedges, candidates, title='Candidates', loc='center left', bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax.set_title(f'Results for Post {post_id} of Election {election_id}')
plt.savefig(f'results/pie_e{election_id}_p{post_id}.png')
# Show the plot
plt.show()
def generate_result(election_id, plot_choice):
cursr.execute(f"SELECT DISTINCT POSTID FROM VOTE_COUNT WHERE ELECTIONID={election_id}")
res = cursr.fetchall()
if res:
cursr.execute(f"SELECT POST_ID FROM POSTS WHERE ELECTIONID={election_id}")
posts = cursr.fetchall()
for i in posts:
cursr.execute(f"SELECT CANDIDATEID, TOTAL_VOTES FROM VOTE_COUNT WHERE POSTID={i[0]} AND ELECTIONID={election_id}")
votes = cursr.fetchall()
candidates = [row[0] for row in votes]
total_votes = [row[1] for row in votes]
if plot_choice==1:
generate_bar_graph(candidates,total_votes, election_id, i[0])
else:
generate_pie_chart(candidates, total_votes, election_id, i[0])
if __name__ == '__main__':
dbms_connect('')
create_database()
while True:
print("\nWelcome to Voting System!!!\n")
choice = int(input("1. LOGIN \n2. SIGN UP \n3. EXIT \nEnter your response: "))
if choice == 1:
choice2 = int(input("\nLogin as: \n1. ADMIN \n2. VOTER \nEnter your response:"))
if choice2 == 1:
print("\nPlease enter the following required parameters: ")
id = int(input("Enter Admin ID: "))
passwd = getpass.getpass("Enter Password: ")
name, user_data = login_admin(id, passwd)
if name:
print("\nWelcome ",name)
while(True):
print("\nSelect what you want to do: \n")
choice3 = int(input("1. Create a new Election \n2. View all Elections \n3. View Candidates of a election \n4. View Election details \n5. Make changes in a Election \n6. Generate Election Results \n7. LOG OUT \nEnter your response: "))
if choice3 == 1:
insert_election_values(user_data[2])
elif choice3 == 2:
show_elections(user_data[2])
elif choice3 == 3:
election_id = validate_input("Enter election ID to view Candidates: ", r'^\d+$')
show_election_candidates(election_id,)
elif choice3 == 4:
election_id = validate_input("Enter election ID to view details: ", r'^\d+$')
if election_id:
show_election_details(user_data[2],election_id)
elif choice3 == 5:
while True:
print("\nSelect what change you want to do: \n")
choice4 = int(input("1. Add Candidate \n2. Remove Candidate \n3. Change year of election \n4. Add a Post \n5. GO BACK \nEnter your response: "))
if choice4 == 1:
insert_candidates_values(user_data[2])
elif choice4 == 2:
remove_candidate(user_data[2])
elif choice4 == 3:
election_id = validate_input("Enter election ID: ", r'^\d+$')
year = validate_input("Enter election year: ", r'^\d{4}$')
if year <= datetime.today().year:
print("\nEnter a valid election year.")
else:
cursr.execute(F"UPDATE ELECTION SET YEAR={year} WHERE ELECTION_ID={election_id}")
elif choice4 == 4:
insert_posts_values()
elif choice4 == 5:
break
else:
print("Pleease enter a valid input.")
elif choice3 == 6:
cursr.execute(f"SELECT ELECTION_ID FROM ELECTION WHERE ADMINID={user_data[2]} AND ELECTION_ID IN (SELECT DISTINCT ELECTIONID FROM VOTE_COUNT)")
elections = cursr.fetchall()
if elections:
print("\nSelect the election to generate Results:")
for i in range(len(elections)):
print(str(i+1)+".",elections[i][0])
election_id = elections[int(input("Enter your response: "))-1][0]
plot_choice = int(input("\nWhat type of Result you want to generate? \n1. Bar Graph \n2. Pie Chart \nEnter your response: "))
generate_result(election_id, plot_choice)
elif choice3 == 7:
break
else:
print("Invalid input.")
else:
print("Login Unsuccessful. \nPlease Retry")
break
elif choice2 == 2:
print("\nPlease enter the following required parameters: ")
id = validate_input("Enter USN (10 characters): ", r'^[A-Za-z0-9]{10}$')
if id:
passwd = getpass.getpass("Enter Password: ")
name, user_data = login_voter(id, passwd)
if name:
print("\nWelcome ",name)
while True:
choice3 = int(input("\n1. Vote in a election \n2. Log Out \nEnter your response: "))
if choice3 ==1:
cursr.execute(f"SELECT ELECTION_ID FROM ELECTION WHERE YEAR>={datetime.datetime.now().year}")
elections = cursr.fetchall()
for i in range(len(elections)):
print(str(i+1)+".",elections[i][0])
choice4 = int(input("Select the election you want to vote in: "))
vote_in_election = elections[choice4-1][0]
print(vote_in_election)
cursr.execute(f"SELECT POST_ID, POSTNAME FROM POSTS WHERE ELECTIONID={vote_in_election}")
posts = cursr.fetchall()
cursr.execute(f"SELECT VOTERID FROM VOTES WHERE ELECTIONID={vote_in_election} AND VOTERID=\'{user_data[0]}\'")
temp=cursr.fetchall()
if not temp:
for i in posts:
print()
print("Vote for: ")
print("POST ID: ", i[0])
print("POST NAME: ", i[1])
cursr.execute(f"SELECT USN, NAME FROM CANDIDATES WHERE ELECTIONID={vote_in_election} AND POSTID={i[0]}")
candidates = cursr.fetchall()
for j in range(len(candidates)):
print(str(j+1)+".", candidates[j][0], candidates[j][1])
while True:
vote = int(input("Enter your vote: "))
if vote<=len(candidates):
try:
print(f"INSERT INTO VOTES VALUES (\'{user_data[0]}\', {vote_in_election}, {i[0]}, \'{candidates[vote-1][0]}\')")
cursr.execute(f"INSERT INTO VOTES VALUES (\'{user_data[0]}\', {vote_in_election}, {i[0]}, \'{candidates[vote-1][0]}\');")
conn_obj.commit()
print("Vote registered successfully")
break
except:
print(f"INSERT INTO VOTES VALUES (\'{user_data[0]}\', {vote_in_election}, {i[0]}, \'{candidates[vote-1][0]}\')")
print("Failed to register the vote.")
else:
print("Please enter a valid vote.")
elif choice3 ==2:
break
else:
print("Enter a valid input.")
else:
break
else:
print("Invalid response.")
elif choice == 2:
choice2 = int(input("\nSign up as: \n1. ADMIN \n2. VOTER \nEnter your response:"))
if choice2 == 1:
print("Please enter the following required parameters: ")
insert_admin_values()
print("Sign up successfully completed. Please go back to login.")
elif choice2 == 2:
print("Please enter the following required parameters: ")
insert_voters_values()
else:
print("Invalid response.")
elif choice == 3:
break
else:
print("Please enter a valid choice:")
close_connection()
# if __name__ == "__main__":
# dbms_connect('divyanshu2345prince')
# create_database()
# root = tk.Tk()
# app = VotingSystemGUI(root)
# root.mainloop()
# close_connection()