Skip to content
Myr

Merge pull request #4055 from pallets/converter-session

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

converters have access to session

authored by David Lord · committed by GitHub · parents 636f195, a7b02b3 · 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

4 files+28 added−16 removed

modified:src/flask/ctx.py+5 added-3 removed
393 unmodified lines
394 self._implicit_app_ctx_stack.append(None)395396 _request_ctx_stack.push(self)397398 if self.url_adapter is not None:399 self.match_request()400398 # 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 lines
405406 if self.session is None:407 self.session = session_interface.make_null_session(self.app)408409 # 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()413414 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 lines
modified:tests/test_converters.py+5 added-3 removed
1from werkzeug.routing import BaseConverter23from flask import has_request_context3from flask import request4from flask import session5from flask import url_for678def test_custom_converters(app, client):
19 unmodified lines
2829def 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 value3536 app.url_map.converters["ctx"] = ContextConverter3736 @app.route("/<ctx:name>")38 @app.get("/<ctx:name>")39 def index(name):40 return name4142 assert client.get("/admin").data == b"admin"
modified:tests/test_session_interface.py+15 added-10 removed
1import flask2from flask.sessions import SessionInterface345def test_open_session_endpoint_not_none():6 # Define a session interface that breaks if request.endpoint is None5def 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 """1011 class MySessionInterface(SessionInterface):8 def save_session(self):12 def save_session(self, app, session, response):13 pass1411 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 None1814 def index():15 return "Hello World!"1617 # Confirm a 200 response, indicating that request.endpoint was NOT None19 app = flask.Flask(__name__)19 app.route("/")(index)20 app.session_interface = MySessionInterface()2122 @app.get("/")23 def index():24 return "Hello, World!"2521 response = app.test_client().open("/")26 response = app.test_client().get("/")27 assert response.status_code == 200
modified:CHANGES.rst+3 added
26 unmodified lines
27 special meaning, it is used to separate (nested) blueprint names and28 the endpoint name. :issue:`4041`29- Combine URL prefixes when nesting blueprints that were created with30 a ``url_prefix`` value. :issue:`4037`31- Roll back a change to the order that URL matching was done. The32 URL is again matched after the session is loaded, so the session is33 available in custom URL converters. :issue:`4053`343536Version 2.0.037-------------
1116 unmodified lines