This repository has been archived on 2026-02-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
shared/browser/app/utils/utils.py
giles ef806f8fbb feat: extract shared infrastructure from shared_lib
Phase 1-3 of decoupling plan:
- Shared DB, models, infrastructure, browser, config, utils
- Event infrastructure (domain_events outbox, bus, processor)
- Structured logging
- Generic container concept (container_type/container_id)
- Alembic migrations for all schema changes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 12:45:56 +00:00

52 lines
1.3 KiB
Python

from quart import (
Response,
request,
g,
)
from shared.utils import host_url
from urllib.parse import urlencode
def current_route_relative_path() -> str:
"""
Returns the current request path relative to the app's mount point (script_root).
"""
(request.script_root or "").rstrip("/")
path = request.path # excludes query string
if g.root and path.startswith(f"/{g.root}"):
rel = path[len(g.root+1):]
return rel if rel.startswith("/") else "/" + rel
return path # app at /
def current_url_without_page() -> str:
"""
Build current URL (host+path+qs) but with ?page= removed.
Used for Hx-Push-Url.
"""
base = host_url(current_route_relative_path())
params = request.args.to_dict(flat=False) # keep multivals
params.pop("page", None)
qs = urlencode(params, doseq=True)
return f"{base}?{qs}" if qs else base
def vary(resp: Response) -> Response:
"""
Ensure caches/CDNs vary on HX headers so htmx/non-htmx versions don't get mixed.
"""
v = resp.headers.get("Vary", "")
parts = [p.strip() for p in v.split(",") if p.strip()]
for h in ("HX-Request", "X-Origin"):
if h not in parts:
parts.append(h)
if parts:
resp.headers["Vary"] = ", ".join(parts)
return resp