converters have access to session
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 8648750 · Browse files · 4 files·+28 added−16 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−16 removed
- tests:+20 added−13 removed
- other:+8 added−3 removed
Provenance
- Commit
a7b02b3- Tree
24417d5- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
8648750
4 files changed+28 added−16 removed
modified:src/flask/ctx.py+5 added-3 removed
393 unmodified lines394 self._implicit_app_ctx_stack.append(None)395 396 _request_ctx_stack.push(self)397 398- if self.url_adapter is not None:399- self.match_request()400-398 # Open the session at the moment that the request context is available.399 # This allows a custom open_session method to use the request context.400 # Only open a new session if this is the first time the request was401 # pushed, otherwise stream_with_context loses the session.3 unmodified lines405 406 if self.session is None:407 self.session = session_interface.make_null_session(self.app)408 409+ # Match the request URL after loading the session, so that the410+ # session is available in custom URL converters.411+ if self.url_adapter is not None:412+ self.match_request()413+414 def pop(self, exc: t.Optional[BaseException] = _sentinel) -> None: # type: ignore415 """Pops the request context and unbinds it by doing that. This will416 also trigger the execution of functions registered by the417 :meth:`~flask.Flask.teardown_request` decorator.63 unmodified linesmodified:tests/test_converters.py+5 added-3 removed
1 from werkzeug.routing import BaseConverter2 3-from flask import has_request_context3+from flask import request4+from flask import session5 from flask import url_for6 7 8 def test_custom_converters(app, client):19 unmodified lines28 29 def test_context_available(app, client):30 class ContextConverter(BaseConverter):31 def to_python(self, value):31- assert has_request_context()32+ assert request is not None33+ assert session is not None34 return value35 36 app.url_map.converters["ctx"] = ContextConverter37 36- @app.route("/<ctx:name>")38+ @app.get("/<ctx:name>")39 def index(name):40 return name41 42 assert client.get("/admin").data == b"admin"modified:tests/test_session_interface.py+15 added-10 removed
1 import flask2 from flask.sessions import SessionInterface3 4 5-def test_open_session_endpoint_not_none():6- # Define a session interface that breaks if request.endpoint is None5+def test_open_session_with_endpoint():6+ """If request.endpoint (or other URL matching behavior) is needed7+ while loading the session, RequestContext.match_request() can be8+ called manually.9+ """10+11 class MySessionInterface(SessionInterface):8- def save_session(self):12+ def save_session(self, app, session, response):13 pass14 11- def open_session(self, _, request):15+ def open_session(self, app, request):16+ flask._request_ctx_stack.top.match_request()17 assert request.endpoint is not None18 14- def index():15- return "Hello World!"16-17- # Confirm a 200 response, indicating that request.endpoint was NOT None19 app = flask.Flask(__name__)19- app.route("/")(index)20 app.session_interface = MySessionInterface()21+22+ @app.get("/")23+ def index():24+ return "Hello, World!"25+21- response = app.test_client().open("/")26+ response = app.test_client().get("/")27 assert response.status_code == 200modified:CHANGES.rst+3 added
18 unmodified lines19 special meaning, it is used to separate (nested) blueprint names and20 the endpoint name. :issue:`4041`21 - Combine URL prefixes when nesting blueprints that were created with22 a ``url_prefix`` value. :issue:`4037`23+- Roll back a change to the order that URL matching was done. The24+ URL is again matched after the session is loaded, so the session is25+ available in custom URL converters. :issue:`4053`26 27 28 Version 2.0.029 -------------1116 unmodified lines