-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
134 lines (102 loc) · 3.59 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
# import the Flask class from the flask module
from flask import Flask, render_template, redirect, url_for, request, session
from flask_googlemaps import GoogleMaps
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from Map.map import periodic_psi_check
# create the application object
app = Flask(__name__)
app.secret_key = "aD1R3s2"
# db part
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from Dashboard.dashboard import dashboard_api
from AccountManagement.account_management import account_api
from Map.map import map_api
from CallCenter.CallCenter_Controller import callcenter_api
from flask_apscheduler import APScheduler
from flask_mail import Mail, Message
import os
app.register_blueprint(dashboard_api)
app.register_blueprint(account_api, url_prefix='/account')
app.register_blueprint(map_api, url_prefix='/map')
app.register_blueprint(callcenter_api, url_prefix='/callcenter')
# for google maps blueprint. not sure if this is a good design
API_KEY = "AIzaSyDc1Hx9zrh10qY4FSl-A0OwIVKRNTBkZGs"
app.config['GOOGLEMAPS_KEY'] = API_KEY
GoogleMaps(app, key=API_KEY)
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
JOBS = [
{
'id': 'job1',
'func': 'Dashboard.report:send_report',
'args': None,
'trigger': 'interval',
'seconds': 60*30
}
]
SCHEDULER_API_ENABLED = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
app.config.from_object(Config)
mail = Mail(app)
@app.route('/', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def login():
"""
View function for login page.
Returns:
render a template.
"""
error = None
if request.method == 'POST':
if not __verify_login(request.form['username'], request.form['password'], request.form['role']):
error = 'Invalid Credentials. Please try again.'
else:
session["logged_in"] = True
session["username"] = request.form['username']
session["role"] = request.form['role']
return redirect(url_for('dashboard.dashboard'))
return render_template('login-new.html', error=error)
@app.route('/logout')
def logout():
"""
View function for logout page.
Returns:
render a template.
"""
session.pop('logged_in', None)
session.pop('username', None)
return redirect(url_for('login'))
from model import *
def __verify_login(username, password, role):
u = User().query.filter_by(username=username).first()
if u and u.password == password and Role.query.filter_by(id=u.role_id).first().name == role:
return True
return False
# string = '{},{},{}'.format(username, password, role)
# userlist = open('db/userlist.csv').readlines()
# for line in userlist:
# if string in line:
# return True
# return False
@app.shell_context_processor
def make_shell_context():
"""
Import models into shell context so they can be available once the shell start.
Returns:
dictionary of database objects mappting.
"""
return {'db': db, 'User': User, 'Role': Role}
# start the server with the 'run()' method
if __name__ == '__main__':
# scheduler
scheduler = APScheduler()
scheduler.api_enabled = True
scheduler.init_app(app)
scheduler.start()
periodic_psi_check()
# turn off use reloader to prevent sending two duplicate emails
app.run(debug=True, use_reloader=False)