Skip to content
Myr

add url_for method to app

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

authored by Ivan Sushkov · committed by David Lord · parent fac6303 · Browse files · 2 files+46 added−37 removed
This commit

State

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

Diff stats

2 files+46 added−37 removed

  • other:+46 added−37 removed

2 files+46 added−37 removed

modified:src/flask/app.py+45 added
21 unmodified lines
22from werkzeug.routing import MapAdapter23from werkzeug.routing import RequestRedirect24from werkzeug.routing import RoutingException25from werkzeug.routing import Rule26from werkzeug.urls import url_quote27from werkzeug.utils import redirect as _wz_redirect28from werkzeug.wrappers import Response as BaseResponse2930from . import cli
1630 unmodified lines
1661 ) from None16621663 return asgiref_async_to_sync(func)16641665 def url_for(1666 self,1667 endpoint: str,1668 external: bool,1669 url_adapter,1670 **values: t.Any,1671 ) -> str:16721673 anchor = values.pop("_anchor", None)1674 method = values.pop("_method", None)1675 scheme = values.pop("_scheme", None)1676 self.inject_url_defaults(endpoint, values)16771678 # This is not the best way to deal with this but currently the1679 # underlying Werkzeug router does not support overriding the scheme on1680 # a per build call basis.1681 old_scheme = None1682 if scheme is not None:1683 if not external:1684 raise ValueError("When specifying _scheme, _external must be True")1685 old_scheme = url_adapter.url_scheme1686 url_adapter.url_scheme = scheme16871688 try:1689 try:1690 rv = url_adapter.build(1691 endpoint, values, method=method, force_external=external1692 )1693 finally:1694 if old_scheme is not None:1695 url_adapter.url_scheme = old_scheme1696 except BuildError as error:1697 # We need to inject the values again so that the app callback can1698 # deal with that sort of stuff.1699 values["_external"] = external1700 values["_anchor"] = anchor1701 values["_method"] = method1702 values["_scheme"] = scheme1703 return self.handle_url_build_error(error, endpoint, values)17041705 if anchor is not None:1706 rv += f"#{url_quote(anchor)}"1707 return rv17081709 def redirect(self, location: str, code: int = 302) -> BaseResponse:1710 """Create a redirect response object.17111712 This is called by :func:`flask.redirect`, and can be called
472 unmodified lines
modified:src/flask/helpers.py+1 added-37 removed
9 unmodified lines
10from threading import RLock1112import werkzeug.utils13from werkzeug.exceptions import abort as _wz_abort14from werkzeug.routing import BuildError15from werkzeug.urls import url_quote14from werkzeug.utils import redirect as _wz_redirect1516from .globals import _app_ctx_stack17from .globals import _request_ctx_stack
286 unmodified lines
304 )305306 external = values.pop("_external", True)307310 anchor = values.pop("_anchor", None)311 method = values.pop("_method", None)312 scheme = values.pop("_scheme", None)313 appctx.app.inject_url_defaults(endpoint, values)308 return current_app.url_for(endpoint, external, url_adapter, **values)309315 # This is not the best way to deal with this but currently the316 # underlying Werkzeug router does not support overriding the scheme on317 # a per build call basis.318 old_scheme = None319 if scheme is not None:320 if not external:321 raise ValueError("When specifying _scheme, _external must be True")322 old_scheme = url_adapter.url_scheme323 url_adapter.url_scheme = scheme310325 try:326 try:327 rv = url_adapter.build(328 endpoint, values, method=method, force_external=external329 )330 finally:331 if old_scheme is not None:332 url_adapter.url_scheme = old_scheme333 except BuildError as error:334 # We need to inject the values again so that the app callback can335 # deal with that sort of stuff.336 values["_external"] = external337 values["_anchor"] = anchor338 values["_method"] = method339 values["_scheme"] = scheme340 return appctx.app.handle_url_build_error(error, endpoint, values)341342 if anchor is not None:343 rv += f"#{url_quote(anchor)}"344 return rv345346311def redirect(312 location: str, code: int = 302, Response: t.Optional[t.Type["BaseResponse"]] = None313) -> "BaseResponse":314 """Create a redirect response object.
493 unmodified lines