Skip to content
Myr

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

4 files+28 added−85 removed

modified:src/flask/blueprints.py+10 added-6 removed
186 unmodified lines
187 static_url_path=static_url_path,188 template_folder=template_folder,189 root_path=root_path,190 )191192 if "." in name:193 raise ValueError("'name' may not contain a dot '.' character.")194195 self.name = name196 self.url_prefix = url_prefix197 self.subdomain = subdomain198 self.deferred_functions: t.List[DeferredSetupFunction] = []
164 unmodified lines
363 ) -> 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.")369370 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.")372373 self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))374375 def app_template_filter(self, name: t.Optional[str] = None) -> t.Callable:376 """Register a custom template filter, available application wide. Like
170 unmodified lines
modified:tests/test_basic.py+3 added-3 removed
1629 unmodified lines
1630 assert client.get("/foo").data == b"/en/about"163116321633def 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")16351636 @bp.url_defaults1637 def bp_defaults(endpoint, values):1638 values["page"] = "login"
4 unmodified lines
16431644 app.register_blueprint(bp)16451646 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 == expected16501651 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 == expected16551656
373 unmodified lines
modified:tests/test_blueprints.py+12 added-76 removed
1import functools21import pytest2from jinja2 import TemplateNotFound3from werkzeug.http import parse_cache_control_header4
245 unmodified lines
250 templates = sorted(app.jinja_env.list_templates())251 assert templates == ["admin/index.html", "frontend/index.html"]252253256def test_dotted_names(app, client):257 frontend = flask.Blueprint("myapp.frontend", __name__)258 backend = flask.Blueprint("myapp.backend", __name__)254def test_dotted_name_not_allowed(app, client):255 with pytest.raises(ValueError):256 flask.Blueprint("app.ui", __name__)257260 @frontend.route("/fe")261 def frontend_index():262 return flask.url_for("myapp.backend.backend_index")258264 @frontend.route("/fe2")265 def frontend_page2():266 return flask.url_for(".frontend_index")267268 @backend.route("/be")269 def backend_index():270 return flask.url_for("myapp.frontend.frontend_index")271272 app.register_blueprint(frontend)273 app.register_blueprint(backend)274275 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"278279259def test_dotted_names_from_app(app, client):260 test = flask.Blueprint("test", __name__)261262 @app.route("/")
58 unmodified lines
321322def test_route_decorator_custom_endpoint_with_dots(app, client):323 bp = flask.Blueprint("bp", __name__)324346 @bp.route("/foo")347 def foo():348 return flask.request.endpoint325 with pytest.raises(ValueError):326 bp.route("/", endpoint="a.b")(lambda: "")327350 try:328 with pytest.raises(ValueError):329 bp.add_url_rule("/", endpoint="a.b")330352 @bp.route("/bar", endpoint="bar.bar")353 def foo_bar():354 return flask.request.endpoint331 def view():332 return ""333356 except AssertionError:357 pass358 else:359 raise AssertionError("expected AssertionError not raised")334 view.__name__ = "a.b"335361 try:336 with pytest.raises(ValueError):337 bp.add_url_rule("/", view_func=view)338363 @bp.route("/bar/123", endpoint="bar.123")364 def foo_bar_foo():365 return flask.request.endpoint339367 except AssertionError:368 pass369 else:370 raise AssertionError("expected AssertionError not raised")371372 def foo_foo_foo():373 pass374375 pytest.raises(376 AssertionError,377 lambda: bp.add_url_rule("/bar/123", endpoint="bar.123", view_func=foo_foo_foo),378 )379380 pytest.raises(381 AssertionError, bp.route("/bar/123", endpoint="bar.123"), lambda: None382 )383384 foo_foo_foo.__name__ = "bar.123"385386 pytest.raises(387 AssertionError, lambda: bp.add_url_rule("/bar/123", view_func=foo_foo_foo)388 )389390 bp.add_url_rule(391 "/bar/456", endpoint="foofoofoo", view_func=functools.partial(foo_foo_foo)392 )393394 app.register_blueprint(bp, url_prefix="/py")395396 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 == 404402403340def test_endpoint_decorator(app, client):341 from werkzeug.routing import Rule342343 app.url_map.add(Rule("/foo", endpoint="bar"))
494 unmodified lines
modified:CHANGES.rst+3 added
13 unmodified lines
14 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`212223Version 2.0.024-------------
1096 unmodified lines