-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
230 lines (168 loc) · 6.16 KB
/
app.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# import flask dependencies for web GUI
# from crypt import methods
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from passlib.hash import sha256_crypt
from flask_mysqldb import MySQL
from functools import wraps
import sys
# import other functions and classes
from sqlhelpers import *
from forms import *
import requests
# other dependencies
import time
# initialize the app
app = Flask(__name__)
# configure mysql
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'sql password'
app.config['MYSQL_DB'] = 'test'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# initialize mysql
mysql = MySQL(app)
# wrap to define if the user is currently logged in from session
PORT = sys.argv[1]
connected_users = [{"PORT":5000}, {"PORT":5001}, {"PORT":5002}]
def broadcast_transaction(data):
print("CORRECT")
print(data)
for user in connected_users:
if str(user['PORT']) != PORT:
requests.post('http://localhost:{}/recieve_blockchain'.format(user['PORT']), json={"new_transaction": data[-1]})
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash("Unauthorized, please login.", "danger")
return redirect(url_for('login'))
return wrap
# log in the user by updating session
def log_in_user(username):
users = Table("users", "name", "email", "username", "password")
user = users.getone("username", username)
session['logged_in'] = True
session['username'] = username
session['name'] = user.get('name')
session['email'] = user.get('email')
# Registration page
@app.route("/register", methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
users = Table("users", "name", "email", "username", "password")
# if form is submitted
if request.method == 'POST' and form.validate():
# collect form data
username = form.username.data
email = form.email.data
name = form.name.data
# make sure user does not already exist
if isnewuser(username):
# add the user to mysql and log them in
password = sha256_crypt.encrypt(form.password.data)
users.insert(name, email, username, password)
log_in_user(username)
return redirect(url_for('dashboard'))
else:
flash('User already exists', 'danger')
return redirect(url_for('register'))
return render_template('register.html', form=form)
@app.route("/recieve_blockchain", methods=['POST'])
def recieve_blockchain():
new_data = (request.json)['new_transaction']
function(new_data)
return (new_data)
@app.route("/login", methods=['GET', 'POST'])
def login():
# if form is submitted
if request.method == 'POST':
# collect form data
username = request.form['username']
candidate = request.form['password']
# access users table to get the user's actual password
users = Table("users", "name", "email", "username", "password")
user = users.getone("username", username)
accPass = user.get('password')
# if the password cannot be found, the user does not exist
if accPass is None:
flash("Username is not found", 'danger')
return redirect(url_for('login'))
else:
# verify that the password entered matches the actual password
if sha256_crypt.verify(candidate, accPass):
# log in the user and redirect to Dashboard page
log_in_user(username)
flash('You are now logged in.', 'success')
return redirect(url_for('dashboard'))
else:
# if the passwords do not match
flash("Invalid password", 'danger')
return redirect(url_for('login'))
return render_template('login.html')
# Transaction page
@app.route("/transaction", methods=['GET', 'POST'])
@is_logged_in
def transaction():
form = SendMoneyForm(request.form)
balance = get_balance(session.get('username'))
# if form is submitted
if request.method == 'POST':
try:
# attempt to execute the transaction
send_money(session.get('username'),
form.username.data, form.amount.data)
broadcast_transaction(dict_blockchain())
flash("Money Sent!", "success")
except Exception as e:
flash(str(e), 'danger')
return redirect(url_for('transaction'))
return render_template('transaction.html', balance=balance, form=form, page='transaction')
# Buy page
@app.route("/buy", methods=['GET', 'POST'])
@is_logged_in
def buy():
form = BuyForm(request.form)
balance = get_balance(session.get('username'))
if request.method == 'POST':
# attempt to buy amount
try:
send_money("BANK", session.get('username'), form.amount.data)
broadcast_transaction(dict_blockchain())
flash("Purchase Successful!", "success")
except Exception as e:
flash(str(e), 'danger')
return redirect(url_for('dashboard'))
return render_template('buy.html', balance=balance, form=form, page='buy')
# logout the user. Ends current session
@app.route("/logout")
@is_logged_in
def logout():
session.clear()
flash("Logout success", "success")
return redirect(url_for('login'))
# Dashboard page
@app.route("/dashboard")
@is_logged_in
def dashboard():
balance = get_balance(session.get('username'))
blockchain = get_blockchain().chain
ct = time.strftime("%I:%M %p")
return render_template('dashboard.html', balance=balance, session=session, ct=ct, blockchain=blockchain, page='dashboard')
# Index page
@app.route("/")
@app.route("/index")
def index():
return render_template('index.html')
# about us
@app.route("/")
@app.route("/aboutUs")
def aboutUs():
return render_template('aboutUs.html')
# Run app
if __name__ == '__main__':
app.secret_key = 'secret123'
app.run(debug=True, port=PORT)