Skip to content

Commit

Permalink
Improve patt & asset path, SimpleStaticFileServer
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatz committed Nov 4, 2024
1 parent f37c8e7 commit e949682
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 14 additions & 5 deletions django_utils_lib/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,25 @@ def guard_path(self, request: HttpRequest, url_path: str) -> Optional[HttpRespon
return None

def serve_static_path(
self, request: HttpRequest, asset_path: Optional[str] = None
self, request: HttpRequest, asset_path: str, url_path: Optional[str] = None
) -> Union[HttpResponse, FileResponse]:
"""
This should be close to a drop-in replacement for `serve` (it wraps it),
with config-based logic for how to handle the request
Note: The difference between `asset_path` and `url_path` is that `asset_path`
is the actual filesystem path (relative to staticfiles root) and `url_path`
is what the user sees as the path. They _can_ be different, but don't _have_
to be. A good use-case for having them different values is so that you can use
something like `/my_page/` as the `url_path`, but `/my_page/index.html` as the
`asset_path`.
"""
if request.method not in ["GET", "HEAD", "OPTIONS"]:
return HttpResponseNotAllowed(["GET", "HEAD", "OPTIONS"])
url_path = asset_path or request.path
url_path = url_path or request.path
if (response := self.guard_path(request, url_path)) is not None:
return response
return serve(request, document_root=str(settings.STATIC_ROOT), path=url_path)
return serve(request, document_root=str(settings.STATIC_ROOT), path=asset_path)

def generate_url_patterns(self, ignore_start_strings: Optional[List[str]] = None):
"""
Expand All @@ -107,8 +114,10 @@ def generate_url_patterns(self, ignore_start_strings: Optional[List[str]] = None
re_path(rf"^{negate_start_pattern}(?P<asset_path>.*\..*)$", self.serve_static_path),
# For extension-less paths, try to map to an `index.html`
re_path(
r"^(?P<asset_path>.+/$)",
lambda request, asset_path: self.serve_static_path(request, f"{asset_path}/index.html"),
r"^(?P<asset_path>[^?#]+).*$",
lambda request, asset_path: self.serve_static_path(
request, f"{asset_path.removesuffix('/')}/index.html"
),
),
]

Expand Down
7 changes: 2 additions & 5 deletions django_utils_lib/tests/test_simplestaticfileserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ def check_response(

check_response(server.serve_static_path(request=mock_request, asset_path=url_path), expected_response)

# Based on request alone
check_response(server.serve_static_path(request=mock_request), expected_response)


@pytest.mark.parametrize(
"ignore_start_strings, expected_patterns",
Expand All @@ -102,14 +99,14 @@ def check_response(
None,
[
re.compile(r"^(?!/static/)(?!/media/)(?P<asset_path>.*\..*)$"),
re.compile(r"^(?P<asset_path>.+/$)"),
re.compile(r"^(?P<asset_path>[^?#]+).*$"),
],
),
(
["/assets/", "/files/"],
[
re.compile(r"^(?!/assets/)(?!/files/)(?P<asset_path>.*\..*)$"),
re.compile(r"^(?P<asset_path>.+/$)"),
re.compile(r"^(?P<asset_path>[^?#]+).*$"),
],
),
],
Expand Down

0 comments on commit e949682

Please sign in to comment.