This commit is contained in:
nvrl committed 2026-04-27 03:36:35 +02:00
1 parent 2b2b4cf48f
commit 22fe0674dd
22 files changed
+620 -286

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
"""Auth helpers for narrowing request.user to the concrete User type.
DRF's `request.user` is typed as `User | AnonymousUser`. After a view enforces
`IsAuthenticated`, the union is misleading: every code path past the permission
check has the concrete user. These helpers express that narrowing for mypy and
keep view bodies readable.
"""
from __future__ import annotations
from rest_framework.exceptions import NotAuthenticated
from rest_framework.request import Request
from api.models import User
def current_user(request: Request) -> User:
"""Return the authenticated User for `request` or raise 401.
Use after `IsAuthenticated` permission. The check is defensive — the
permission class already rejected anonymous calls.
"""
user = request.user
if not isinstance(user, User):
raise NotAuthenticated
return user