How to access app.config in a blueprint?

前端 未结 7 967
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 23:07

I am trying to access access application configuration inside a blueprint authorisation.py which in a package api. I am initializing the blueprint in __in

7条回答
  •  爱一瞬间的悲伤
    2020-12-12 23:54

    To build on tbicr's answer, here's an example overriding the register method example:

    from flask import Blueprint
    
    auth = None
    
    class RegisteringExampleBlueprint(Blueprint):
        def register(self, app, options, first_registration=False):
            global auth
    
            config = app.config
            client_id = config.get('CLIENT_ID')
            client_secret = config.get('CLIENT_SECRET')
            scope = config.get('SCOPE')
            callback = config.get('CALLBACK')
    
            auth = OauthAdapter(client_id, client_secret, scope, callback)
    
            super(RegisteringExampleBlueprint,
                  self).register(app, options, first_registration)
    
    the_blueprint = RegisteringExampleBlueprint('example', __name__)
    

    And an example using the record decorator:

    from flask import Blueprint
    from api import api_blueprint as api
    
    auth = None
    
    # Note there's also a record_once decorator
    @api.record
    def record_auth(setup_state):
        global auth
    
        config = setup_state.app.config
        client_id = config.get('CLIENT_ID')
        client_secret = config.get('CLIENT_SECRET')
        scope = config.get('SCOPE')
        callback = config.get('CALLBACK')
    
        auth = OauthAdapter(client_id, client_secret, scope, callback)
    

提交回复
热议问题