feat / AEB-26 login page
This commit is contained in:
0
backend/api/account/__init__.py
Normal file
0
backend/api/account/__init__.py
Normal file
30
backend/api/account/serializers/UserSerializer.py
Normal file
30
backend/api/account/serializers/UserSerializer.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from rest_framework import serializers
|
||||
from django.conf import settings
|
||||
|
||||
class UserResponseSerializer(serializers.Serializer):
|
||||
id = serializers.IntegerField()
|
||||
email = serializers.EmailField()
|
||||
name = serializers.CharField(source='first_name')
|
||||
surname = serializers.CharField(source='last_name')
|
||||
image = serializers.SerializerMethodField()
|
||||
uuid = serializers.SerializerMethodField()
|
||||
account_type = serializers.CharField(source='userprofile.account_type')
|
||||
|
||||
def get_uuid(self, obj):
|
||||
try:
|
||||
return str(obj.userprofile.uuid)[:6]
|
||||
except Exception as e:
|
||||
return None
|
||||
|
||||
def get_image(self, obj):
|
||||
try:
|
||||
if obj.userprofile.image:
|
||||
relative_url = obj.userprofile.image.url
|
||||
base_url = settings.BASE_URL
|
||||
base_url = base_url.rstrip('/')
|
||||
relative_url = relative_url.lstrip('/')
|
||||
full_url = f"{base_url}/{relative_url}"
|
||||
return full_url
|
||||
return None
|
||||
except Exception as e:
|
||||
return None
|
||||
0
backend/api/account/serializers/__init__.py
Normal file
0
backend/api/account/serializers/__init__.py
Normal file
24
backend/api/account/urls.py
Normal file
24
backend/api/account/urls.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from django.urls import path
|
||||
from .views.UserDataView import UserDataView
|
||||
from drf_spectacular.views import (
|
||||
SpectacularAPIView,
|
||||
SpectacularSwaggerView,
|
||||
SpectacularRedocView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path('schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||
path(
|
||||
'docs/',
|
||||
SpectacularSwaggerView.as_view(url_name='schema'),
|
||||
name='swagger-ui',
|
||||
),
|
||||
# ReDoc UI - альтернативный вариант отображения доков:
|
||||
path(
|
||||
'redoc/',
|
||||
SpectacularRedocView.as_view(url_name='schema'),
|
||||
name='redoc',
|
||||
),
|
||||
|
||||
path("user/", UserDataView.as_view(), name="user-data"),
|
||||
]
|
||||
62
backend/api/account/views/UserDataView.py
Normal file
62
backend/api/account/views/UserDataView.py
Normal file
@@ -0,0 +1,62 @@
|
||||
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": "engieneer",
|
||||
"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
|
||||
)
|
||||
3
backend/api/account/views/__init__.py
Normal file
3
backend/api/account/views/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Логика работы с аккаунтом пользователя
|
||||
"""
|
||||
Reference in New Issue
Block a user