Allow LAN admin access and add a campaign mint form (#11).
CI / test (pull_request) Successful in 6s
CI / test (pull_request) Successful in 6s
Serve /admin/ on 10.0.0.128 so it can be used from another machine on the network, and mint tracked short URLs from domain/campaign/source/metric.
This commit is contained in:
+35
-1
@@ -7,7 +7,7 @@ import hmac
|
||||
import logging
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
from urllib.parse import urlencode, urlsplit, urlunsplit
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import IntegrityError
|
||||
@@ -45,6 +45,40 @@ def host_allowed(hostname: str, allowed: list[str]) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def normalize_destination_host(raw: str) -> str:
|
||||
"""Strip scheme/path from a domain field. Raise ValidationError if empty."""
|
||||
raw = (raw or "").strip()
|
||||
if not raw:
|
||||
raise ValidationError("invalid url")
|
||||
if raw.startswith("//"):
|
||||
raise ValidationError("invalid url")
|
||||
if "://" not in raw:
|
||||
raw = "https://" + raw
|
||||
try:
|
||||
parts = urlsplit(raw)
|
||||
except ValueError as exc:
|
||||
raise ValidationError("invalid url") from exc
|
||||
hostname = (parts.hostname or "").lower().rstrip(".")
|
||||
if not hostname:
|
||||
raise ValidationError("invalid url")
|
||||
if parts.username or parts.password:
|
||||
raise ValidationError("invalid url")
|
||||
return hostname
|
||||
|
||||
|
||||
def build_tracked_url(*, domain: str, campaign: str, source: str, metric: str) -> str:
|
||||
"""Build an allowlisted https URL with UTM query params."""
|
||||
hostname = normalize_destination_host(domain)
|
||||
query = urlencode(
|
||||
{
|
||||
"utm_campaign": campaign.strip(),
|
||||
"utm_source": source.strip(),
|
||||
"utm_medium": metric.strip(),
|
||||
}
|
||||
)
|
||||
return validate_target_url(urlunsplit(("https", hostname, "/", query, "")))
|
||||
|
||||
|
||||
def validate_target_url(raw: str) -> str:
|
||||
"""Return a canonical https URL or raise ValidationError."""
|
||||
if not raw or not isinstance(raw, str):
|
||||
|
||||
Reference in New Issue
Block a user