Merge pull request #4045 from pallets/blueprint-name-dot
Before adoption: this commit came in with the history the project adopted, so no landing records it.
blueprint name may not contain a dot
authored by David Lord · committed by GitHub · parents d8c37f4, 7c52614 · Browse files · 4 files·+28 added−85 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+28 added−85 removed
- tests:+15 added−79 removed
- other:+13 added−6 removed
Provenance
- Commit
08e459e- Tree
653bfa0- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d8c37f4- Parent 2
7c52614
4 files changed+28 added−85 removed
modified:src/flask/blueprints.py+10 added-6 removed
186 unmodified lines187 static_url_path=static_url_path,188 template_folder=template_folder,189 root_path=root_path,190 )191+192+ if "." in name:193+ raise ValueError("'name' may not contain a dot '.' character.")194+195 self.name = name196 self.url_prefix = url_prefix197 self.subdomain = subdomain198 self.deferred_functions: t.List[DeferredSetupFunction] = []164 unmodified lines363 ) -> None:364 """Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for365 the :func:`url_for` function is prefixed with the name of the blueprint.366 """363- if endpoint:364- assert "." not in endpoint, "Blueprint endpoints should not contain dots"365- if view_func and hasattr(view_func, "__name__"):366- assert (367- "." not in view_func.__name__368- ), "Blueprint view function name should not contain dots"367+ if endpoint and "." in endpoint:368+ raise ValueError("'endpoint' may not contain a dot '.' character.")369+370+ if view_func and hasattr(view_func, "__name__") and "." in view_func.__name__:371+ raise ValueError("'view_func' name may not contain a dot '.' character.")372+373 self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))374 375 def app_template_filter(self, name: t.Optional[str] = None) -> t.Callable:376 """Register a custom template filter, available application wide. Like170 unmodified linesmodified:tests/test_basic.py+3 added-3 removed
1629 unmodified lines1630 assert client.get("/foo").data == b"/en/about"1631 1632 1633 def test_inject_blueprint_url_defaults(app):1634- bp = flask.Blueprint("foo.bar.baz", __name__, template_folder="template")1634+ bp = flask.Blueprint("foo", __name__, template_folder="template")1635 1636 @bp.url_defaults1637 def bp_defaults(endpoint, values):1638 values["page"] = "login"4 unmodified lines1643 1644 app.register_blueprint(bp)1645 1646 values = dict()1647- app.inject_url_defaults("foo.bar.baz.view", values)1647+ app.inject_url_defaults("foo.view", values)1648 expected = dict(page="login")1649 assert values == expected1650 1651 with app.test_request_context("/somepage"):1652- url = flask.url_for("foo.bar.baz.view")1652+ url = flask.url_for("foo.view")1653 expected = "/login"1654 assert url == expected1655 1656 373 unmodified linesmodified:tests/test_blueprints.py+12 added-76 removed
1-import functools2-1 import pytest2 from jinja2 import TemplateNotFound3 from werkzeug.http import parse_cache_control_header4 245 unmodified lines250 templates = sorted(app.jinja_env.list_templates())251 assert templates == ["admin/index.html", "frontend/index.html"]252 253 256-def test_dotted_names(app, client):257- frontend = flask.Blueprint("myapp.frontend", __name__)258- backend = flask.Blueprint("myapp.backend", __name__)254+def test_dotted_name_not_allowed(app, client):255+ with pytest.raises(ValueError):256+ flask.Blueprint("app.ui", __name__)257 260- @frontend.route("/fe")261- def frontend_index():262- return flask.url_for("myapp.backend.backend_index")258 264- @frontend.route("/fe2")265- def frontend_page2():266- return flask.url_for(".frontend_index")267-268- @backend.route("/be")269- def backend_index():270- return flask.url_for("myapp.frontend.frontend_index")271-272- app.register_blueprint(frontend)273- app.register_blueprint(backend)274-275- assert client.get("/fe").data.strip() == b"/be"276- assert client.get("/fe2").data.strip() == b"/fe"277- assert client.get("/be").data.strip() == b"/fe"278-279-259 def test_dotted_names_from_app(app, client):260 test = flask.Blueprint("test", __name__)261 262 @app.route("/")58 unmodified lines321 322 def test_route_decorator_custom_endpoint_with_dots(app, client):323 bp = flask.Blueprint("bp", __name__)324 346- @bp.route("/foo")347- def foo():348- return flask.request.endpoint325+ with pytest.raises(ValueError):326+ bp.route("/", endpoint="a.b")(lambda: "")327 350- try:328+ with pytest.raises(ValueError):329+ bp.add_url_rule("/", endpoint="a.b")330 352- @bp.route("/bar", endpoint="bar.bar")353- def foo_bar():354- return flask.request.endpoint331+ def view():332+ return ""333 356- except AssertionError:357- pass358- else:359- raise AssertionError("expected AssertionError not raised")334+ view.__name__ = "a.b"335 361- try:336+ with pytest.raises(ValueError):337+ bp.add_url_rule("/", view_func=view)338 363- @bp.route("/bar/123", endpoint="bar.123")364- def foo_bar_foo():365- return flask.request.endpoint339 367- except AssertionError:368- pass369- else:370- raise AssertionError("expected AssertionError not raised")371-372- def foo_foo_foo():373- pass374-375- pytest.raises(376- AssertionError,377- lambda: bp.add_url_rule("/bar/123", endpoint="bar.123", view_func=foo_foo_foo),378- )379-380- pytest.raises(381- AssertionError, bp.route("/bar/123", endpoint="bar.123"), lambda: None382- )383-384- foo_foo_foo.__name__ = "bar.123"385-386- pytest.raises(387- AssertionError, lambda: bp.add_url_rule("/bar/123", view_func=foo_foo_foo)388- )389-390- bp.add_url_rule(391- "/bar/456", endpoint="foofoofoo", view_func=functools.partial(foo_foo_foo)392- )393-394- app.register_blueprint(bp, url_prefix="/py")395-396- assert client.get("/py/foo").data == b"bp.foo"397- # The rule's didn't actually made it through398- rv = client.get("/py/bar")399- assert rv.status_code == 404400- rv = client.get("/py/bar/123")401- assert rv.status_code == 404402-403-340 def test_endpoint_decorator(app, client):341 from werkzeug.routing import Rule342 343 app.url_map.add(Rule("/foo", endpoint="bar"))494 unmodified linesmodified:CHANGES.rst+3 added
13 unmodified lines14 object that has arbitrary attributes. :issue:`4020`15 - Fix some types that weren't available in Python 3.6.0. :issue:`4040`16 - Improve typing for ``send_file``, ``send_from_directory``, and17 ``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 22 23 Version 2.0.024 -------------1096 unmodified lines