-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
229 lines (164 loc) · 5.92 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
from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_bcrypt import Bcrypt
from flask_login import LoginManager, login_required, login_user, logout_user, current_user
from models.Users import User
from models.Users import db
import re
from flask_admin import Admin
# setup the app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = "SuperSecretKey"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db.init_app(app)
bcrypt = Bcrypt(app)
# setup the login manager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# create the db structure
with app.app_context():
db.create_all()
#### setup routes ####
@app.route('/admin')
@login_required
def index():
return render_template('admin.html', user=current_user)
@app.route('/')
def index_admin():
return render_template('indexi.html', user=current_user)
@app.route('/contact')
def contact():
return render_template('contact.html', user=current_user)
@app.route("/login", methods=["GET", "POST"])
def login():
# clear the inital flash message
session.clear()
if request.method == 'GET':
return render_template('login.html')
# get the form data
username = request.form['username']
password = request.form['password']
remember_me = False
if 'remember_me' in request.form:
remember_me = True
# query the user
registered_user = User.query.filter_by(username=username).first()
# check the passwords
if registered_user is None and bcrypt.check_password_hash(registered_user.password, password) == False:
flash('Invalid Username/Password')
return render_template('login.html')
# login the user
login_user(registered_user, remember=remember_me)
return redirect(request.args.get('next') or url_for('index'))
@app.route('/newadmin', methods=["GET", "POST"])
def newadmin():
if request.method == 'GET':
session.clear()
return render_template('register.html')
# get the data from our form
password = request.form['password']
conf_password = request.form['confirm-password']
username = request.form['username']
email = request.form['email']
# make sure the password match
if conf_password != password:
flash("Passwords do not match")
return render_template('register.html')
# check if it meets the right complexity
check_password = password_check(password)
# generate error messages if it doesnt pass
if True in check_password.values():
for k,v in check_password.iteritems():
if str(v) is "True":
flash(k)
return render_template('register.html')
# hash the password for storage
pw_hash = bcrypt.generate_password_hash(password)
# create a user, and check if its unique
user = User(username, pw_hash, email)
u_unique = user.unique()
# add the user
if u_unique == 0:
db.session.add(user)
db.session.commit()
flash("Account Created")
return redirect(url_for('login'))
# else error check what the problem is
elif u_unique == -1:
flash("Email address already in use.")
return render_template('register.html')
elif u_unique == -2:
flash("Username already in use.")
return render_template('register.html')
else:
flash("Username and Email already in use.")
return render_template('register.html')
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/charts')
def charts():
return render_template('charts.html', user=current_user)
@app.route('/tables')
def tables():
return render_template('tables.html', user=current_user)
@app.route('/forms')
def forms():
return render_template('forms.html', user=current_user)
@app.route('/bootstrap-elements')
def bootstrap_elements():
return render_template('bootstrap-elements.html', user=current_user)
@app.route('/bootstrap-grid')
def bootstrap_grid():
return render_template('bootstrap-grid.html', user=current_user)
@app.route('/blank-page')
def blank_page():
return render_template('blank-page.html', user=current_user)
@app.route('/profile')
def profile():
return render_template('profile.html', user=current_user)
@app.route('/settings')
def settings():
return render_template('settings.html', user=current_user)
#### end routes ####
# required function for loading the right user
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
# check password complexity
def password_check(password):
"""
Verify the strength of 'password'
Returns a dict indicating the wrong criteria
A password is considered strong if:
8 characters length or more
1 digit or more
1 symbol or more
1 uppercase letter or more
1 lowercase letter or more
credit to: ePi272314
"""
# calculating the length
length_error = len(password) <= 8
# searching for digits
digit_error = re.search(r"\d", password) is None
# searching for uppercase
uppercase_error = re.search(r"[A-Z]", password) is None
# searching for lowercase
lowercase_error = re.search(r"[a-z]", password) is None
# searching for symbols
symbol_error = re.search(r"[ !@#$%&'()*+,-./[\\\]^_`{|}~"+r'"]', password) is None
ret = {
'Password is less than 8 characters' : length_error,
'Password does not contain a number' : digit_error,
'Password does not contain a uppercase character' : uppercase_error,
'Password does not contain a lowercase character' : lowercase_error,
'Password does not contain a special character' : symbol_error,
}
return ret
if __name__ == "__main__":
# change to app.run(host="0.0.0.0"), if you want other machines to be able to reach the webserver.
app.run()