Decorator that enforces authentication on DRF views, returning 401 if unauthenticated.
# Source: https://github.com/horilla/horilla-hr | License: LGPL-2.1 | Modified files must remain under LGPL-2.1
# Modifications to this file must remain under LGPL-2.1.
# Provided via justclaw.it.com; keep this notice with the code.
"""
Decorators for API views
"""
from functools import wraps
from rest_framework import status
from rest_framework.response import Response
def api_authentication_required(view_func):
"""
Decorator to ensure API views require authentication
"""
@wraps(view_func)
def wrapped_view(request, *args, **kwargs):
if not request.user.is_authenticated:
return Response(
{"detail": "Authentication credentials were not provided."},
status=status.HTTP_401_UNAUTHORIZED,
)
return v
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key