forked from IndianHills-CSD/Vote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.py
167 lines (125 loc) · 4.13 KB
/
vote.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
#! C:\Python38-32\python.exe -u
import cgi, cgitb, re, encryptionlib as enc, os, mysql.connector as mysql, http.cookies as c
from connectlib import connect_db
from cookielib import get_cookie
def valid_vote(party, can):
"""
Checks if a vote that was entered is valid
"""
global errmsgs
errors = 0
# Political Party Validation
if len(party.strip()) == 0:
errors += 1
errmsgs.append(" <p>No political party was selected</p>")
# Candidate Validation
# Regex pattern for validating candidates
canformat = re.search("^([A-Z|a-z]{1,}[\s][A-Z|a-z]{1,})$", can)
if len(can.strip()) == 0:
errors += 1
errmsgs.append(" <p>No candidate was entered</p>")
elif not canformat:
errors += 1
errmsgs.append(
' <div class="center">\n\t\t <p>The name of a candidate should include their firstname and lastname</p>\n\t\t </div>'
)
if errors == 0:
errors += not_voted()
return errors
def not_voted():
"""
Checks if the user has not voted yet
"""
errors = 0
try:
accid = find_accid() # gets an id
# Prepare SELECT statement
prep_select = "SELECT * FROM votes WHERE accId = %s"
# A tuple is always used when binding placeholders (%s)
cursor.execute(
prep_select, (accid,)
) # (value,) used when searching for a single value
result = cursor.fetchall() # returns a list of tuples
if result:
errors += 1
errmsgs.append(" <p>You can only vote once</p>")
else:
insert_vote()
except mysql.Error as e:
errors += 1
msg = " <p> " + str(e) + " </p>"
errmsgs.append(msg)
return errors
def insert_vote():
"""
Stores the vote that was place by the user
"""
accid = find_accid() # gets an id
# Prepare INSERT statement
prep_insert = "INSERT INTO votes (accId, candidate, polParty) VALUES (%s, %s, %s)"
# A tuple should always be used to bind placeholders (%s)
cursor.execute(prep_insert, (accid, can, party[0]))
db.commit() # saves changes
def find_accid():
"""
Finds the id of an account for the Votes table
"""
accid = 0
uname = get_cookie()
# Prepare SELECT statement
prep_select = "SELECT accId FROM accounts WHERE uname = %s"
cursor.execute(prep_select, (uname,))
result = cursor.fetchall() # returns a list of tuples
if result:
(val_id,) = result[0] # unpacks the tuple
accid = int(val_id)
return accid
# Intializes an empty list of error messages
errmsgs = []
# Connects to the database
db = connect_db()
cursor = db.cursor(prepared=True) # allows the prepare statement to be used
errctr = 0 # keeps track of all errors
form = cgi.FieldStorage()
# Vote Validation
if "party" in form and "can" in form:
party = form.getlist("party")
can = form.getvalue("can")
else:
if "party" not in form:
party = [""]
else:
party = form.getlist("party")
if "can" not in form:
can = ""
else:
can = form.getvalue("can")
errctr += valid_vote(party[0], can)
print("Content-Type: text/html\n")
# HTML code that is always printed
print("<!DOCTYPE html>")
print('<html lang="en">')
print(" <head>")
print(" <title>Vote</title>")
print(" <link rel='stylesheet' href='css/main-styles.css'>")
print(" </head>")
print(" <body>")
print(' <div id="container">')
print(' <div id="content">')
if errctr == 0:
# For when the page is still redirecting
print(" <h1>Thank you for voting!</h1>")
print(" <p>Your vote was accepted!</p>")
print(' <a href="index.html">Click here to return to the home page</a>')
else:
# Printed when invalid data is entered
print(" <h1>Error</h1>")
# Prints any error messages when errors occur
for i in range(errctr):
print(errmsgs[i])
print(' <a href="vote.html">Click here to fix your mistakes</a>')
# More HTML code that is always printed
print(" </div>")
print(" </div>")
print(" </body>")
print("</html>")