Remove unnecessary prefix from websocket handler name (#2021)
Remove the websocket prefix "websocket_handler_" introduced in 761eef7. Add a backward support for url_for() calling with this prefix in param "view_name".
This commit is contained in:
parent
97635111af
commit
8d86c3c598
23
sanic/app.py
23
sanic/app.py
@ -12,6 +12,7 @@ from ssl import Purpose, SSLContext, create_default_context
|
|||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
from typing import Any, Dict, Optional, Type, Union
|
from typing import Any, Dict, Optional, Type, Union
|
||||||
from urllib.parse import urlencode, urlunparse
|
from urllib.parse import urlencode, urlunparse
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
from sanic import reloader_helpers
|
from sanic import reloader_helpers
|
||||||
from sanic.asgi import ASGIApp
|
from sanic.asgi import ASGIApp
|
||||||
@ -494,9 +495,7 @@ class Sanic:
|
|||||||
websocket_handler = partial(
|
websocket_handler = partial(
|
||||||
self._websocket_handler, handler, subprotocols=subprotocols
|
self._websocket_handler, handler, subprotocols=subprotocols
|
||||||
)
|
)
|
||||||
websocket_handler.__name__ = (
|
websocket_handler.__name__ = handler.__name__
|
||||||
"websocket_handler_" + handler.__name__
|
|
||||||
)
|
|
||||||
routes.extend(
|
routes.extend(
|
||||||
self.router.add(
|
self.router.add(
|
||||||
uri=uri,
|
uri=uri,
|
||||||
@ -747,6 +746,24 @@ class Sanic:
|
|||||||
kw.update(name=view_name)
|
kw.update(name=view_name)
|
||||||
|
|
||||||
uri, route = self.router.find_route_by_view_name(view_name, **kw)
|
uri, route = self.router.find_route_by_view_name(view_name, **kw)
|
||||||
|
|
||||||
|
# TODO(laggardkernel): this fix should be removed in v21.3.
|
||||||
|
# Try again without the unnecessary prefix "websocket_handler_",
|
||||||
|
# which was added by accident on non-blueprint handlers. GH-2021
|
||||||
|
if not (uri and route) and view_name.startswith("websocket_handler_"):
|
||||||
|
view_name = view_name[18:]
|
||||||
|
uri, route = self.router.find_route_by_view_name(view_name, **kw)
|
||||||
|
if uri and route:
|
||||||
|
warn(
|
||||||
|
"The bug of adding unnecessary `websocket_handler_` "
|
||||||
|
"prefix in param `view_name` for non-blueprint handlers "
|
||||||
|
"is fixed. This backward support will be removed in "
|
||||||
|
"v21.3. Please update `Sanic.url_for()` callings in your "
|
||||||
|
"code soon.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
|
||||||
if not (uri and route):
|
if not (uri and route):
|
||||||
raise URLBuildError(
|
raise URLBuildError(
|
||||||
f"Endpoint with name `{view_name}` was not found"
|
f"Endpoint with name `{view_name}` was not found"
|
||||||
|
@ -348,3 +348,13 @@ def test_methodview_naming(methodview_app):
|
|||||||
|
|
||||||
assert viewone_url == "/view_one"
|
assert viewone_url == "/view_one"
|
||||||
assert viewtwo_url == "/view_two"
|
assert viewtwo_url == "/view_two"
|
||||||
|
|
||||||
|
|
||||||
|
def test_url_for_with_websocket_handlers(app):
|
||||||
|
# Test for a specific bugfix in GH-2021
|
||||||
|
@app.websocket("/ws")
|
||||||
|
async def my_handler(request, ws):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert app.url_for("my_handler") == "/ws"
|
||||||
|
assert app.url_for("websocket_handler_my_handler") == "/ws"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user