healthchecks_healthchecks/hc/lib/urls.py
Pēteris Caune 7836da6448
Update settings.py to allow subpath in SITE_ROOT
If SITE_ROOT contains a subpath
(e.g., "http://example.org/healthchecks"), settings.py now adjusts
FORCE_SCRIPT_NAME and STATIC_URL.

Also, hc.lib.urls.absolute_reverse() now recognizes subpath in
SITE_ROOT, and makes sure it does not show up twice in the
generated URLs.

cc: #1091
2024-12-04 10:37:52 +02:00

19 lines
592 B
Python

from collections.abc import Callable, Sequence
from typing import Any
from urllib.parse import urlparse
from django.conf import settings
from django.http.response import HttpResponse
from django.urls import reverse
def absolute_url(path: str) -> str:
subpath = urlparse(settings.SITE_ROOT).path
return settings.SITE_ROOT.removesuffix(subpath) + path
def absolute_reverse(
viewname: str | Callable[..., HttpResponse], args: Sequence[Any] | None = None
) -> str:
"""Generate absolute URL (starting with http[s]://)."""
return absolute_url(reverse(viewname, args=args))