Skip to content
Myr

Fix blueprint renaming

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This ensures that if a blueprint is renamed at the time of registration that name is used when constructing endpoints, as expected.

authored by pgjones · committed by pgjones · parent 714b0a4 · Browse files · 2 files+25 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−1 removed

  • tests:+22 added−0 removed
  • other:+3 added−1 removed

2 files+25 added−1 removed

modified:src/flask/blueprints.py+3 added-1 removed
65 unmodified lines
66 #: The prefix that should be used for all URLs defined on the67 #: blueprint.68 self.url_prefix = url_prefix6970 self.name = self.options.get("name", blueprint.name)71 self.name_prefix = self.options.get("name_prefix", "")7273 #: A dictionary with URL defaults that is added to each and every74 #: URL that was defined with the blueprint.
21 unmodified lines
96 endpoint = _endpoint_from_view_func(view_func) # type: ignore97 defaults = self.url_defaults98 if "defaults" in options:99 defaults = dict(defaults, **options.pop("defaults"))100101 self.app.add_url_rule(102 rule,101 f"{self.name_prefix}.{self.blueprint.name}.{endpoint}".lstrip("."),103 f"{self.name_prefix}.{self.name}.{endpoint}".lstrip("."),104 view_func,105 defaults=defaults,106 **options,107 )
496 unmodified lines
modified:tests/test_blueprints.py+22 added
887 unmodified lines
888def test_self_registration(app, client) -> None:889 bp = flask.Blueprint("bp", __name__)890 with pytest.raises(ValueError):891 bp.register_blueprint(bp)892893894def test_blueprint_renaming(app, client) -> None:895 bp = flask.Blueprint("bp", __name__)896 bp2 = flask.Blueprint("bp2", __name__)897898 @bp.get("/")899 def index():900 return flask.request.endpoint901902 @bp2.get("/")903 def index2():904 return flask.request.endpoint905906 bp.register_blueprint(bp2, url_prefix="/a", name="sub")907 app.register_blueprint(bp, url_prefix="/a")908 app.register_blueprint(bp, url_prefix="/b", name="alt")909910 assert client.get("/a/").data == b"bp.index"911 assert client.get("/b/").data == b"alt.index"912 assert client.get("/a/a/").data == b"bp.sub.index2"913 assert client.get("/b/a/").data == b"alt.sub.index2"