Skip to content
Myr

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

5 files+55 added−1 removed

modified:src/flask/__init__.py+1 added-1 removed
1from markupsafe import escape2from markupsafe import Markup3from werkzeug.exceptions import abort as abort4from werkzeug.utils import redirect as redirect45from . import json as json6from .app import Flask as Flask7from .app import Request as Request
13 unmodified lines
21from .helpers import flash as flash22from .helpers import get_flashed_messages as get_flashed_messages23from .helpers import get_template_attribute as get_template_attribute24from .helpers import make_response as make_response25from .helpers import redirect as redirect26from .helpers import send_file as send_file27from .helpers import send_from_directory as send_from_directory28from .helpers import stream_with_context as stream_with_context29from .helpers import url_for as url_for
16 unmodified lines
modified:src/flask/app.py+11 added
20 unmodified lines
21from werkzeug.routing import MapAdapter22from werkzeug.routing import RequestRedirect23from werkzeug.routing import RoutingException24from werkzeug.routing import Rule25from werkzeug.utils import redirect as _wz_redirect26from werkzeug.wrappers import Response as BaseResponse2728from . import cli29from . import json
1600 unmodified lines
1630 ) from None16311632 return asgiref_async_to_sync(func)16331634 def redirect(self, location: str, code: int = 302) -> BaseResponse:1635 """Create a redirect response object.16361637 :param location: the url of the redirect1638 :param code: http return code16391640 .. versionadded:: 2.21641 """1642 return _wz_redirect(location, code=code, Response=self.response_class)16431644 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 lines
modified:src/flask/helpers.py+24 added
10 unmodified lines
1112import werkzeug.utils13from werkzeug.routing import BuildError14from werkzeug.urls import url_quote15from werkzeug.utils import redirect as _wz_redirect1617from .globals import _app_ctx_stack18from .globals import _request_ctx_stack19from .globals import current_app
1 unmodified line
21from .globals import session22from .signals import message_flashed2324if t.TYPE_CHECKING: # pragma: no cover25 from werkzeug.wrappers import Response as BaseResponse26 from .wrappers import Response272829def get_env() -> str:
311 unmodified lines
341 rv += f"#{url_quote(anchor)}"342 return rv343344345def redirect(346 location: str, code: int = 302, Response: t.Optional[t.Type["BaseResponse"]] = None347) -> "BaseResponse":348 """Create a redirect response object.349350 If :data:`~flask.current_app` is available, it will use351 :meth:`~flask.app.Flask.redirect`, otherwise it will use352 :func:`werkzeug.utils.redirect`.353354 :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``.358359 .. versionadded:: 2.2360 """361 if current_app:362 return current_app.redirect(location, code=code)363364 return _wz_redirect(location, code=code, Response=Response)365366367def 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 lines
modified:tests/test_helpers.py+16 added
156 unmodified lines
157 assert flask.url_for("myview", id=42, _method="GET") == "/myview/42"158 assert flask.url_for("myview", _method="POST") == "/myview/create"159160161def test_redirect_no_app():162 response = flask.redirect("https://localhost", 307)163 assert response.location == "https://localhost"164 assert response.status_code == 307165166167def test_redirect_with_app(app):168 def redirect(location, code=302):169 raise ValueError170171 app.redirect = redirect172173 with app.app_context(), pytest.raises(ValueError):174 flask.redirect("other")175176177class TestNoImports:178 """Test Flasks are created without import.179180 Avoiding ``__import__`` helps create Flask instances where there are errors
149 unmodified lines
modified:CHANGES.rst+3 added
3 unmodified lines
4-------------56Unreleased78- 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`1314
1282 unmodified lines