Files
aerbim-ht-monitor/backend/api/account/views/UserDataView.py

62 lines
2.6 KiB
Python

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema, OpenApiResponse, OpenApiExample
from api.auth.serializers import UserResponseSerializer
from api.models import UserProfile
from api.utils.decorators import handle_exceptions
@extend_schema(tags=['Профиль'])
class UserDataView(APIView):
"""View для получения данных текущего пользователя"""
permission_classes = [IsAuthenticated]
serializer_class = UserResponseSerializer
@extend_schema(
summary="Получение данных пользователя",
description="Получение данных авторизованного пользователя для инициализации клиентского состояния",
responses={
200: OpenApiResponse(
response=UserResponseSerializer,
description="Данные пользователя успешно получены",
examples=[
OpenApiExample(
'Успешный ответ',
value={
"id": 1,
"email": "user@example.com",
"account_type": "engineer",
"name": "Иван",
"surname": "Иванов",
"imageURL": "https://example.com/avatar.jpg",
"uuid": "abc123"
}
)
]
),
404: OpenApiResponse(
description="Профиль пользователя не найден",
examples=[
OpenApiExample(
'Профиль не найден',
value={"error": "Профиль пользователя не найден"}
)
]
)
}
)
@handle_exceptions
def get(self, request):
"""Получение данных текущего пользователя"""
try:
user_data = UserResponseSerializer(request.user).data
return Response(user_data, status=status.HTTP_200_OK)
except UserProfile.DoesNotExist:
return Response(
{"error": "Профиль пользователя не найден"},
status=status.HTTP_404_NOT_FOUND
)