Add user data retreival viewset

This commit is contained in:
sosokker 2023-11-27 14:17:37 +07:00
parent 92731815da
commit fcff22e64d
2 changed files with 16 additions and 4 deletions

View File

@ -1,7 +1,8 @@
from django.urls import path
from users.views import CustomUserCreate, CustomUserProfileUpdate
from users.views import CustomUserCreate, CustomUserProfileUpdate, UserDataRetriveViewset
urlpatterns = [
path('user/create/', CustomUserCreate.as_view(), name="create_user"),
path('user/update/', CustomUserProfileUpdate.as_view(), name='update_user')
path('user/update/', CustomUserProfileUpdate.as_view(), name='update_user'),
path('user/data/', UserDataRetriveViewset.as_view({'get': 'retrieve'}), name="get_user_data"),
]

View File

@ -1,6 +1,6 @@
"""This module defines API views for user creation"""
from rest_framework import status
from rest_framework import status, viewsets, mixins
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
@ -61,4 +61,15 @@ class CustomUserProfileUpdate(APIView):
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class UserDataRetriveViewset(viewsets.GenericViewSet, mixins.RetrieveModelMixin):
queryset = CustomUser.objects.all()
permission_classes = (IsAuthenticated,)
serializer_class = UpdateProfileSerializer
def retrieve(self, request, *args, **kwargs):
serializer = self.get_serializer(request.user)
return Response(serializer.data)