Skip to content
Myr

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

4 files+78 added−0 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+14 added
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.
87 unmodified lines
455456 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")460461 if bp_subdomain is None:462 bp_subdomain = blueprint.subdomain463464 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_prefix473474 if state.url_prefix is not None and bp_url_prefix is not None:
246 unmodified lines
modified:tests/test_blueprints.py+49 added
948 unmodified lines
949 response = client.get("/parent/child/")950 assert response.status_code == 200951952953def test_nesting_subdomains(app, client) -> None:954 subdomain = "api"955 parent = flask.Blueprint("parent", __name__)956 child = flask.Blueprint("child", __name__)957958 @child.route("/child/")959 def index():960 return "child"961962 parent.register_blueprint(child)963 app.register_blueprint(parent, subdomain=subdomain)964965 client.allow_subdomain_redirects = True966967 domain_name = "domain.tld"968 app.config["SERVER_NAME"] = domain_name969 response = client.get("/child/", base_url="http://api." + domain_name)970971 assert response.status_code == 200972973974def 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)979980 @child.route("/")981 def index():982 return "child"983984 parent.register_blueprint(child)985 app.register_blueprint(parent, subdomain=parent_subdomain)986987 client.allow_subdomain_redirects = True988989 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 )994995 assert response.status_code == 200996997 response = client.get("/", base_url=f"http://{parent_subdomain}.{domain_name}")998999 assert response.status_code == 404100010011002def test_unique_blueprint_names(app, client) -> None:1003 bp = flask.Blueprint("bp", __name__)1004 bp2 = flask.Blueprint("bp", __name__)1005
49 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