Coverage for apps / profiles / utils.py: 73%
30 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 00:40 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-11 00:40 +0000
1"""Profile utilities."""
3from asgiref.sync import sync_to_async
4from django.http import Http404
6from .models import Profile
9def get_current_profile(request):
10 """
11 Get the current profile from the session.
13 Raises Http404 if no profile is selected.
14 """
15 profile_id = request.session.get('profile_id')
16 if not profile_id:
17 raise Http404('No profile selected')
19 try:
20 return Profile.objects.get(id=profile_id)
21 except Profile.DoesNotExist:
22 raise Http404('Profile not found')
25def get_current_profile_or_none(request):
26 """
27 Get the current profile from the session, or None if not set.
29 Use this for endpoints that work with or without a profile,
30 but need to apply profile-based filtering when one is present.
31 """
32 profile_id = request.session.get('profile_id')
33 if not profile_id:
34 return None
36 try:
37 return Profile.objects.get(id=profile_id)
38 except Profile.DoesNotExist:
39 return None
42async def aget_current_profile_or_none(request):
43 """
44 Async version of get_current_profile_or_none.
46 Use this in async views/endpoints.
47 """
48 @sync_to_async
49 def _get_profile():
50 profile_id = request.session.get('profile_id')
51 if not profile_id:
52 return None
53 try:
54 return Profile.objects.get(id=profile_id)
55 except Profile.DoesNotExist:
56 return None
58 return await _get_profile()