Nested blueprints

This allows blueprints to be nested within blueprints via a new
Blueprint.register_blueprint method. This should provide a use case
that has been desired for the past ~10 years.

This works by setting the endpoint name to be the blueprint names,
from parent to child delimeted by "." and then iterating over the
blueprint names in reverse order in the app (from most specific to
most general). This means that the expectation of nesting a blueprint
within a nested blueprint is met.
This commit is contained in:
pgjones 2021-02-24 21:18:12 +00:00 committed by David Lord
parent 85dce2c836
commit f92e820b4b
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
5 changed files with 154 additions and 56 deletions

View file

@ -850,3 +850,52 @@ def test_app_url_processors(app, client):
assert client.get("/de/").data == b"/de/about"
assert client.get("/de/about").data == b"/de/"
def test_nested_blueprint(app, client):
parent = flask.Blueprint("parent", __name__)
child = flask.Blueprint("child", __name__)
grandchild = flask.Blueprint("grandchild", __name__)
@parent.errorhandler(403)
def forbidden(e):
return "Parent no", 403
@parent.route("/")
def parent_index():
return "Parent yes"
@parent.route("/no")
def parent_no():
flask.abort(403)
@child.route("/")
def child_index():
return "Child yes"
@child.route("/no")
def child_no():
flask.abort(403)
@grandchild.errorhandler(403)
def grandchild_forbidden(e):
return "Grandchild no", 403
@grandchild.route("/")
def grandchild_index():
return "Grandchild yes"
@grandchild.route("/no")
def grandchild_no():
flask.abort(403)
child.register_blueprint(grandchild, url_prefix="/grandchild")
parent.register_blueprint(child, url_prefix="/child")
app.register_blueprint(parent, url_prefix="/parent")
assert client.get("/parent/").data == b"Parent yes"
assert client.get("/parent/child/").data == b"Child yes"
assert client.get("/parent/child/grandchild/").data == b"Grandchild yes"
assert client.get("/parent/no").data == b"Parent no"
assert client.get("/parent/child/no").data == b"Parent no"
assert client.get("/parent/child/grandchild/no").data == b"Grandchild no"