diff --git a/README.md b/README.md index fa6a732..df15e5f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Pin a tag, not a branch: ```toml dependencies = [ - "chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.2.0", + "chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.3.0", ] ``` @@ -106,3 +106,14 @@ chrome's assets then live at `/ui/chrome/...` and the service's at this: its design (D-0008) puts everything behind exactly two proxied prefixes, `/ui` and `/api`, so a third top-level path would be unreachable behind Caddy. + +## Per-request page context + +Flask's context processors, with the app object taken out: + +```python +ui.page_context = lambda request: {"user": current_user(request)} +``` + +It runs on every render, because what it returns usually depends on who is +asking. An explicit value passed at the render site wins over it. diff --git a/chrome/__init__.py b/chrome/__init__.py index 6a4b4c0..ca68ee7 100644 --- a/chrome/__init__.py +++ b/chrome/__init__.py @@ -7,7 +7,7 @@ need an `if service == "marketplace"`, it belongs in the service. from pathlib import Path -__version__ = "0.2.0" +__version__ = "0.3.0" PACKAGE_DIR = Path(__file__).parent STATIC_DIR = PACKAGE_DIR / "static" diff --git a/chrome/app.py b/chrome/app.py index b20fd7f..1c2928d 100644 --- a/chrome/app.py +++ b/chrome/app.py @@ -101,6 +101,11 @@ class Chrome: wa_base=self.wa_path, ) + #: Optional per-request context every page gets, set by the service. + #: Flask had context processors; this is the same idea with the app + #: object taken out of it. Takes a Request, returns a dict. + self.page_context = None + app.include_router(health_router(service, version)) self._install_language_route() self._install_error_pages() @@ -129,7 +134,9 @@ class Chrome: def context(self, request: Request, **extra: object) -> dict[str, object]: locale = self.translations.locale_for(request) + shared = self.page_context(request) if self.page_context else {} return { + **shared, "title": self.title, "subtitle": self.subtitle, "service": self.service, diff --git a/pyproject.toml b/pyproject.toml index 18c8a56..d97dadf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chrome" -version = "0.2.0" +version = "0.3.0" description = "Shared page shell, design tokens and service helpers for the internal services." requires-python = ">=3.13" dependencies = [ diff --git a/tests/test_chrome.py b/tests/test_chrome.py index 7e7e9b3..0909339 100644 --- a/tests/test_chrome.py +++ b/tests/test_chrome.py @@ -279,3 +279,34 @@ def test_a_service_can_move_both_static_trees_under_one_prefix(tmp_path): ).status_code == 200 # The default prefix must still be empty for services that want it. assert client.get("/chrome/tokens.css").status_code == 404 + + +def test_a_service_can_inject_per_request_context(tmp_path): + """Flask's context processors, without the app object. + + The hook runs per render, not once at startup, because what it returns + depends on who is asking - which group a visitor is in decides whether + a tile is even mentioned. + """ + templates = tmp_path / "templates" + templates.mkdir() + (templates / "index.html").write_text( + '{% extends "base.html" %}{% block content %}
{{ who }}
{% endblock %}' + ) + app = FastAPI() + ui = Chrome(app, service="t", version="1", title="T", templates_dir=templates) + ui.page_context = lambda request: {"who": request.headers.get("x-who", "nobody")} + + @app.get("/") + def page(request: Request): + return ui.render(request, "index.html") + + client = TestClient(app) + assert "alice
" in client.get("/", headers={"x-who": "alice"}).text + assert "nobody
" in client.get("/").text + # An explicit value at the render site beats the hook. + @app.get("/explicit") + def explicit(request: Request): + return ui.render(request, "index.html", who="override") + + assert "override
" in TestClient(app).get("/explicit").text