Skip to content
Myr

Ensure that blueprint subdomains suffix-chain

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

This ensures that a child's subdomain prefixs any parent subdomain such that the full domain is child.parent.domain.tld and onwards with further nesting. This makes the most sense to users and mimics how url_prefixes work (although subdomains suffix).

authored by pgjones · committed by pgjones · parent d7b6c1f · Browse files · 4 files+26 added−4 removed
This commit

State

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

Diff stats

4 files+26 added−4 removed

  • tests:+4 added−2 removed
  • other:+22 added−2 removed

4 files+26 added−4 removed

modified:docs/blueprints.rst+13 added
138 unmodified lines
139140 url_for('parent.child.create')141 /parent/child/create142143In addition a child blueprint's will gain their parent's subdomain,144with their subdomain as prefix if present i.e.145146.. code-block:: python147148 parent = Blueprint('parent', __name__, subdomain='parent')149 child = Blueprint('child', __name__, subdomain='child')150 parent.register_blueprint(child)151 app.register_blueprint(parent)152153 url_for('parent.child.create', _external=True)154 "child.parent.domain.tld"155156Blueprint-specific before request functions, etc. registered with the157parent will trigger for the child. If a child does not have an error158handler that can handle a given exception, the parent's will be tried.159
156 unmodified lines
modified:src/flask/blueprints.py+7 added-2 removed
356 unmodified lines
357 with.358 :param options: Keyword arguments forwarded from359 :meth:`~Flask.register_blueprint`.360361 .. versionchanged:: 2.3362 Nested blueprints now correctly apply subdomains.363364 .. versionchanged:: 2.0.1365 Nested blueprints are registered with their dotted name.366 This allows different blueprints with the same name to be367 nested at different locations.
92 unmodified lines
460461 if bp_subdomain is None:462 bp_subdomain = blueprint.subdomain463461 if state.subdomain is not None and bp_subdomain is None:462 bp_options["subdomain"] = state.subdomain464 if state.subdomain is not None and bp_subdomain is not None:465 bp_options["subdomain"] = bp_subdomain + "." + state.subdomain466 elif bp_subdomain is not None:467 bp_options["subdomain"] = bp_subdomain468 elif state.subdomain is not None:469 bp_options["subdomain"] = state.subdomain470471 if bp_url_prefix is None:472 bp_url_prefix = blueprint.url_prefix473
247 unmodified lines
modified:tests/test_blueprints.py+4 added-2 removed
969 unmodified lines
970971 assert response.status_code == 200972973974def test_child_overrides_parent_subdomain(app, client) -> None:974def test_child_and_parent_subdomain(app, client) -> None:975 child_subdomain = "api"976 parent_subdomain = "parent"977 parent = flask.Blueprint("parent", __name__)978 child = flask.Blueprint("child", __name__, subdomain=child_subdomain)
8 unmodified lines
987 client.allow_subdomain_redirects = True988989 domain_name = "domain.tld"990 app.config["SERVER_NAME"] = domain_name991 response = client.get("/", base_url=f"http://{child_subdomain}.{domain_name}")991 response = client.get(992 "/", base_url=f"http://{child_subdomain}.{parent_subdomain}.{domain_name}"993 )994995 assert response.status_code == 200996997 response = client.get("/", base_url=f"http://{parent_subdomain}.{domain_name}")
57 unmodified lines
modified:CHANGES.rst+2 added
1 unmodified line
2-------------34Unreleased56- Ensure subdomains are applied with nested blueprints. :issue:`4834`789Version 2.2.310-------------1112Unreleased
1415 unmodified lines