-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
134 lines (95 loc) · 3.44 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import timedelta
from functools import wraps
from flask import Flask, render_template, session
from flask_login import current_user, LoginManager
from flask_sqlalchemy import SQLAlchemy
import logging
db = SQLAlchemy()
# app configuration
app = Flask(__name__, static_url_path='/static', static_folder='static')
# if app.config['ENV'] == 'development':
# app.config.from_object('config.ProductionConfig')
# else:
app.config.from_object('config.DevelopmentConfig')
# logging
def setup_logging(filename):
""" Function to setup a logging file for important actions using logging module."""
fh = logging.FileHandler(filename, "w")
fh.setLevel(logging.WARNING)
formatter = logging.Formatter('%(asctime)s : %(message)s', '%m/%d/%Y %I:%m:%S %p')
fh.setFormatter(formatter)
logger = logging.getLogger('')
logger.propagate = False
logger.addHandler(fh)
def setup_login():
""" Function to initialize the flask-login LoginManager and attributes"""
# Flask LoginManager instance attributes
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.refresh_view = 'auth.login'
login_manager.login_message_category = 'danger'
login_manager.login_message = 'Session timed out, please login again.'
login_manager.init_app(app)
from models import User
db.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.before_first_request
def execute_this():
setup_logging('security.log')
setup_login()
# import blueprints
from auth.views import auth
from users.views import users
from groups.views import groups
from webadmin.views import webadmin_blueprint
# register blueprints
app.register_blueprint(auth)
app.register_blueprint(users)
app.register_blueprint(groups)
app.register_blueprint(webadmin_blueprint)
@app.before_request
def before_request():
""" A function to set the permanent session lifetime to 30 minutes before every request
"""
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=30)
session.modified = True
@app.route('/')
def index():
return render_template("index.html")
# error pages to render upon error handling
@app.errorhandler(400)
def bad_request(_error):
return render_template('errors/400.html'), 400
@app.errorhandler(403)
def page_forbidden(_error):
return render_template('errors/403.html'), 403
@app.errorhandler(404)
def page_not_found(_error):
return render_template('errors/404.html'), 404
@app.errorhandler(410)
def page_forbidden(_error):
return render_template('errors/410.html'), 410
@app.errorhandler(500)
def internal_error(_error):
return render_template('errors/500.html'), 500
@app.errorhandler(503)
def page_forbidden(_error):
return render_template('errors/503.html'), 503
def requires_roles(*roles):
""" Function using wraps annotation to check if the current_user has the role required."""
def wrapper(f):
@wraps(f)
def wrapped(*args, **kwargs):
if current_user.role not in roles:
# Redirect the user to an unauthorised notice!
return render_template('errors/403.html')
return f(*args, **kwargs)
return wrapped
return wrapper
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port='5000')