-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
175 lines (135 loc) · 4.43 KB
/
api.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
from flask import *
from database import *
import demjson
import uuid
from core import *
api=Blueprint('api',__name__)
@api.route('/login',methods=['get','post'])
def login():
data={}
username = request.args['username']
password = request.args['password']
q="SELECT * from login where username='%s' and password='%s'" % (username,password)
print(q)
res = select(q)
if res :
data['status'] = 'success'
data['data'] = res
data['method']='login'
else:
data['status'] = 'failed'
data['method']='login'
return demjson.encode(data)
@api.route('/View_candidate',methods=['get','post'])
def View_candidate():
data={}
login_id=request.args['login_id']
q="SELECT *,CONCAT(`candidate`.`first_name`,' ',`candidate`.`last_name`) AS cname FROM `candidate` INNER JOIN `election` USING (`election_id`) WHERE `candidate_status`='accepted' AND district_id=(SELECT `district_id` FROM `voter` INNER JOIN `booth` USING (`booth_id`) WHERE `voter_id`=(SELECT `voter_id` FROM `voter` WHERE `login_id`='%s'))"%(login_id)
print(q)
res = select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='View_candidate'
return demjson.encode(data)
@api.route('/View_district',methods=['get','post'])
def View_district():
data={}
q="SELECT * FROM `district`"
res = select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='View_district'
return demjson.encode(data)
@api.route('/View_booth',methods=['get','post'])
def View_booth():
data={}
district_ids=request.args['district_ids']
q="SELECT* FROM `booth` WHERE `district_id`='%s'"%(district_ids)
res = select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='View_booth'
return demjson.encode(data)
@api.route('/View_election_status',methods=['get','post'])
def View_election_status():
data={}
q="SELECT* FROM `election`"
res = select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='View_election_status'
return demjson.encode(data)
@api.route('/Make_vote_candidate',methods=['get','post'])
def Make_vote_candidate():
data={}
login_id=request.args['login_id']
election_ids=request.args['election_ids']
q="SELECT *,CONCAT(`candidate`.`first_name`,' ',`candidate`.`last_name`) AS cname FROM `candidate` WHERE `election_id`='%s' and district_id=(SELECT `district_id` FROM `voter` INNER JOIN `booth` USING (`booth_id`) WHERE `voter_id`=(SELECT `voter_id` FROM `voter` WHERE `login_id`='%s'))"%(election_ids,login_id)
res = select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='Make_vote_candidate'
return demjson.encode(data)
@api.route('/make_vote',methods=['get','post'])
def make_vote():
data = {}
data['method']='make_vote'
login_id=request.args['login_id']
candidate_ids=request.args['candidate_ids']
election_ids=request.args['election_ids']
q="SELECT `voter_id` FROM `voter` WHERE `login_id`='%s'"%(login_id)
res=select(q)
vid=res[0]['voter_id']
q="SELECT * FROM `vote` INNER JOIN `candidate` USING (`candidate_id`) INNER JOIN `election` USING (`election_id`) WHERE `election_id`='%s' AND `voter_id`=(SELECT `voter_id` FROM `voter` WHERE `login_id`='%s')"%(election_ids,login_id)
print(q)
res=select(q)
if res:
data['status']='Already Voted'
else:
q = "select * from vote order by vote_id desc"
print(q)
res = select(q)
if len(res) > 0 :
previous_hash = res[0]['hash_value']
else:
previous_hash = '0'
hashing_value = str(vid) + str(election_ids) + str(previous_hash)
new_hash = get_hashed_value(hashing_value)
q="INSERT INTO `vote`(`vote_time`,`voter_id`,`candidate_id`,`hash_value`) VALUES(NOW(),(SELECT `voter_id` FROM `voter` WHERE `login_id`='%s'),'%s','%s')"%(login_id,candidate_ids,new_hash)
print(q)
insert(q)
data['status']='success'
return demjson.encode(data)
@api.route('/View_result',methods=['get','post'])
def View_result():
data={}
q="SELECT *,CONCAT(`candidate`.`first_name`,' ',`candidate`.`last_name`) AS cname FROM `result` INNER JOIN `candidate` USING (`candidate_id`)"
res=select(q)
print(res)
if res :
data['status'] = 'success'
data['data'] = res
else:
data['status'] = 'failed'
data['method']='View_result'
return demjson.encode(data)