Merge pull request #4935 from pgjones/subdomain
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix subdomain inheritance for nested blueprints
authored by David Lord · committed by GitHub · parents fa1ee70, cabda59 · Browse files · 4 files·+78 added−0 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+78 added−0 removed
- tests:+49 added−0 removed
- other:+29 added−0 removed
Provenance
- Commit
836866d- Tree
e0621ec- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
fa1ee70- Parent 2
cabda59
4 files changed+78 added−0 removed
modified:docs/blueprints.rst+13 added
138 unmodified lines139 140 url_for('parent.child.create')141 /parent/child/create142 143+In addition a child blueprint's will gain their parent's subdomain,144+with their subdomain as prefix if present i.e.145+146+.. code-block:: python147+148+ parent = Blueprint('parent', __name__, subdomain='parent')149+ child = Blueprint('child', __name__, subdomain='child')150+ parent.register_blueprint(child)151+ app.register_blueprint(parent)152+153+ url_for('parent.child.create', _external=True)154+ "child.parent.domain.tld"155+156 Blueprint-specific before request functions, etc. registered with the157 parent will trigger for the child. If a child does not have an error158 handler that can handle a given exception, the parent's will be tried.159 156 unmodified linesmodified:src/flask/blueprints.py+14 added
356 unmodified lines357 with.358 :param options: Keyword arguments forwarded from359 :meth:`~Flask.register_blueprint`.360 361+ .. versionchanged:: 2.3362+ Nested blueprints now correctly apply subdomains.363+364 .. 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.87 unmodified lines455 456 for blueprint, bp_options in self._blueprints:457 bp_options = bp_options.copy()458 bp_url_prefix = bp_options.get("url_prefix")459+ bp_subdomain = bp_options.get("subdomain")460 461+ if bp_subdomain is None:462+ bp_subdomain = blueprint.subdomain463+464+ 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.subdomain470+471 if bp_url_prefix is None:472 bp_url_prefix = blueprint.url_prefix473 474 if state.url_prefix is not None and bp_url_prefix is not None:246 unmodified linesmodified:tests/test_blueprints.py+49 added
948 unmodified lines949 response = client.get("/parent/child/")950 assert response.status_code == 200951 952 953+def test_nesting_subdomains(app, client) -> None:954+ subdomain = "api"955+ parent = flask.Blueprint("parent", __name__)956+ child = flask.Blueprint("child", __name__)957+958+ @child.route("/child/")959+ def index():960+ return "child"961+962+ parent.register_blueprint(child)963+ app.register_blueprint(parent, subdomain=subdomain)964+965+ client.allow_subdomain_redirects = True966+967+ domain_name = "domain.tld"968+ app.config["SERVER_NAME"] = domain_name969+ response = client.get("/child/", base_url="http://api." + domain_name)970+971+ assert response.status_code == 200972+973+974+def 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)979+980+ @child.route("/")981+ def index():982+ return "child"983+984+ parent.register_blueprint(child)985+ app.register_blueprint(parent, subdomain=parent_subdomain)986+987+ client.allow_subdomain_redirects = True988+989+ domain_name = "domain.tld"990+ app.config["SERVER_NAME"] = domain_name991+ response = client.get(992+ "/", base_url=f"http://{child_subdomain}.{parent_subdomain}.{domain_name}"993+ )994+995+ assert response.status_code == 200996+997+ response = client.get("/", base_url=f"http://{parent_subdomain}.{domain_name}")998+999+ assert response.status_code == 4041000+1001+1002 def test_unique_blueprint_names(app, client) -> None:1003 bp = flask.Blueprint("bp", __name__)1004 bp2 = flask.Blueprint("bp", __name__)1005 49 unmodified linesmodified:CHANGES.rst+2 added
1 unmodified line2 -------------3 4 Unreleased5 6+- Ensure subdomains are applied with nested blueprints. :issue:`4834`7 8+9 Version 2.2.310 -------------11 12 Unreleased1415 unmodified lines