add redirect method to app
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tim Hoagland · committed by David Lord · parent bd56d19 · Browse files · 5 files·+55 added−1 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+55 added−1 removed
- tests:+16 added−0 removed
- other:+39 added−1 removed
Provenance
- Commit
fdab801- Tree
771b4e2- Author
Tim Hoagland <tim.hoagalnd@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
bd56d19
5 files changed+55 added−1 removed
modified:src/flask/__init__.py+1 added-1 removed
1 from markupsafe import escape2 from markupsafe import Markup3 from werkzeug.exceptions import abort as abort4-from werkzeug.utils import redirect as redirect4 5 from . import json as json6 from .app import Flask as Flask7 from .app import Request as Request13 unmodified lines21 from .helpers import flash as flash22 from .helpers import get_flashed_messages as get_flashed_messages23 from .helpers import get_template_attribute as get_template_attribute24 from .helpers import make_response as make_response25+from .helpers import redirect as redirect26 from .helpers import send_file as send_file27 from .helpers import send_from_directory as send_from_directory28 from .helpers import stream_with_context as stream_with_context29 from .helpers import url_for as url_for16 unmodified linesmodified:src/flask/app.py+11 added
20 unmodified lines21 from werkzeug.routing import MapAdapter22 from werkzeug.routing import RequestRedirect23 from werkzeug.routing import RoutingException24 from werkzeug.routing import Rule25+from werkzeug.utils import redirect as _wz_redirect26 from werkzeug.wrappers import Response as BaseResponse27 28 from . import cli29 from . import json1600 unmodified lines1630 ) from None1631 1632 return asgiref_async_to_sync(func)1633 1634+ def redirect(self, location: str, code: int = 302) -> BaseResponse:1635+ """Create a redirect response object.1636+1637+ :param location: the url of the redirect1638+ :param code: http return code1639+1640+ .. versionadded:: 2.21641+ """1642+ return _wz_redirect(location, code=code, Response=self.response_class)1643+1644 def make_response(self, rv: ResponseReturnValue) -> Response:1645 """Convert the return value from a view function to an instance of1646 :attr:`response_class`.1647 459 unmodified linesmodified:src/flask/helpers.py+24 added
10 unmodified lines11 12 import werkzeug.utils13 from werkzeug.routing import BuildError14 from werkzeug.urls import url_quote15+from werkzeug.utils import redirect as _wz_redirect16 17 from .globals import _app_ctx_stack18 from .globals import _request_ctx_stack19 from .globals import current_app1 unmodified line21 from .globals import session22 from .signals import message_flashed23 24 if t.TYPE_CHECKING: # pragma: no cover25+ from werkzeug.wrappers import Response as BaseResponse26 from .wrappers import Response27 28 29 def get_env() -> str:311 unmodified lines341 rv += f"#{url_quote(anchor)}"342 return rv343 344 345+def redirect(346+ location: str, code: int = 302, Response: t.Optional[t.Type["BaseResponse"]] = None347+) -> "BaseResponse":348+ """Create a redirect response object.349+350+ If :data:`~flask.current_app` is available, it will use351+ :meth:`~flask.app.Flask.redirect`, otherwise it will use352+ :func:`werkzeug.utils.redirect`.353+354+ :param location: The URL to redirect to.355+ :param code: The status code for the redirect.356+ :param Response: The response class to use. Not used when357+ ``current_app`` is active, which uses ``app.response_class``.358+359+ .. versionadded:: 2.2360+ """361+ if current_app:362+ return current_app.redirect(location, code=code)363+364+ return _wz_redirect(location, code=code, Response=Response)365+366+367 def get_template_attribute(template_name: str, attribute: str) -> t.Any:368 """Loads a macro (or variable) a template exports. This can be used to369 invoke a macro from within Python code. If you for example have a370 template named :file:`_cider.html` with the following contents:444 unmodified linesmodified:tests/test_helpers.py+16 added
156 unmodified lines157 assert flask.url_for("myview", id=42, _method="GET") == "/myview/42"158 assert flask.url_for("myview", _method="POST") == "/myview/create"159 160 161+def test_redirect_no_app():162+ response = flask.redirect("https://localhost", 307)163+ assert response.location == "https://localhost"164+ assert response.status_code == 307165+166+167+def test_redirect_with_app(app):168+ def redirect(location, code=302):169+ raise ValueError170+171+ app.redirect = redirect172+173+ with app.app_context(), pytest.raises(ValueError):174+ flask.redirect("other")175+176+177 class TestNoImports:178 """Test Flasks are created without import.179 180 Avoiding ``__import__`` helps create Flask instances where there are errors149 unmodified linesmodified:CHANGES.rst+3 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- Add an ``app.redirect`` method, which ``flask.redirect`` will call.9+ This makes it possible for an app to override how redirects work.10+ :issue:`4569`11 - Refactor ``register_error_handler`` to consolidate error checking.12 Rewrite some error messages to be more consistent. :issue:`4559`13 14 1282 unmodified lines