CI / test (pull_request) Successful in 6s
Standalone Django service so callers can mint links and phones get a 302. Closes #1.
32 lines
821 B
Python
32 lines
821 B
Python
"""Public redirect — no auth. SMS recipients tap GET /<code>."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from django.http import Http404, HttpResponseRedirect
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
from links.models import ShortLink
|
|
from links.services import record_click_best_effort
|
|
|
|
CODE_RE = re.compile(r"^[a-z0-9]{4,8}$")
|
|
|
|
|
|
@require_http_methods(["GET", "HEAD"])
|
|
def redirect_view(request, code: str):
|
|
if not CODE_RE.fullmatch(code):
|
|
raise Http404()
|
|
|
|
try:
|
|
link = ShortLink.objects.get(code=code)
|
|
except ShortLink.DoesNotExist as exc:
|
|
raise Http404() from exc
|
|
|
|
if not link.is_available():
|
|
raise Http404()
|
|
|
|
if request.method == "GET":
|
|
record_click_best_effort(request, link)
|
|
return HttpResponseRedirect(link.target_url)
|