-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.py
143 lines (119 loc) · 4.38 KB
/
application.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
from flask import Flask, render_template, request, redirect, url_for
from opioid_dict import src_dict, center_dict, special_dict, counties, names, MEcounties, cities
from flask_login import LoginManager, login_required, login_user, logout_user, UserMixin, current_user
from flask_bcrypt import Bcrypt
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from itertools import chain
import os
application = Flask(__name__)
application.secret_key = b'whatisacsrf#$^@&^@#!&^:{>}'
login_manager = LoginManager()
login_manager.init_app(application)
login_manager.login_view = '/login'
bcrypt = Bcrypt(application)
class User(UserMixin):
def __init__(self, email, username, pw_raw, authenticated):
self.email = email
self.username = username
self.password = bcrypt.generate_password_hash(pw_raw).decode('UTF-8') # 'UTF-8' needed for Python 3.X
self.authenticated = authenticated
def get_id(self):
return self.email
def is_authenticated(self):
return self.authenticated
@staticmethod
def authenticate(username, pw_raw):
fetched_user = user1 #hardcoded user1
if fetched_user:
authenticated_user = bcrypt.check_password_hash(fetched_user.password, pw_raw)
fetched_user.authenticated = True
else:
authenticated_user = False
return fetched_user, authenticated_user
class LoginForm(FlaskForm):
username = StringField('Username')
password = PasswordField('Password')
submit = SubmitField('Submit')
user1 = User('flask@login', os.getenv('User'), os.getenv('Pwd'), False)
@login_manager.user_loader
def load_user(user_id):
user1.email = user_id
return user1
# Route for handling the login page logic
@application.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('searchpage'))
error = None
form = LoginForm()
if form.validate_on_submit():
fetched_user, authenticated_user = User.authenticate(form.username.data, form.password.data)
if fetched_user and authenticated_user:
login_user(fetched_user, remember=True)
return fetched_user and redirect(url_for('searchpage'))
else:
error = 'Invalid Credentials.'
return render_template('login.html', form=form, error=error)
@application.route("/logout")
@login_required
def logout():
user = current_user
user.authenticated = False
logout_user()
return redirect(url_for('login'))
@login_manager.unauthorized_handler
def unauthorized():
form = LoginForm()
redirerror = 'You must login first.'
return render_template('login.html', form=form, redirerror=redirerror)
@application.route('/')
def landing():
data = {'MEcounties': MEcounties}
return render_template('landing.html', data=data)
@application.route('/about')
def about():
return render_template('about.html')
@application.route('/contact')
def contact():
return render_template('contact.html')
@application.route('/howtouse')
def howtouse():
return render_template('howtouse.html')
@application.route('/search', methods=['GET'])
@login_required
def searchpage():
data = {'placenames': list(chain.from_iterable(names.values()))}
citylst = cities
return render_template("search.html", data=data, citylst = citylst)
@application.route('/dashboard', methods=['GET'])
@login_required
def navtocorrect():
source = request.args.get('src', default = "EMS", type = str).upper()
city = request.args.get('city')
county = request.args.get('county')
city_flag = True if city else False
if city_flag:
placename = city.title()
titlename = special_dict.get(placename, placename)
folder = 'cities'
else:
placename = county.title()
titlename = f'{placename} County'
folder = 'counties'
data = {
'placename': placename,
'titlename': titlename,
'src': source,
'f_geojson': f'geojson/{folder}/{placename}.geojson',
'center': center_dict[folder][placename]['center'],
'zoom': center_dict[folder][placename].get('zoom', 11),
'names': names,
'counties': counties,
'placetype': folder.replace('ies','y'),
}
return render_template("dashboard.html", data=data)
#%% Run Flask app
# python application.py
if __name__ == '__main__':
application.run(debug=True)