-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathviews.py
100 lines (81 loc) · 2.98 KB
/
views.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
import datetime
import json
import requests
from flask import render_template, redirect, request
from flask import flash
from app import app
# The node with which our application interacts, there can be multiple
# such nodes as well.
CONNECTED_SERVICE_ADDRESS = "http://127.0.0.1:8000"
POLITICAL_PARTIES = ["Democratic Party","Republican Party","Socialist party"]
VOTER_IDS=[
'VOID001','VOID002','VOID003',
'VOID004','VOID005','VOID006',
'VOID007','VOID008','VOID009',
'VOID010','VOID011','VOID012',
'VOID013','VOID014','VOID015']
vote_check=[]
posts = []
def fetch_posts():
"""
Function to fetch the chain from a blockchain node, parse the
data and store it locally.
"""
get_chain_address = "{}/chain".format(CONNECTED_SERVICE_ADDRESS)
response = requests.get(get_chain_address)
if response.status_code == 200:
content = []
vote_count = []
chain = json.loads(response.content)
for block in chain["chain"]:
for tx in block["transactions"]:
tx["index"] = block["index"]
tx["hash"] = block["previous_hash"]
content.append(tx)
global posts
posts = sorted(content, key=lambda k: k['timestamp'],
reverse=True)
@app.route('/')
def index():
fetch_posts()
vote_gain = []
for post in posts:
vote_gain.append(post["party"])
return render_template('index.html',
title='E-voting system '
'using Blockchain and python',
posts=posts,
vote_gain=vote_gain,
node_address=CONNECTED_SERVICE_ADDRESS,
readable_time=timestamp_to_string,
political_parties=POLITICAL_PARTIES,
voter_ids=VOTER_IDS)
@app.route('/submit', methods=['POST'])
def submit_textarea():
"""
Endpoint to create a new transaction via our application.
"""
party = request.form["party"]
voter_id = request.form["voter_id"]
post_object = {
'voter_id': voter_id,
'party': party,
}
if voter_id not in VOTER_IDS:
flash('Voter ID invalid, please select voter ID from sample!', 'error')
return redirect('/')
if voter_id in vote_check:
flash('Voter ID ('+voter_id+') already vote, Vote can be done by unique vote ID only once!', 'error')
return redirect('/')
else:
vote_check.append(voter_id)
# Submit a transaction
new_tx_address = "{}/new_transaction".format(CONNECTED_SERVICE_ADDRESS)
requests.post(new_tx_address,
json=post_object,
headers={'Content-type': 'application/json'})
# print(vote_check)
flash('Voted to '+party+' successfully!', 'success')
return redirect('/')
def timestamp_to_string(epoch_time):
return datetime.datetime.fromtimestamp(epoch_time).strftime('%Y-%m-%d %H:%M')