Skip to content
Myr

Merge pull request #3104 from eladm26/adding_models_converter

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

moving match_request to RequestContext.push to enable ModelConverter

authored by David Lord · committed by GitHub · parents 29111a3, c658639 · Browse files · 5 files+52 added−26 removed
This commit

State

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

Diff stats

5 files+52 added−26 removed

  • tests:+43 added−21 removed
  • other:+9 added−5 removed

5 files+52 added−26 removed

modified:src/flask/ctx.py+5 added-5 removed
312 unmodified lines
313 # object. These will be called before the regular "after_request"314 # functions.315 self._after_request_functions = []316317 if self.url_adapter is not None:318 self.match_request()319317 @property318 def g(self):319 return _app_ctx_stack.top.g320
25 unmodified lines
346 """Can be overridden by a subclass to hook into the matching347 of the request.348 """349 try:353 url_rule, self.request.view_args = self.url_adapter.match(return_rule=True)354 self.request.url_rule = url_rule350 result = self.url_adapter.match(return_rule=True)351 self.request.url_rule, self.request.view_args = result352 except HTTPException as e:353 self.request.routing_exception = e354355 def push(self):
35 unmodified lines
391392 if self.session is None:393 self.session = session_interface.make_null_session(self.app)394395 if self.url_adapter is not None:396 self.match_request()397398 def pop(self, exc=_sentinel):399 """Pops the request context and unbinds it by doing that. This will400 also trigger the execution of functions registered by the401 :meth:`~flask.Flask.teardown_request` decorator.
74 unmodified lines
modified:tests/test_basic.py-20 removed
1382 unmodified lines
1383 with app.test_request_context():1384 flask.url_for("/")138513861387def test_custom_converters(app, client):1388 from werkzeug.routing import BaseConverter13891390 class ListConverter(BaseConverter):1391 def to_python(self, value):1392 return value.split(",")13931394 def to_url(self, value):1395 base_to_url = super(ListConverter, self).to_url1396 return ",".join(base_to_url(x) for x in value)13971398 app.url_map.converters["list"] = ListConverter13991400 @app.route("/<list:args>")1401 def index(args):1402 return "|".join(args)14031404 assert client.get("/1,2,3").data == b"1|2|3"140514061387def test_static_files(app, client):1388 rv = client.get("/static/index.html")1389 assert rv.status_code == 2001390 assert rv.data.strip() == b"<h1>Hello World!</h1>"
579 unmodified lines
added:tests/test_converters.py+40 added
1from werkzeug.routing import BaseConverter23from flask import has_request_context4from flask import url_for567def test_custom_converters(app, client):8 class ListConverter(BaseConverter):9 def to_python(self, value):10 return value.split(",")1112 def to_url(self, value):13 base_to_url = super(ListConverter, self).to_url14 return ",".join(base_to_url(x) for x in value)1516 app.url_map.converters["list"] = ListConverter1718 @app.route("/<list:args>")19 def index(args):20 return "|".join(args)2122 assert client.get("/1,2,3").data == b"1|2|3"2324 with app.test_request_context():25 assert url_for("index", args=[4, 5, 6]) == "/4,5,6"262728def test_context_available(app, client):29 class ContextConverter(BaseConverter):30 def to_python(self, value):31 assert has_request_context()32 return value3334 app.url_map.converters["ctx"] = ContextConverter3536 @app.route("/<ctx:name>")37 def index(name):38 return name3940 assert client.get("/admin").data == b"admin"
modified:tests/test_testing.py+3 added-1 removed
151 unmodified lines
152 app.register_blueprint(bp)153154 ctx = app.test_request_context("/", subdomain="xxx")155 assert ctx.request.url == "http://xxx.example.com:1234/foo/"156 assert ctx.request.blueprint == bp.name156157 with ctx:158 assert ctx.request.blueprint == bp.name159160 rv = client.get("/", subdomain="xxx")161 assert rv.data == b"http://xxx.example.com:1234/foo/"162163
284 unmodified lines
modified:CHANGES.rst+4 added
69 unmodified lines
70 favour of a new class ``flask.testing.EnvironBuilder``. :pr:`3232`71- The ``flask run`` command no longer fails if Python is not built72 with SSL support. Using the ``--cert`` option will show an73 appropriate error message. :issue:`3211`74- URL matching now occurs after the request context is pushed, rather75 than when it's created. This allows custom URL converters to access76 the app and request contexts, such as to query a database for an id.77 :issue:`3088`7879.. _#2935: https://github.com/pallets/flask/issues/293580.. _#2957: https://github.com/pallets/flask/issues/295781.. _#2994: https://github.com/pallets/flask/pull/2994
947 unmodified lines