-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
167 lines (150 loc) · 4.96 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
from pydantic import BaseModel
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from blockchain.chain import Chain
from db.userdata import UserData
import utils
import sys
import uvicorn
print("Starting Decentralised Voting System")
token = input("Create a access token: ")
orgDetails, orgNames, leaderNames, orgCodes = utils.takeOrgDetails()
if len(orgDetails.keys()) < 2:
print("At least two organisation shoudl be registered")
sys.exit(1)
chain = Chain(10)
userData = UserData()
orgVoteCount: "dict[str, list[str]]" = {}
castedVoters : "list[str]" = []
print("Voting system is ready")
controller = {
"isStarted": False,
"isStoped": False
}
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def showHome(request: Request):
return templates.TemplateResponse(
"item.html", {
"request": request
}
)
@app.get("/confirmation", response_class=HTMLResponse)
def showConfirmation(request: Request):
return templates.TemplateResponse(
"confirma.html", {
"request": request
}
)
@app.get("/getOrgDetail")
def getOrgDetail():
return {
"orgNames": orgNames,
"leaderNames": leaderNames,
"orgCodes": orgCodes,
}
@app.post("/admin/start")
def startVoting(item: utils.Token):
if (item.token == token):
controller["isStarted"] = True
print("System is running: Voter can cast there vote")
return {
"status": "started"
}
@app.post("/admin/stop")
def stopVoting(item: utils.Token):
if item.token == token:
controller["isStoped"] = True
print("System is stoped")
return {
"status": "stoped"
}
@app.post("/voter/vote")
async def CastVote(request: Request):
formData = await request.form()
formDict = formData._dict
print(formData)
print("Reached")
if controller["isStarted"]:
if formDict["userId"] in userData.data:
if formDict["userId"] not in castedVoters:
try:
orgName = orgDetails[formDict["orgCode"]]
chain.add_to_pool(orgName)
hash: str = chain.mine()
try:
votes = orgVoteCount[orgName]
votes.append(hash)
orgVoteCount[orgName] = votes
except KeyError:
orgVoteCount[orgName] = [hash]
castedVoters.append(formDict["userId"])
return templates.TemplateResponse(
"confirm.html", {
"request": request,
"status": "successfull",
"organisation": orgName,
"description": "Hash -> " + hash
}
)
except KeyError:
return templates.TemplateResponse(
"confirm.html", {
"request": request,
"status": "unsuccessful",
"description": "Invalid Organisation Details"
}
)
else:
return templates.TemplateResponse(
"confirm.html", {
"request": request,
"status": "unsuccessful",
"description": "you have already casted your vote"
}
)
else:
return templates.TemplateResponse(
"confirm.html", {
"request": request,
"status": "unsuccessful",
"description": "Invalid userId"
}
)
else:
return templates.TemplateResponse(
"confirma.html", {
"request": request,
"status": "unsuccessful",
"description": "Voting is closed or it is not started"
}
)
@app.get("/getvotes")
def getTotalVotes():
if not controller["isStoped"]:
return {
"total_votes": 0,
"status": "Voting is currently running"
}
return {
"total_votes" : len(chain.blocks)-1,
"status": "voting has been stoped"
}
@app.get("/getvotesbyorg")
def getVotesByOrg():
if not controller["isStoped"]:
return {
"status": "Voting is currently running"
}
result = {}
for i in range(len(orgVoteCount.keys())):
org = list(orgVoteCount.keys())[i]
count = len(list(orgVoteCount.values())[i])
result[org] = count
return result
if __name__ == "__main__":
uvicorn.run(app)