1

I am having issues trying to make things work with the application factory pattern. I had a working Flask application with a bad code structure. Today I improved the structure and created a more suitable application factory.

In order to access users identity, I used to following before_request which was working fine.

# user_module/authorization.py

@current_app.before_request
def before_request():
    if hasattr(current_user, 'id'):
        g.identity = Identity(current_user.id)
        load_identity()
    else:
        g.identity = AnonymousIdentity

However, I moved my base application to

# application.py

def config_extensions(application):
    mysql.init_app(application)
    login_manager.init_app(application)
    principal.init_app(application)

def config_blueprints(application):
    from w_app.user_module.views import user_blueprint
    application.register_blueprint(user_blueprint)

    from w_app.admin_module.views import admin_blueprint
    application.register_blueprint(admin_blueprint)

    from w_app.main_module.views import main_blueprint
    application.register_blueprint(main_blueprint)

# Define create_app function, the core of the Flask App
def create_app():
    app = Flask(__name__)
    app.config.from_pyfile('config.py')
    app.secret_key = 'cvwenjvibvuiu934vh843vn839f234nc893cu9234'

    # with app.app_context():
    # Add a debug toolbar to all responses in dev
    app.config['SECRET_KEY'] = 'cvwenjvibvuiu934vh843vn839f234nc893cu9234'
    DebugToolbarExtension(app)

    config_extensions(app)
    config_blueprints(app)

    return app

And now the authorization tells me it can not find the Application Context.

RuntimeError: Working outside the application context

I've tried to move it arround and also tried to use with app.app_context without success.

davidism
  • 121,510
  • 29
  • 395
  • 339
nate-k
  • 191
  • 2
  • 10

0 Answers0