2022-11-06 20:48:04 +00:00
|
|
|
from django.http import Http404
|
2022-11-05 20:17:27 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2022-11-06 20:48:04 +00:00
|
|
|
from users.models import Domain, Identity
|
2022-11-05 20:17:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def by_handle_or_404(request, handle, local=True):
|
|
|
|
"""
|
|
|
|
Retrieves an Identity by its long or short handle.
|
|
|
|
Domain-sensitive, so it will understand short handles on alternate domains.
|
|
|
|
"""
|
|
|
|
if "@" not in handle:
|
2022-11-06 20:48:04 +00:00
|
|
|
if "HTTP_HOST" not in request.META:
|
|
|
|
raise Http404("No hostname available")
|
|
|
|
username = handle
|
|
|
|
domain_instance = Domain.get_local_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)
|
2022-11-05 20:17:27 +00:00
|
|
|
if local:
|
2022-11-06 20:48:04 +00:00
|
|
|
return get_object_or_404(
|
|
|
|
Identity.objects.filter(local=True),
|
|
|
|
username=username,
|
|
|
|
domain_id=domain,
|
|
|
|
)
|
2022-11-05 20:17:27 +00:00
|
|
|
else:
|
2022-11-06 20:48:04 +00:00
|
|
|
return get_object_or_404(
|
|
|
|
Identity,
|
|
|
|
username=username,
|
|
|
|
domain_id=domain,
|
|
|
|
)
|