-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
336 lines (273 loc) · 10.4 KB
/
admin.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
from flask import *
from database import *
admin=Blueprint('admin',__name__)
@admin.route('/admin_home')
def admin_home():
if session.get('lid'):
return render_template('admin_home.html')
else:
return redirect(url_for("public.login"))
@admin.route('/admin_manage_district',methods=['get','post'])
def admin_manage_district():
if session.get('lid'):
data={}
if 'action' in request.args:
action=request.args['action']
ids=request.args['ids']
else:
action=None
if action=='update':
q="SELECT * FROM `district` WHERE `login_id`='%s'"%(ids)
res=select(q)
data['dist_up']=res
if 'submits' in request.form:
dname=request.form['dname']
q="UPDATE `district` SET `district`='%s' WHERE `login_id`='%s'"%(dname,ids)
update(q)
flash('Details updated')
return redirect(url_for('admin.admin_manage_district'))
if action=='delete':
q="DELETE FROM `district` WHERE `login_id`='%s'"%(ids)
delete(q)
print(q)
q1="DELETE FROM `login` WHERE `login_id`='%s'"%(ids)
delete(q1)
flash('Details deleted')
return redirect(url_for('admin.admin_manage_district'))
if 'submit' in request.form:
dname=request.form['dname']
uname=request.form['uname']
password=request.form['password']
q="SELECT * from `district` WHERE `district`='%s'"%(dname)
res=select(q)
q1="SELECT * FROM `login` WHERE `username`='%s'"%(uname)
res1=select(q1)
if res:
flash('DISTRICT ALREADY INSERTED')
elif res1:
flash('USER NAME ALREADY TAKEN')
else:
q="INSERT INTO `login`(`username`,`password`,`login_type`) VALUES ('%s','%s','district')"%(uname,password)
id=insert(q)
q1="INSERT INTO `district`(`login_id`,`district`) VALUES ('%s','%s')"%(id,dname)
insert(q1)
flash('DISTRICT DETAILS SUBMITED')
return redirect(url_for('admin.admin_manage_district'))
q="SELECT * FROM `district`"
res=select(q)
data['dist']=res
return render_template('admin_manage_district.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_manage_booth',methods=['get','post'])
def admin_manage_booth():
if session.get('lid'):
data={}
if 'action' in request.args:
action=request.args['action']
ids=request.args['ids']
else:
action=None
if action=='update':
q="SELECT * FROM `booth` WHERE `login_id`='%s'"%(ids)
res=select(q)
data['booth_up']=res
if 'submits' in request.form:
dname=request.form['dname']
q="UPDATE `booth` SET `booth`='%s' WHERE `login_id`='%s'"%(dname,ids)
update(q)
flash('Booth updated')
return redirect(url_for('admin.admin_manage_booth'))
q="SELECT * FROM `booth`"
res=select(q)
data['booth']=res
if action=='delete':
q="DELETE FROM `booth` WHERE `login_id`='%s'"%(ids)
delete(q)
q1="DELETE FROM `login` WHERE `login_id`='%s'"%(ids)
delete(q1)
flash('Booth deleted')
return redirect(url_for('admin.admin_manage_booth'))
q="SELECT * FROM `district`"
res=select(q)
data['district']=res
if 'submit' in request.form:
district_id=request.form['district']
bname=request.form['bname']
uname=request.form['uname']
password=request.form['password']
q="SELECT * FROM `booth` WHERE `booth`='%s'"%(bname)
res=select(q)
q1="SELECT * FROM `login` WHERE `username`='%s'"%(uname)
res1=select(q1)
if res:
flash('BOOTH NAME ALREADY INSERTED')
elif res1:
flash('USERNAME ALREADY TAKEN')
else:
q="INSERT INTO `login`(`username`,`password`,`login_type`) VALUES ('%s','%s','booth')"%(uname,password)
id=insert(q)
q1="INSERT INTO `booth`(`login_id`,`district_id`,`booth`) VALUES ('%s','%s','%s')"%(id,district_id,bname)
insert(q1)
flash('DETAILS SUBMITED')
return redirect(url_for('admin.admin_manage_booth'))
return render_template('admin_manage_booth.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_manage_voters',methods=['get','post'])
def admin_manage_voters():
if session.get('lid'):
data={}
if 'action' in request.args:
action=request.args['action']
ids=request.args['ids']
else:
action=None
if action=='approve':
q1="UPDATE `login` SET `login_type`='voter' WHERE `login_id`='%s'"%(ids)
update(q1)
return redirect(url_for('admin.admin_manage_voters'))
if action=='reject':
q1="UPDATE `login` SET `login_type`='reject' WHERE `login_id`='%s'"%(ids)
update(q1)
return redirect(url_for('admin.admin_manage_voters'))
if 'submit' in request.form:
eno=request.form['eno']
q="SELECT *,CONCAT (`voter`.`first_name`,' ',`voter`.`last_name`) AS voter,voter.login_id as lids FROM `voter` INNER JOIN `booth` USING(`booth_id`) INNER JOIN `election` USING(`election_id`) INNER JOIN `login` ON (`voter`.`login_id`=`login`.`login_id`) WHERE `aadhar`='%s'"%(eno)
res=select(q)
data['votor']=res
else:
q="SELECT *,CONCAT (`voter`.`first_name`,' ',`voter`.`last_name`) AS voter,voter.login_id as lids FROM `voter` INNER JOIN `booth` USING(`booth_id`) INNER JOIN `election` USING(`election_id`) INNER JOIN `login` ON (`voter`.`login_id`=`login`.`login_id`) ORDER BY FIELD(`login_type`, 'pending', 'reported', 'voter', 'reject') ASC"
res=select(q)
data['votor']=res
return render_template('admin_manage_voters.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_manage_candidate',methods=['get','post'])
def admin_manage_candidate():
if session.get('lid'):
data={}
if 'action' in request.args:
action=request.args['action']
ids=request.args['ids']
else:
action=None
if action=='approve':
q1="UPDATE `login` SET `login_type`='candidate' WHERE `login_id`='%s'"%(ids)
update(q1)
q="UPDATE `candidate` SET `candidate_status`='accepted' WHERE `login_id`='%s'"%(ids)
update(q)
return redirect(url_for('admin.admin_manage_candidate'))
if action=='reject':
q1="UPDATE `login` SET `login_type`='reject' WHERE `login_id`='%s'"%(ids)
update(q1)
q="UPDATE `candidate` SET `candidate_status`='reject' WHERE `login_id`='%s'"%(ids)
update(q)
return redirect(url_for('admin.admin_manage_candidate'))
q="SELECT *,CONCAT (`candidate`.`first_name`,' ',`candidate`.`last_name`) AS candidate,candidate.login_id as lids FROM `candidate` INNER JOIN `district` USING(`district_id`) INNER JOIN `election` USING(`election_id`) INNER JOIN `login` ON (`candidate`.`login_id`=`login`.`login_id`) ORDER BY FIELD(`login_type`, 'pending' , 'candidate' , 'reject') ASC"
res=select(q)
data['candidate']=res
return render_template('admin_manage_candidate.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_manage_election',methods=['get','post'])
def admin_manage_election():
if session.get('lid'):
if 'submit' in request.form:
body=request.form['body']
edate=request.form['edate']
ddate=request.form['date']
q="INSERT INTO `election` (`body`,`election_date`,`declared_on`,`status`) VALUES('%s','%s','%s','pending')"%(body,edate,ddate)
insert(q)
data={}
q="SELECT * FROM `election` "
res=select(q)
data['election']=res
if 'action' in request.args:
action=request.args['action']
eid=request.args['eid']
else:
action=None
if action=='started':
q="UPDATE `election` SET `status`='started' WHERE `election_id`='%s'"%(eid)
update(q)
return redirect(url_for('admin.admin_manage_election'))
if action=='completed':
q="UPDATE `election` SET `status`='completed' WHERE `election_id`='%s'"%(eid)
update(q)
q="INSERT INTO `result`(candidate_id,total_vote) SELECT candidate_id,COUNT(*) AS total_vote FROM vote GROUP BY candidate_id ORDER BY total_vote DESC "
insert(q)
return redirect(url_for('admin.admin_manage_election'))
if 'action' in request.args:
action=request.args['action']
eid=request.args['eid']
else:
action=None
if action=='delete':
q="DELETE FROM `election` WHERE `election_id`='%s'"%(eid)
delete(q)
flash('DELETED')
return redirect(url_for('admin.admin_manage_election'))
if action=='update':
q="SELECT * FROM `election` WHERE `election_id`='%s'"%(eid)
data['election_up']=select(q)
if 'updatez' in request.form:
body=request.form['body']
edate=request.form['edate']
q="UPDATE `election` SET `election_date`='%s',body='%s' WHERE `election_id`='%s'"%(edate,body,eid)
update(q)
flash('UPDATED')
return redirect(url_for('admin.admin_manage_election'))
return render_template('admin_manage_election.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_view_result',methods=['get','post'])
def admin_view_result():
if session.get('lid'):
data={}
q="SELECT *,CONCAT (`candidate`.`first_name`,' ',`candidate`.`last_name`) AS candidate FROM `candidate` INNER JOIN `result` USING(`candidate_id`) INNER JOIN district USING(`district_id`) ORDER BY(`district`)"
res=select(q)
data['result']=res
return render_template('admin_view_result.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_realtime_result',methods=['get','post'])
def admin_realtime_result():
if session.get('lid'):
data={}
q="SELECT * FROM `election` WHERE `election_id`='%S'"%(eid)
res=select(q)
data['election']=res
return render_template('admin_realtime_result.html',data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/admin_count_vote')
def admin_count_vote():
if session.get('lid'):
data={}
q="SELECT * FROM `election`"
res=select(q)
data['election']=res
if 'action' in request.args:
action=request.args['action']
eid=request.args['id']
else:
action=None
if action=='count':
q="SELECT COUNT(`candidate_id`) AS COUNT,`candidate`.`candidate_id`,`election_id`,`district`,CONCAT (`candidate`.`first_name`,' ',`candidate`.`last_name`) AS candidate FROM `vote` INNER JOIN `candidate` USING (`candidate_id`) INNER JOIN `district` USING(`district_id`) INNER JOIN `election` USING (`election_id`) WHERE `election_id`='%s' GROUP BY `candidate_id` ORDER BY(`district`)"%(eid)
res1=select(q)
data['status']=res1
return render_template('admin_realtime_result.html',data=data)
return render_template("admin_count_vote.html",data=data)
else:
return redirect(url_for("public.login"))
@admin.route('/clear_tables',methods=['get','post'])
def clear_tables():
data={}
tables = ['booth','candidate','district','election','login','result','vote','voter']
for table in tables:
q="TRUNCATE TABLE %s"%(table)
update(q)
q="INSERT INTO `login` VALUES (null,'admin','admin','admin')"
insert(q)
return redirect(url_for('public.home'))