Skip to content
Myr

Merge pull request #4036 from greyli/fix-nesting-bp

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

Fix url_prefix argument for nesting blueprint

authored by David Lord · committed by GitHub · parents 08e459e, e93704f · Browse files · 3 files+45 added−6 removed
This commit

State

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

Diff stats

3 files+45 added−6 removed

  • tests:+33 added−0 removed
  • other:+12 added−6 removed

3 files+45 added−6 removed

modified:src/flask/blueprints.py+10 added-6 removed
258 unmodified lines
259 def register(self, app: "Flask", options: dict) -> None:260 """Called by :meth:`Flask.register_blueprint` to register all261 views and callbacks registered on the blueprint with the262 application. Creates a :class:`.BlueprintSetupState` and calls263 each :meth:`record` callbackwith it.263 each :meth:`record` callback with it.264265 :param app: The application this blueprint is being registered266 with.267 :param options: Keyword arguments forwarded from
75 unmodified lines
343 self.cli.name = cli_resolved_group344 app.cli.add_command(self.cli)345346 for blueprint, bp_options in self._blueprints:347 url_prefix = options.get("url_prefix", "")348 if "url_prefix" in bp_options:349 url_prefix = (350 url_prefix.rstrip("/") + "/" + bp_options["url_prefix"].lstrip("/")347 bp_options = bp_options.copy()348 bp_url_prefix = bp_options.get("url_prefix")349350 if bp_url_prefix is None:351 bp_url_prefix = blueprint.url_prefix352353 if state.url_prefix is not None and bp_url_prefix is not None:354 bp_options["url_prefix"] = (355 state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/")356 )357353 bp_options["url_prefix"] = url_prefix358 bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "."359 blueprint.register(app, bp_options)360361 def add_url_rule(
189 unmodified lines
modified:tests/test_blueprints.py+33 added
833 unmodified lines
834 assert client.get("/parent/child/grandchild/").data == b"Grandchild yes"835 assert client.get("/parent/no").data == b"Parent no"836 assert client.get("/parent/child/no").data == b"Parent no"837 assert client.get("/parent/child/grandchild/no").data == b"Grandchild no"838839840def test_nested_blueprint_url_prefix(app, client):841 parent = flask.Blueprint("parent", __name__, url_prefix="/parent")842 child = flask.Blueprint("child", __name__, url_prefix="/child")843 grandchild = flask.Blueprint("grandchild", __name__, url_prefix="/grandchild")844 apple = flask.Blueprint("apple", __name__, url_prefix="/apple")845846 @parent.route("/")847 def parent_index():848 return "Parent"849850 @child.route("/")851 def child_index():852 return "Child"853854 @grandchild.route("/")855 def grandchild_index():856 return "Grandchild"857858 @apple.route("/")859 def apple_index():860 return "Apple"861862 child.register_blueprint(grandchild)863 child.register_blueprint(apple, url_prefix="/orange") # test overwrite864 parent.register_blueprint(child)865 app.register_blueprint(parent)866867 assert client.get("/parent/").data == b"Parent"868 assert client.get("/parent/child/").data == b"Child"869 assert client.get("/parent/child/grandchild/").data == b"Grandchild"870 assert client.get("/parent/child/orange/").data == b"Apple"
modified:CHANGES.rst+2 added
16 unmodified lines
17 ``get_send_file_max_age``. :issue:`4044`, :pr:`4026`18- Show an error when a blueprint name contains a dot. The ``.`` has19 special meaning, it is used to separate (nested) blueprint names and20 the endpoint name. :issue:`4041`21- Combine URL prefixes when nesting blueprints that were created with22 a ``url_prefix`` value. :issue:`4037`232425Version 2.0.026-------------
1096 unmodified lines