This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/users/shortcuts.py

32 lines
1.0 KiB
Python
Raw Normal View History

from django.http import Http404
2022-11-05 21:17:27 +01:00
from users.models import Domain, Identity
2022-11-05 21:17:27 +01:00
def by_handle_or_404(request, handle, local=True, fetch=False) -> Identity:
2022-11-05 21:17:27 +01:00
"""
Retrieves an Identity by its long or short handle.
Domain-sensitive, so it will understand short handles on alternate domains.
"""
if "@" not in handle:
if "HTTP_HOST" not in request.META:
raise Http404("No hostname available")
username = handle
domain_instance = Domain.get_domain(request.META["HTTP_HOST"])
if domain_instance is None:
raise Http404("No matching domains found")
domain = domain_instance.domain
else:
username, domain = handle.split("@", 1)
# Resolve the domain to the display domain
domain = Domain.get_remote_domain(domain).domain
identity = Identity.by_username_and_domain(
username,
domain,
local=local,
fetch=fetch,
)
if identity is None:
raise Http404(f"No identity for handle {handle}")
return identity