Skip to content

Commit e9164cd

Browse files
Moritz WolfMoritz Wolf
Moritz Wolf
authored and
Moritz Wolf
committed
refactoring to use flasks app factory
routes are now a blueprint and registered separately in create_app this will allow pytest to run properly
1 parent 7f96b5e commit e9164cd

16 files changed

+302
-280
lines changed

app/__init__.py

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
11
from flask import Flask
2+
from setuptools import setup
23
from app.db import get_db, init_app
34
from flask_login import LoginManager
45
import os
56
import logging
7+
from flask.logging import create_logger
68
from flask_cors import CORS
9+
from app.user_handler import User, load_user
10+
from app.error_handler import handle_database_error, handle_unknown_error, UnknownError, DatabaseError, page_not_found
711

8-
app = Flask(__name__)
9-
CORS(app)
10-
logging.basicConfig(level=logging.DEBUG)
11-
app.config.from_mapping(
12-
SECRET_KEY='dev',
13-
DATABASE=os.path.join(app.instance_path, 'database.sqlite'),
14-
)
15-
init_app(app)
1612

17-
login_manager = LoginManager(app)
18-
login_manager.init_app(app)
19-
login_manager.login_view = 'login'
13+
def create_app(test_config=None, debug=True):
14+
print('went into __init__')
15+
app = Flask(__name__)
16+
CORS(app)
17+
app.config.from_mapping(
18+
SECRET_KEY='dev',
19+
DATABASE=os.path.join(app.instance_path, 'database.sqlite'),
20+
DEBUG=debug,
21+
TESTING=debug
22+
)
23+
if test_config is None:
24+
# load the instance config, if it exists, when not testing
25+
app.config.from_pyfile("config.py", silent=True)
26+
else:
27+
# load the test config if passed in
28+
app.config.update(test_config)
2029

21-
from app import routes
30+
print(app.debug)
31+
init_app(app)
32+
33+
login_manager = LoginManager(app)
34+
login_manager.init_app(app)
35+
login_manager.login_view = 'routes.login'
36+
37+
login_manager.user_loader(load_user)
38+
# logger
39+
# when running via flask run:
40+
# logging.basicConfig(level=logging.DEBUG)
41+
42+
# app.logger = create_logger(app)
43+
# when running gunicorn
44+
gunicorn_logger = logging.getLogger('gunicorn.error')
45+
app.logger.handlers.extend(gunicorn_logger.handlers)
46+
app.logger = gunicorn_logger
47+
# transitions logger
48+
logging.getLogger('transitions').setLevel(logging.ERROR)
49+
50+
from app import routes
51+
app.register_blueprint(routes.app)
52+
53+
app.register_error_handler(DatabaseError, handle_database_error)
54+
app.register_error_handler(UnknownError, handle_unknown_error)
55+
app.register_error_handler(404, page_not_found)
56+
57+
return app

app/automaton.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ def print_debug(self, **kwargs): print(self.current_state)
159159

160160

161161
@staticmethod
162-
def setup():
162+
def setup(protocol:str='protocol.yml'):
163163
# TODO: Check Correctness
164164
# needs: Transition to end, no unreachable nodes, no unknown task types
165165
# Build database
166166
# Handle api calls and predictions
167167

168168
automaton = AnnotationAutomaton()
169169

170-
with open('protocol.yml') as f:
170+
with open(protocol) as f:
171171
yaml = YAML(typ='safe')
172172
protocol: dict = next(yaml.load_all(f))
173173

app/db.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def init_db():
4343

4444
with current_app.open_resource('schema.sql') as f:
4545
db.executescript(f.read().decode('utf8'))
46-
columns_from_automaton()
4746

4847
def columns_from_automaton():
4948
from app.automaton import AnnotationAutomaton
@@ -89,6 +88,8 @@ def init_db_command():
8988
break
9089
save_db('all')
9190
init_db()
91+
columns_from_automaton()
92+
9293
click.echo('Initialized the database.')
9394

9495
@click.command('db-from-csv')

app/error_handler.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from flask import render_template
2-
from app import app
2+
# from app import app
3+
# from flask import current_app as app
4+
35
from flask_login import current_user
46

57
class DatabaseError(Exception):
@@ -20,8 +22,8 @@ def __init__(self, message, status_code=None, payload=None):
2022
self.status_code = status_code
2123
self.payload = payload
2224

23-
@app.errorhandler(UnknownError)
24-
def unknown_error(self):
25+
# @app.errorhandler(UnknownError)
26+
def handle_unknown_error(self):
2527
error_tile = "Unknown Error Occurred."
2628
error_message = self.message
2729
error_info = None
@@ -31,8 +33,8 @@ def unknown_error(self):
3133
return render_template('error.html', error_tile = error_tile, error_message = error_message, error_info = error_info)
3234

3335

34-
@app.errorhandler(DatabaseError)
35-
def handle_database_error(error):
36+
# @app.errorhandler(DatabaseError)
37+
def handle_database_error(self):
3638
error_tile = "Database error occurred."
3739
error_message = self.message
3840
error_info = None
@@ -42,7 +44,7 @@ def handle_database_error(error):
4244
return render_template('error.html', error_tile = error_tile, error_message = error_message, error_info = error_info)
4345

4446
## Error 404 page not found
45-
@app.errorhandler(404)
47+
# @app.errorhandler(404)
4648
def page_not_found(internal_error):
4749
error_tile = "Page not found Error."
4850
error_message = "The page you are trying to access cannot not be found or does not exist."

app/login_handler.py

-101
This file was deleted.

0 commit comments

Comments
 (0)