From ae5bf35d70c78aad0f455b79ea11dfe8af10bf74 Mon Sep 17 00:00:00 2001 From: Burathar Date: Thu, 16 Jul 2020 01:29:59 +0200 Subject: [PATCH] Redefine app as factory function --- app/__init__.py | 37 +++++++++++++++++++++++++------------ tests/test_routes.py | 2 +- the_hunt.py | 10 ++++++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c7b97a1..b65b6d7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,8 @@ -from config import Config +import os import logging + from logging.handlers import SMTPHandler from logging.handlers import RotatingFileHandler -import os from flask import Flask from flask_bootstrap import Bootstrap @@ -11,19 +11,28 @@ from flask_migrate import Migrate from flask_login import LoginManager from flask_moment import Moment +from config import Config -app = Flask(__name__) -app.config.from_object(Config) -bootstrap = Bootstrap(app) -db = SQLAlchemy(app) -migrate = Migrate(app, db) -login = LoginManager(app) +db = SQLAlchemy() +bootstrap = Bootstrap() +migrate = Migrate() +login = LoginManager() login.login_view = 'login' -moment = Moment(app) +login.login_message = 'Please log in to access this page.' +moment = Moment() -from app import routes, models, errors +def create_app(config_class=Config): + # pylint: disable=no-member + app = Flask(__name__) + app.config.from_object(config_class) + + db.init_app(app) + bootstrap.init_app(app) + migrate.init_app(app, db) + login.init_app(app) + moment.init_app(app) -if not app.debug and not app.testing: + if not app.debug and not app.testing: if app.config['MAIL_SERVER']: auth = None if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']: @@ -56,4 +65,8 @@ if not app.debug and not app.testing: app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) - app.logger.info('Microblog startup') \ No newline at end of file + app.logger.info('Microblog startup') + + return app + +from app import routes, models, errors diff --git a/tests/test_routes.py b/tests/test_routes.py index a498db9..e73f27a 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -27,7 +27,7 @@ class RoutesCase(unittest.TestCase): g.players.append(p2) db.session.append(g) db.session.commit() - self.assertEqual + self.assertEqual(True, True) self.assertFalse('Foo'.isupper()) if __name__ == '__main__': diff --git a/the_hunt.py b/the_hunt.py index 7fd9fdc..52ed5d6 100644 --- a/the_hunt.py +++ b/the_hunt.py @@ -1,10 +1,12 @@ -from app import app, db +from app import create_app, db from app.models import Game, Player, Objective, Location, Notification, GamePlayer, \ PlayerFoundObjective, NotificationPlayer, PlayerCaughtPlayer, Role, GameState +app = create_app() + @app.shell_context_processor def make_shell_context(): return {'db': db, 'Game' : Game, 'Player' : Player, 'Objective' : Objective, - 'Location' : Location, 'Notification' : Notification, 'GamePlayer' : GamePlayer, - 'PlayerFoundObjective' : PlayerFoundObjective, 'NotificationPlayer' : NotificationPlayer, - 'PlayerCaughtPlayer' : PlayerCaughtPlayer, 'Role' : Role, 'GameState' : GameState} + 'Location' : Location, 'Notification' : Notification, 'GamePlayer' : GamePlayer, + 'PlayerFoundObjective' : PlayerFoundObjective, 'NotificationPlayer' : NotificationPlayer, + 'PlayerCaughtPlayer' : PlayerCaughtPlayer, 'Role' : Role, 'GameState' : GameState}