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
Provenance
- Commit
92acd05- Tree
f772867- Author
Ivan Sushkov <ivan.sushkov91@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
fac6303
2 files changed+46 added−37 removed
modified:src/flask/app.py+45 added
21 unmodified lines22 from werkzeug.routing import MapAdapter23 from werkzeug.routing import RequestRedirect24 from werkzeug.routing import RoutingException25 from werkzeug.routing import Rule26+from werkzeug.urls import url_quote27 from werkzeug.utils import redirect as _wz_redirect28 from werkzeug.wrappers import Response as BaseResponse29 30 from . import cli1630 unmodified lines1661 ) from None1662 1663 return asgiref_async_to_sync(func)1664 1665+ def url_for(1666+ self,1667+ endpoint: str,1668+ external: bool,1669+ url_adapter,1670+ **values: t.Any,1671+ ) -> str:1672+1673+ anchor = values.pop("_anchor", None)1674+ method = values.pop("_method", None)1675+ scheme = values.pop("_scheme", None)1676+ self.inject_url_defaults(endpoint, values)1677+1678+ # 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 = scheme1687+1688+ 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)1704+1705+ if anchor is not None:1706+ rv += f"#{url_quote(anchor)}"1707+ return rv1708+1709 def redirect(self, location: str, code: int = 302) -> BaseResponse:1710 """Create a redirect response object.1711 1712 This is called by :func:`flask.redirect`, and can be called472 unmodified linesmodified:src/flask/helpers.py+1 added-37 removed
9 unmodified lines10 from threading import RLock11 12 import werkzeug.utils13 from werkzeug.exceptions import abort as _wz_abort14-from werkzeug.routing import BuildError15-from werkzeug.urls import url_quote14 from werkzeug.utils import redirect as _wz_redirect15 16 from .globals import _app_ctx_stack17 from .globals import _request_ctx_stack286 unmodified lines304 )305 306 external = values.pop("_external", True)307 310- 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)309 315- # 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 = scheme310 325- 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)341-342- if anchor is not None:343- rv += f"#{url_quote(anchor)}"344- return rv345-346-311 def redirect(312 location: str, code: int = 302, Response: t.Optional[t.Type["BaseResponse"]] = None313 ) -> "BaseResponse":314 """Create a redirect response object.493 unmodified lines