Add ability for app.static() to return the routes it created. (#1954)
This allows blueprint registration to add the bp's static routes to its list of own routes. So now blueprint middlewares will apply to a blueprint's static file routes. Fixes #1953
This commit is contained in:
parent
5928c50057
commit
fb3d368a78
|
@ -676,9 +676,10 @@ class Sanic:
|
||||||
:param strict_slashes: Instruct :class:`Sanic` to check if the request
|
:param strict_slashes: Instruct :class:`Sanic` to check if the request
|
||||||
URLs need to terminate with a */*
|
URLs need to terminate with a */*
|
||||||
:param content_type: user defined content type for header
|
:param content_type: user defined content type for header
|
||||||
:return: None
|
:return: routes registered on the router
|
||||||
|
:rtype: List[sanic.router.Route]
|
||||||
"""
|
"""
|
||||||
static_register(
|
return static_register(
|
||||||
self,
|
self,
|
||||||
uri,
|
uri,
|
||||||
file_or_directory,
|
file_or_directory,
|
||||||
|
|
|
@ -143,7 +143,18 @@ class Blueprint:
|
||||||
if _routes:
|
if _routes:
|
||||||
routes += _routes
|
routes += _routes
|
||||||
|
|
||||||
|
# Static Files
|
||||||
|
for future in self.statics:
|
||||||
|
# Prepend the blueprint URI prefix if available
|
||||||
|
uri = url_prefix + future.uri if url_prefix else future.uri
|
||||||
|
_routes = app.static(
|
||||||
|
uri, future.file_or_directory, *future.args, **future.kwargs
|
||||||
|
)
|
||||||
|
if _routes:
|
||||||
|
routes += _routes
|
||||||
|
|
||||||
route_names = [route.name for route in routes if route]
|
route_names = [route.name for route in routes if route]
|
||||||
|
|
||||||
# Middleware
|
# Middleware
|
||||||
for future in self.middlewares:
|
for future in self.middlewares:
|
||||||
if future.args or future.kwargs:
|
if future.args or future.kwargs:
|
||||||
|
@ -160,14 +171,6 @@ class Blueprint:
|
||||||
for future in self.exceptions:
|
for future in self.exceptions:
|
||||||
app.exception(*future.args, **future.kwargs)(future.handler)
|
app.exception(*future.args, **future.kwargs)(future.handler)
|
||||||
|
|
||||||
# Static Files
|
|
||||||
for future in self.statics:
|
|
||||||
# Prepend the blueprint URI prefix if available
|
|
||||||
uri = url_prefix + future.uri if url_prefix else future.uri
|
|
||||||
app.static(
|
|
||||||
uri, future.file_or_directory, *future.args, **future.kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
# Event listeners
|
# Event listeners
|
||||||
for event, listeners in self.listeners.items():
|
for event, listeners in self.listeners.items():
|
||||||
for listener in listeners:
|
for listener in listeners:
|
||||||
|
|
|
@ -134,6 +134,8 @@ def register(
|
||||||
threshold size to switch to file_stream()
|
threshold size to switch to file_stream()
|
||||||
:param name: user defined name used for url_for
|
:param name: user defined name used for url_for
|
||||||
:param content_type: user defined content type for header
|
:param content_type: user defined content type for header
|
||||||
|
:return: registered static routes
|
||||||
|
:rtype: List[sanic.router.Route]
|
||||||
"""
|
"""
|
||||||
# If we're not trying to match a file directly,
|
# If we're not trying to match a file directly,
|
||||||
# serve from the folder
|
# serve from the folder
|
||||||
|
@ -155,10 +157,11 @@ def register(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
app.route(
|
_routes, _ = app.route(
|
||||||
uri,
|
uri,
|
||||||
methods=["GET", "HEAD"],
|
methods=["GET", "HEAD"],
|
||||||
name=name,
|
name=name,
|
||||||
host=host,
|
host=host,
|
||||||
strict_slashes=strict_slashes,
|
strict_slashes=strict_slashes,
|
||||||
)(_handler)
|
)(_handler)
|
||||||
|
return _routes
|
||||||
|
|
|
@ -735,6 +735,36 @@ def test_static_blueprint_name(app: Sanic, static_file_directory, file_name):
|
||||||
_, response = app.test_client.get("/static/test.file/")
|
_, response = app.test_client.get("/static/test.file/")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("file_name", ["test.file"])
|
||||||
|
def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
|
||||||
|
current_file = inspect.getfile(inspect.currentframe())
|
||||||
|
with open(current_file, "rb") as file:
|
||||||
|
file.read()
|
||||||
|
|
||||||
|
triggered = False
|
||||||
|
|
||||||
|
bp = Blueprint(name="test_mw", url_prefix="")
|
||||||
|
|
||||||
|
@bp.middleware('request')
|
||||||
|
def bp_mw1(request):
|
||||||
|
nonlocal triggered
|
||||||
|
triggered = True
|
||||||
|
|
||||||
|
bp.static(
|
||||||
|
"/test.file",
|
||||||
|
get_file_path(static_file_directory, file_name),
|
||||||
|
strict_slashes=True,
|
||||||
|
name="static"
|
||||||
|
)
|
||||||
|
|
||||||
|
app.blueprint(bp)
|
||||||
|
|
||||||
|
uri = app.url_for("test_mw.static")
|
||||||
|
assert uri == "/test.file"
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/test.file")
|
||||||
|
assert triggered is True
|
||||||
|
|
||||||
|
|
||||||
def test_route_handler_add(app: Sanic):
|
def test_route_handler_add(app: Sanic):
|
||||||
view = CompositionView()
|
view = CompositionView()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user