-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_app.py
67 lines (53 loc) · 1.76 KB
/
create_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
from flask import Flask, render_template, g, request, redirect, abort
from social_flask.routes import social_auth
from flask_login import current_user, LoginManager, login_required
from flask_migrate import Migrate
from serializers import ma
from social_flask_sqlalchemy.models import init_social
from api import api
from models import db, User
def create_app(config='Config'):
app = Flask(__name__)
app.config.from_object('config.' + config)
db.init_app(app)
Migrate(app, db)
if config != 'TestConfig':
init_social(app, db.session)
lm = LoginManager(app)
lm.login_view = 'login'
ma.init_app(app)
@lm.user_loader
def load_user(user_id):
return User.query.get(user_id)
@app.before_request
def force_ssl():
if not app.config['SKIP_SSL'] and request.url.startswith('http://'):
new = request.url.replace('http://', 'https://', 1)
return redirect(new, code=301)
@app.before_request
def global_user():
g.user = current_user._get_current_object()
@app.context_processor
def inject_user():
try:
return {'user': g.user}
except AttributeError:
return {'user': None}
app.register_blueprint(social_auth)
app.register_blueprint(api, url_prefix='/api')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/admin/')
@app.route('/admin/<path:path>')
@login_required
def admin(path=None):
if not g.user.email == app.config['ADMIN_USER']:
raise abort(403)
return render_template('admin.html')
@app.route('/')
@app.route('/<path:path>')
@login_required
def index(path=None):
return render_template('app.html')
return app