diff --git a/apps/users/api/serializers.py b/apps/users/api/serializers.py index d35c54d..710616a 100644 --- a/apps/users/api/serializers.py +++ b/apps/users/api/serializers.py @@ -128,4 +128,18 @@ class TokenPairSerializer(serializers.Serializer): class RegisterWithPasswordSerializer(serializers.Serializer): mobile = serializers.CharField() - password = serializers.CharField() \ No newline at end of file + password = serializers.CharField() + + +class UserProfileSerializer(BaseModelSerializer): + full_name = serializers.ReadOnlyField() + age = serializers.ReadOnlyField() + + class Meta: + model = User + fields = BaseModelSerializer.Meta.fields + ( + "mobile", "email", "first_name", "last_name", + "description", "profile_picture", "birth_date", + "is_verified", "full_name", "age" + ) + read_only_fields = BaseModelSerializer.Meta.fields + ("mobile", "is_verified") diff --git a/apps/users/api/urls.py b/apps/users/api/urls.py index 705c047..e739b25 100644 --- a/apps/users/api/urls.py +++ b/apps/users/api/urls.py @@ -3,6 +3,8 @@ from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView from apps.users.api import views + + app_name = "users" urlpatterns = [ @@ -16,6 +18,12 @@ urlpatterns = [ path("password/change/", views.ChangePasswordView.as_view(), name="change_password"), path("profile/picture/", views.ProfilePictureView.as_view(), name="profile_picture"), path("list/", views.UserListView.as_view(), name="user_list"), + path('me/', views.UserProfileViewSet.as_view({ + 'get': 'retrieve', + 'put': 'update', + 'patch': 'partial_update', + 'delete': 'destroy' + }), name='user-me'), path("token/obtain/", TokenObtainPairView.as_view(), name="token_obtain_pair"), path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), ] diff --git a/apps/users/api/views.py b/apps/users/api/views.py index bf9348c..d9cd804 100644 --- a/apps/users/api/views.py +++ b/apps/users/api/views.py @@ -9,7 +9,9 @@ from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView from rest_framework_simplejwt.authentication import JWTAuthentication -from rest_framework_simplejwt.tokens import RefreshToken +from rest_framework.mixins import UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin +from rest_framework.viewsets import GenericViewSet + from core.paginations.limit_offset import CustomLimitOffsetPagination @@ -25,6 +27,7 @@ from apps.users.api.serializers import ( LogoutSerializer, TokenPairSerializer, RegisterWithPasswordSerializer, + UserProfileSerializer, ) from apps.users.services.auth import ( register_user_with_password, @@ -235,3 +238,11 @@ class UserListView(ListAPIView): @extend_schema(responses=UserListSerializer(many=True)) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) + + +class UserProfileViewSet(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericViewSet): + serializer_class = UserProfileSerializer + permission_classes = [IsAuthenticated] + + def get_object(self): + return self.request.user