|
1 | 1 | # project/server/__init__.py |
2 | 2 |
|
3 | 3 |
|
4 | | -################# |
5 | | -#### imports #### |
6 | | -################# |
7 | | - |
8 | 4 | import os |
9 | 5 |
|
10 | 6 | from flask import Flask, render_template |
|
13 | 9 | from flask_debugtoolbar import DebugToolbarExtension |
14 | 10 | from flask_bootstrap import Bootstrap |
15 | 11 | from flask_sqlalchemy import SQLAlchemy |
| 12 | +from flask_migrate import Migrate |
16 | 13 |
|
17 | 14 |
|
18 | | -################ |
19 | | -#### config #### |
20 | | -################ |
21 | | - |
22 | | -app = Flask( |
23 | | - __name__, |
24 | | - template_folder='../client/templates', |
25 | | - static_folder='../client/static' |
26 | | -) |
27 | | - |
28 | | - |
29 | | -app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig') |
30 | | -app.config.from_object(app_settings) |
31 | | - |
32 | | - |
33 | | -#################### |
34 | | -#### extensions #### |
35 | | -#################### |
36 | | - |
| 15 | +# instantiate the extensions |
37 | 16 | login_manager = LoginManager() |
38 | | -login_manager.init_app(app) |
39 | | -bcrypt = Bcrypt(app) |
40 | | -toolbar = DebugToolbarExtension(app) |
41 | | -bootstrap = Bootstrap(app) |
42 | | -db = SQLAlchemy(app) |
43 | | - |
44 | | - |
45 | | -################### |
46 | | -### blueprints #### |
47 | | -################### |
48 | | - |
49 | | -from project.server.user.views import user_blueprint |
50 | | -from project.server.main.views import main_blueprint |
51 | | -app.register_blueprint(user_blueprint) |
52 | | -app.register_blueprint(main_blueprint) |
53 | | - |
54 | | - |
55 | | -################### |
56 | | -### flask-login #### |
57 | | -################### |
58 | | - |
59 | | -from project.server.models import User |
60 | | - |
61 | | -login_manager.login_view = "user.login" |
62 | | -login_manager.login_message_category = 'danger' |
63 | | - |
64 | | - |
65 | | -@login_manager.user_loader |
66 | | -def load_user(user_id): |
67 | | - return User.query.filter(User.id == int(user_id)).first() |
68 | | - |
69 | | - |
70 | | -######################## |
71 | | -#### error handlers #### |
72 | | -######################## |
73 | | - |
74 | | -@app.errorhandler(401) |
75 | | -def unauthorized_page(error): |
76 | | - return render_template("errors/401.html"), 401 |
77 | | - |
78 | | - |
79 | | -@app.errorhandler(403) |
80 | | -def forbidden_page(error): |
81 | | - return render_template("errors/403.html"), 403 |
82 | | - |
83 | | - |
84 | | -@app.errorhandler(404) |
85 | | -def page_not_found(error): |
86 | | - return render_template("errors/404.html"), 404 |
87 | | - |
88 | | - |
89 | | -@app.errorhandler(500) |
90 | | -def server_error_page(error): |
91 | | - return render_template("errors/500.html"), 500 |
| 17 | +bcrypt = Bcrypt() |
| 18 | +toolbar = DebugToolbarExtension() |
| 19 | +bootstrap = Bootstrap() |
| 20 | +db = SQLAlchemy() |
| 21 | +migrate = Migrate() |
| 22 | + |
| 23 | + |
| 24 | +def create_app(): |
| 25 | + |
| 26 | + # instantiate the app |
| 27 | + app = Flask( |
| 28 | + __name__, |
| 29 | + template_folder='../client/templates', |
| 30 | + static_folder='../client/static' |
| 31 | + ) |
| 32 | + |
| 33 | + # set config |
| 34 | + app_settings = os.getenv( |
| 35 | + 'APP_SETTINGS', 'project.server.config.DevelopmentConfig') |
| 36 | + app.config.from_object(app_settings) |
| 37 | + |
| 38 | + # set up extensions |
| 39 | + login_manager.init_app(app) |
| 40 | + bcrypt.init_app(app) |
| 41 | + toolbar.init_app(app) |
| 42 | + bootstrap.init_app(app) |
| 43 | + db.init_app(app) |
| 44 | + migrate.init_app(app, db) |
| 45 | + |
| 46 | + # register blueprints |
| 47 | + from project.server.user.views import user_blueprint |
| 48 | + from project.server.main.views import main_blueprint |
| 49 | + app.register_blueprint(user_blueprint) |
| 50 | + app.register_blueprint(main_blueprint) |
| 51 | + |
| 52 | + # flask login |
| 53 | + from project.server.models import User |
| 54 | + login_manager.login_view = 'user.login' |
| 55 | + login_manager.login_message_category = 'danger' |
| 56 | + |
| 57 | + @login_manager.user_loader |
| 58 | + def load_user(user_id): |
| 59 | + return User.query.filter(User.id == int(user_id)).first() |
| 60 | + |
| 61 | + return app |
| 62 | + |
| 63 | + # error handlers |
| 64 | + @app.errorhandler(401) |
| 65 | + def unauthorized_page(error): |
| 66 | + return render_template('errors/401.html'), 401 |
| 67 | + |
| 68 | + @app.errorhandler(403) |
| 69 | + def forbidden_page(error): |
| 70 | + return render_template('errors/403.html'), 403 |
| 71 | + |
| 72 | + @app.errorhandler(404) |
| 73 | + def page_not_found(error): |
| 74 | + return render_template('errors/404.html'), 404 |
| 75 | + |
| 76 | + @app.errorhandler(500) |
| 77 | + def server_error_page(error): |
| 78 | + return render_template('errors/500.html'), 500 |
| 79 | + |
| 80 | + return app |
0 commit comments