Make app instance accessible to blueprint

This commit is contained in:
messense 2017-02-22 20:57:29 +08:00
parent d1cc14201b
commit d388523dd5
No known key found for this signature in database
GPG Key ID: BB41A8A2C716CCA9
2 changed files with 17 additions and 0 deletions

View File

@ -18,6 +18,7 @@ class Blueprint:
:param name: unique name of the blueprint
:param url_prefix: URL to be prefixed before all route URLs
"""
self.app = None
self.name = name
self.url_prefix = url_prefix
self.host = host
@ -31,6 +32,7 @@ class Blueprint:
def register(self, app, options):
"""Register the blueprint to the sanic app."""
self.app = app
url_prefix = options.get('url_prefix', self.url_prefix)
# Routes

View File

@ -308,3 +308,18 @@ def test_bp_shorthand():
request, response = app.test_client.get('/delete')
assert response.status == 405
def test_bp_access_app_config():
app = Sanic('test_access_app_config')
app.config.TEXT = 'Hello'
bp = Blueprint('test_access_app_config')
@bp.route('/')
def handler(request):
return text(bp.app.config.TEXT)
app.blueprint(bp)
request, response = app.test_client.get('/')
assert response.text == 'Hello'