-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
274 lines (224 loc) · 7.08 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# DESCRIPTION
"""
FLASK APPLICATION: Dabdoub (DAB)
Cryptocurrency in python.
app.py is main program.
"""
__author__ = "Adam Dabdoub"
__copyright__ = "Copyright 2021, Dabdoub"
__credits__ = ["Adam Dabdoub"]
__developers__ = ["adamdabdoub "]
__license__ = "GPL"
__version__ = "1.0."
__maintainer__ = "Adam Dabdoub"
__email__ = "[email protected]"
__status__ = "Production"
#IMPORTS
from flask import (Flask, render_template,
flash, redirect, url_for,
session, request, logging)
from cryptography.passwordtools import get_pass
from passlib.hash import sha256_crypt
from flask_mysqldb import MySQL
from functools import wraps
from appfuncs import *
from forms import *
import time
import os
#initialize app instance
app = Flask(__name__)
#use mysql to store datasets
# Configure MySQL settings
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'Tilt58Knob!'
app.config['MYSQL_DB'] = 'dabdoub'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# initialize MySQL
mysql = MySQL(app)
"""WRAPS: verify if user is logged in,
is a manager, or is a developer
"""
def sflash(message): flash(message, 'success')
def dflash(message): flash(message, 'danger')
#Check if user is logged in using wraps
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
#Check if user is logged in using wraps
def is_logged_out(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
flash('You are already logged in.', 'danger')
return redirect(url_for('dashboard'))
else:
return f(*args, **kwargs)
return wrap
#Check if user is developer using wraps
def is_developer(f):
@wraps(f)
def wrap(*args, **kwargs):
if session.get('username') in __developers__:
return f(*args, **kwargs)
else:
return redirect(url_for('dashboard'))
return wrap
"""BASIC PAGES: /, /home
ONLY HTML, BOOTSTRAP, CSS
"""
@app.route("/")
@app.route("/index")
@app.route("/home")
def index():
return render_template("index.html")
@app.route("/blank")
@is_developer
def blank_page():
return render_template("blank.html")
"""REGISTER AND LOGIN PAGES
"""
#Register page for students
@app.route("/register", methods = ['GET','POST'])
@is_logged_out
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
username = form.username.data
email = form.email.data
name = form.name.data
if isnewuser(username) and isnewtable(username):
password = sha256_crypt.encrypt(form.password.data)
sql_raw(
"INSERT INTO users(name,email,username,password)" +
"VALUES(\"%s\", \"%s\", \"%s\", \"%s\")" %(
name,email,username,password
)
)
send_money(username,0,True); log_in_user(username)
sflash('Welcome to your dashboard %s.' %username)
return redirect(url_for('dashboard'))
else:
dflash('User already exists.')
return redirect(url_for('register'))
return render_template('register.html', form=form)
def log_in_user(username):
users = Table("users")
user = users.getone("username", username)
accpass = user.get('password')
balances = getbalances()
session['logged_in'] = True
session['username'] = username
session['name'] = user.get('name')
session['email'] = user.get('email')
session['balance'] = balances.get(username)
#Login page for students
@app.route("/login", methods=['GET','POST'])
@is_logged_out
def login():
if request.method == 'POST':
username = request.form['username']
candidate = request.form['password']
users = Table("users")
user = users.getone("username", username)
accpass = user.get('password')
if accpass is None:
dflash('Username "%s" not found.' %username)
return redirect(url_for('login'))
else:
if sha256_crypt.verify(candidate, accpass):
log_in_user(username)
sflash('You are now logged in.')
return redirect(url_for('dashboard'))
else:
dflash('Invalid username and/or password.')
return redirect(url_for('login'))
return render_template('login.html')
#Logout, ends current session
@app.route("/logout")
@is_logged_in
def logout():
session.clear()
sflash('You have now logged out.')
return redirect(url_for('login'))
#Dashboard
@app.route("/dashboard", methods=['GET','POST'])
@is_logged_in
def dashboard():
current_time = time.strftime("%I:%M %p")
blockchain = getLastBlockchain()
if not verifyBlockchain():
dflash("Corrupt blockchain.")
return redirect(url_for('index'))
return render_template(
'dashboard.html',
page='dashboard',
ct=current_time,
blockchain=blockchain,
session=session
)
@app.route("/transaction", methods=['GET','POST'])
@is_logged_in
def transaction():
form = SendMoneyForm(request.form)
if request.method == 'POST' and form.validate():
username = form.username.data
amount = form.amount.data
if not amount.isdigit():
dflash('Must enter number value for amount.')
elif int(amount) > session.get('balance'):
dflash('Amount exceeds current balance.')
elif username == session.get('username'):
dflash('Cannot send money to yourself.')
elif isnewuser(username):
dflash('User does not exist, cannot send.')
else:
send_money(username,int(amount))
sflash('%s DAB successfully sent to %s.' %(amount,username))
return redirect(url_for('transaction'))
return render_template(
'transaction.html',
page='transaction',
form=form,
session=session
)
#Buy DAB
@app.route("/buy", methods=['GET','POST'])
@is_logged_in
def buy():
form = BuyDabdoub(request.form)
if request.method == 'POST' and form.validate():
amount = form.amount.data
key = form.key.data
if key == 'password':
send_money(session.get('username'),int(amount),True)
sflash('Transaction successful.')
else:
dflash('Transaction failed, incorrect key.')
return redirect(url_for('buy'))
return render_template(
'buy.html',
page='buy',
form=form,
session=session
)
"""ERROR HANDLERS 404"""
#Handle 404 error
@app.errorhandler(404)
def page_not_found(e):
return render_template('handlers/404.html'), 404
#Run as main, not module
if __name__ == "__main__":
if not internet_connected():
print("CHECK CONNECTION.")
else:
app.secret_key = """YOUR APP SECRET KEY"""
app.run(debug = True, port = 65013)