Skip to content

Commit

Permalink
StaticFilesConfig name parameter (#464)
Browse files Browse the repository at this point in the history
* Fix statics and add tests
* Update static files config and notes
  • Loading branch information
tarsil authored Dec 26, 2024
1 parent f7ff231 commit eb3ac9b
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 10 deletions.
5 changes: 4 additions & 1 deletion docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ hide:

## 3.6.2

### Added

- `name` to StaticFiles config allowing to reverse lookup internally.

### Changed

- Cleanup Response.
- Move `transform` method to lilya but provide speedup in a mixin.
- Esmerald `Response` behaves like `make_response` in lilya with a plain `Response`.
- Special handle None (nothing is returned) in `Response`. It shouldn't map to `null` so not all handlers have to return a value.


### Fixed

- `bytes` won't be encoded as json when returned from a handler. This would unexpectly lead to a base64 encoding.
Expand Down
2 changes: 1 addition & 1 deletion docs_src/configurations/staticfiles/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", packages=["mypackage"], directory=Path("static")
path="/static", packages=["mypackage"], directory=Path("static"), name="static"
)

app = Esmerald(static_files_config=static_files_config)
2 changes: 1 addition & 1 deletion docs_src/configurations/staticfiles/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", packages=["mypackage"], directory=Path("static")
path="/static", packages=["mypackage"], directory=Path("static"), name="static"
)

app = Esmerald(static_files_config=static_files_config)
2 changes: 1 addition & 1 deletion docs_src/configurations/staticfiles/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", packages=["mypackage"], directory=Path("static")
path="/static", packages=["mypackage"], directory=Path("static"), name="static"
)

app = Esmerald(static_files_config=static_files_config)
7 changes: 5 additions & 2 deletions docs_src/configurations/staticfiles/example_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", packages=["mypackage"], directory=Path("static")
path="/static",
packages=["mypackage"],
directory=Path("static"),
name="static",
)

static_files_node_modules_config = StaticFilesConfig(
path="/static/node_modules", directory=Path("node_modules")
path="/static/node_modules", directory=Path("node_modules"), name="static"
)

app = Esmerald(static_files_config=[static_files_node_modules_config, static_files_config])
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", directory=["static/overwrites", "static", "static/defaults", "node_modules"]
path="/static", directory=["static/overwrites", "static", "static/defaults", "node_modules"],
name="static"
)

app = Esmerald(static_files_config=static_files_config)
7 changes: 6 additions & 1 deletion docs_src/configurations/staticfiles/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ def static_files_config(self) -> StaticFilesConfig:
Simple configuration indicating where the statics will be placed in
the application.
"""
return StaticFilesConfig(path="/static", packages=["mypackage"], directory=Path("static"))
return StaticFilesConfig(
path="/static",
packages=["mypackage"],
directory=Path("static"),
name="static",
)
2 changes: 1 addition & 1 deletion esmerald/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ def _configure(self) -> None:
if isinstance(self.static_files_config, (list, tuple))
else [self.static_files_config]
):
static_route = Include(path=config.path, app=config.to_app())
static_route = Include(path=config.path, app=config.to_app(), name=config.name)
self.router.validate_root_route_parent(static_route)
self.router.routes.append(static_route)

Expand Down
10 changes: 9 additions & 1 deletion esmerald/config/static_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class StaticFilesConfig(BaseModel):
"""
),
] = True
name: Annotated[
Optional[str],
Doc(
"""
The name of the static files to be looked up at.
"""
),
] = None

@field_validator("path")
def validate_path(cls, value: str) -> str:
Expand All @@ -88,4 +96,4 @@ def to_app(self) -> ASGIApp:
It can be three scenarios
"""

return StaticFiles(**self.model_dump(exclude_none=True, exclude=["path"])) # type: ignore
return StaticFiles(**self.model_dump(exclude_none=True, exclude=["path", "name"])) # type: ignore
52 changes: 52 additions & 0 deletions tests/test_static_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import pytest
from pydantic import ValidationError

from esmerald import Include, get
from esmerald.config import StaticFilesConfig
from esmerald.config.template import TemplateConfig
from esmerald.testclient import create_client


Expand Down Expand Up @@ -117,3 +119,53 @@ def test_static_substring_of_self(tmpdir: Any) -> None:
response = client.get("/static/static_part/static/test.txt")
assert response.status_code == 200
assert response.text == "content"


@pytest.mark.parametrize("redirect_slashes", [True, False])
def test_mixing_static_and_include(tmpdir: Any, redirect_slashes) -> None:
@get("/include")
async def get_include() -> str:
return "include"

path = tmpdir.join("test.txt")
path.write("content")
static_files_config = StaticFilesConfig(path="/static", directory=tmpdir)

with create_client(
[Include("/", routes=[get_include])],
static_files_config=static_files_config,
redirect_slashes=redirect_slashes,
) as client:
response = client.get("/static/test.txt")
assert response.status_code == 200
assert response.text == "content"

response = client.get("/include")
assert response.status_code == 200


@pytest.mark.parametrize("redirect_slashes", [True, False])
def test_mixing_static_and_include_template_config(
tmpdir: Any, template_dir, redirect_slashes
) -> None:
@get("/include")
async def get_include() -> str:
return "include"

path = tmpdir.join("test.txt")
path.write("content")
static_files_config = StaticFilesConfig(path="/static", directory=tmpdir)
template_config = TemplateConfig(directory=template_dir)

with create_client(
[Include("/", routes=[get_include])],
static_files_config=static_files_config,
redirect_slashes=redirect_slashes,
template_config=template_config,
) as client:
response = client.get("/static/test.txt")
assert response.status_code == 200
assert response.text == "content"

response = client.get("/include")
assert response.status_code == 200

0 comments on commit eb3ac9b

Please sign in to comment.