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
Provenance
- Commit
2fa30f6- Tree
085dd99- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
29111a3- Parent 2
c658639
5 files changed+52 added−26 removed
modified:src/flask/ctx.py+5 added-5 removed
312 unmodified lines313 # object. These will be called before the regular "after_request"314 # functions.315 self._after_request_functions = []316 317- if self.url_adapter is not None:318- self.match_request()319-317 @property318 def g(self):319 return _app_ctx_stack.top.g320 25 unmodified lines346 """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 = e354 355 def push(self):35 unmodified lines391 392 if self.session is None:393 self.session = session_interface.make_null_session(self.app)394 395+ if self.url_adapter is not None:396+ self.match_request()397+398 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 linesmodified:tests/test_basic.py-20 removed
1382 unmodified lines1383 with app.test_request_context():1384 flask.url_for("/")1385 1386 1387-def test_custom_converters(app, client):1388- from werkzeug.routing import BaseConverter1389-1390- class ListConverter(BaseConverter):1391- def to_python(self, value):1392- return value.split(",")1393-1394- def to_url(self, value):1395- base_to_url = super(ListConverter, self).to_url1396- return ",".join(base_to_url(x) for x in value)1397-1398- app.url_map.converters["list"] = ListConverter1399-1400- @app.route("/<list:args>")1401- def index(args):1402- return "|".join(args)1403-1404- assert client.get("/1,2,3").data == b"1|2|3"1405-1406-1387 def 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 linesadded:tests/test_converters.py+40 added
1+from werkzeug.routing import BaseConverter2+3+from flask import has_request_context4+from flask import url_for5+6+7+def test_custom_converters(app, client):8+ class ListConverter(BaseConverter):9+ def to_python(self, value):10+ return value.split(",")11+12+ def to_url(self, value):13+ base_to_url = super(ListConverter, self).to_url14+ return ",".join(base_to_url(x) for x in value)15+16+ app.url_map.converters["list"] = ListConverter17+18+ @app.route("/<list:args>")19+ def index(args):20+ return "|".join(args)21+22+ assert client.get("/1,2,3").data == b"1|2|3"23+24+ with app.test_request_context():25+ assert url_for("index", args=[4, 5, 6]) == "/4,5,6"26+27+28+def test_context_available(app, client):29+ class ContextConverter(BaseConverter):30+ def to_python(self, value):31+ assert has_request_context()32+ return value33+34+ app.url_map.converters["ctx"] = ContextConverter35+36+ @app.route("/<ctx:name>")37+ def index(name):38+ return name39+40+ assert client.get("/admin").data == b"admin"modified:tests/test_testing.py+3 added-1 removed
151 unmodified lines152 app.register_blueprint(bp)153 154 ctx = app.test_request_context("/", subdomain="xxx")155 assert ctx.request.url == "http://xxx.example.com:1234/foo/"156- assert ctx.request.blueprint == bp.name156 157+ with ctx:158+ assert ctx.request.blueprint == bp.name159+160 rv = client.get("/", subdomain="xxx")161 assert rv.data == b"http://xxx.example.com:1234/foo/"162 163 284 unmodified linesmodified:CHANGES.rst+4 added
69 unmodified lines70 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`78 79 .. _#2935: https://github.com/pallets/flask/issues/293580 .. _#2957: https://github.com/pallets/flask/issues/295781 .. _#2994: https://github.com/pallets/flask/pull/2994947 unmodified lines