diff --git a/pytest_bdd_example/auth/blueprint.py b/pytest_bdd_example/auth/blueprint.py index 7e2b144..4bd4c43 100644 --- a/pytest_bdd_example/auth/blueprint.py +++ b/pytest_bdd_example/auth/blueprint.py @@ -1,14 +1,11 @@ from flask import Blueprint from .manager import login_manager, check_valid_login -from .models import db auth = Blueprint('auth', __name__, template_folder='../') @auth.record_once def on_registered(state): - db.init_app(state.app) - login_manager.init_app(state.app) state.app.before_request(check_valid_login) diff --git a/pytest_bdd_example/auth/models.py b/pytest_bdd_example/auth/models.py index fd1f53e..5a987e7 100644 --- a/pytest_bdd_example/auth/models.py +++ b/pytest_bdd_example/auth/models.py @@ -1,8 +1,9 @@ +from flask import current_app + from flask.ext.security import UserMixin -from flask.ext.sqlalchemy import SQLAlchemy -db = SQLAlchemy() +db = current_app.config['db'] ROLE_USER = 0 ROLE_ADMIN = 1 diff --git a/pytest_bdd_example/book/__init__.py b/pytest_bdd_example/book/__init__.py index 84f0c6f..19de369 100644 --- a/pytest_bdd_example/book/__init__.py +++ b/pytest_bdd_example/book/__init__.py @@ -1,5 +1,5 @@ -from .models import db, Author, Book +from .models import Author, Book from .blueprint import book -__all__ = ['book', 'db', 'Author', 'Book'] +__all__ = ['book', 'Author', 'Book'] diff --git a/pytest_bdd_example/book/admin.py b/pytest_bdd_example/book/admin.py index ea78f29..10c71be 100644 --- a/pytest_bdd_example/book/admin.py +++ b/pytest_bdd_example/book/admin.py @@ -1,6 +1,10 @@ +from flask import current_app + from flask.ext.admin import Admin from flask.ext.admin.contrib.sqlamodel import ModelView -from pytest_bdd_example.book import Book, db +from pytest_bdd_example.book import Book + +db = current_app.config['db'] admin = Admin() -admin.add_view(ModelView(Book, db.Session(), 'books', endpoint='books')) \ No newline at end of file +admin.add_view(ModelView(Book, db.session, 'books', endpoint='books')) diff --git a/pytest_bdd_example/book/blueprint.py b/pytest_bdd_example/book/blueprint.py index a5dbb14..5826e4b 100644 --- a/pytest_bdd_example/book/blueprint.py +++ b/pytest_bdd_example/book/blueprint.py @@ -1,12 +1,10 @@ from flask import Blueprint -from .models import db from .admin import admin -book = Blueprint('book', __name__, template_folder='../') +book = Blueprint('book', __name__) @book.record_once def on_registered(state): - db.init_app(state.app) admin.init_app(state.app) diff --git a/pytest_bdd_example/book/models.py b/pytest_bdd_example/book/models.py index f6b5a0e..325c3bd 100644 --- a/pytest_bdd_example/book/models.py +++ b/pytest_bdd_example/book/models.py @@ -1,6 +1,6 @@ -from flask.ext.sqlalchemy import SQLAlchemy +from flask import current_app -db = SQLAlchemy() +db = current_app.config['db'] class Author(db.Model): diff --git a/pytest_bdd_example/dashboard/__init__.py b/pytest_bdd_example/dashboard/__init__.py index c8407a1..11bec64 100644 --- a/pytest_bdd_example/dashboard/__init__.py +++ b/pytest_bdd_example/dashboard/__init__.py @@ -1,4 +1,4 @@ -from .app import app, db +from .app import app -__all__ = ['app', 'db'] +__all__ = ['app'] diff --git a/pytest_bdd_example/dashboard/app.py b/pytest_bdd_example/dashboard/app.py index 2c92704..e9c13bd 100644 --- a/pytest_bdd_example/dashboard/app.py +++ b/pytest_bdd_example/dashboard/app.py @@ -1,11 +1,8 @@ from flask import Flask + from flask.ext.sqlalchemy import SQLAlchemy - from pytest_bdd_example.dashboard import settings -from pytest_bdd_example.auth import auth -from pytest_bdd_example.book import book - app = Flask( __name__, @@ -13,13 +10,16 @@ app = Flask( template_folder=settings.TEMPLATES_ROOT, ) - -db = SQLAlchemy(app) - app.config.from_object('pytest_bdd_example.dashboard.settings') app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' -app.register_blueprint(auth, url_prefix='/auth') -app.register_blueprint(book, url_prefix='/book') +db = SQLAlchemy(app) +app.config['db'] = db +with app.app_context(): + from pytest_bdd_example.auth import auth + from pytest_bdd_example.book import book + + app.register_blueprint(auth, url_prefix='/auth') + app.register_blueprint(book) diff --git a/pytest_bdd_example/manage.py b/pytest_bdd_example/manage.py index 528d236..5e4df2f 100755 --- a/pytest_bdd_example/manage.py +++ b/pytest_bdd_example/manage.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from flask.ext.script import Manager, Shell, Server, Command +from flask.ext.script import Manager, Shell, Server from pytest_bdd_example.dashboard import app as dashboard_app from pytest_bdd_example.shop import app as shop_app @@ -15,7 +15,7 @@ def create_app(app=None): return APPS[app] manager = Manager(create_app) -manager.add_option("-app", "--application", dest="app", required=True) -manager.add_command("runserver", Server("127.0.0.1")) -manager.add_command("shell", Shell(use_ipython=True)) -manager.run() \ No newline at end of file +manager.add_option('-app', '--application', dest='app', required=True) +manager.add_command('runserver', Server('127.0.0.1')) +manager.add_command('shell', Shell(use_ipython=True)) +manager.run()