fix url_prefix argument when nesting blueprints
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Grey Li · committed by David Lord · parent 08e459e · 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
Provenance
- Commit
e93704f- Tree
0dc9dc4- Author
Grey Li <withlihui@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
08e459e
3 files changed+45 added−6 removed
modified:src/flask/blueprints.py+10 added-6 removed
258 unmodified lines259 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.264 265 :param app: The application this blueprint is being registered266 with.267 :param options: Keyword arguments forwarded from75 unmodified lines343 self.cli.name = cli_resolved_group344 app.cli.add_command(self.cli)345 346 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")349+350+ if bp_url_prefix is None:351+ bp_url_prefix = blueprint.url_prefix352+353+ 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 )357 353- bp_options["url_prefix"] = url_prefix358 bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "."359 blueprint.register(app, bp_options)360 361 def add_url_rule(189 unmodified linesmodified:tests/test_blueprints.py+33 added
833 unmodified lines834 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"838+839+840+def 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")845+846+ @parent.route("/")847+ def parent_index():848+ return "Parent"849+850+ @child.route("/")851+ def child_index():852+ return "Child"853+854+ @grandchild.route("/")855+ def grandchild_index():856+ return "Grandchild"857+858+ @apple.route("/")859+ def apple_index():860+ return "Apple"861+862+ child.register_blueprint(grandchild)863+ child.register_blueprint(apple, url_prefix="/orange") # test overwrite864+ parent.register_blueprint(child)865+ app.register_blueprint(parent)866+867+ 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 lines17 ``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`23 24 25 Version 2.0.026 -------------1096 unmodified lines