feat(users): add /me/ endpoint for detail/update/delete user object
This commit is contained in:
@@ -128,4 +128,18 @@ class TokenPairSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
class RegisterWithPasswordSerializer(serializers.Serializer):
|
class RegisterWithPasswordSerializer(serializers.Serializer):
|
||||||
mobile = serializers.CharField()
|
mobile = serializers.CharField()
|
||||||
password = serializers.CharField()
|
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")
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
|
|||||||
|
|
||||||
from apps.users.api import views
|
from apps.users.api import views
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app_name = "users"
|
app_name = "users"
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@@ -16,6 +18,12 @@ urlpatterns = [
|
|||||||
path("password/change/", views.ChangePasswordView.as_view(), name="change_password"),
|
path("password/change/", views.ChangePasswordView.as_view(), name="change_password"),
|
||||||
path("profile/picture/", views.ProfilePictureView.as_view(), name="profile_picture"),
|
path("profile/picture/", views.ProfilePictureView.as_view(), name="profile_picture"),
|
||||||
path("list/", views.UserListView.as_view(), name="user_list"),
|
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/obtain/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
|
||||||
path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
|
path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ from rest_framework.permissions import AllowAny, IsAuthenticated
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
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
|
from core.paginations.limit_offset import CustomLimitOffsetPagination
|
||||||
|
|
||||||
@@ -25,6 +27,7 @@ from apps.users.api.serializers import (
|
|||||||
LogoutSerializer,
|
LogoutSerializer,
|
||||||
TokenPairSerializer,
|
TokenPairSerializer,
|
||||||
RegisterWithPasswordSerializer,
|
RegisterWithPasswordSerializer,
|
||||||
|
UserProfileSerializer,
|
||||||
)
|
)
|
||||||
from apps.users.services.auth import (
|
from apps.users.services.auth import (
|
||||||
register_user_with_password,
|
register_user_with_password,
|
||||||
@@ -235,3 +238,11 @@ class UserListView(ListAPIView):
|
|||||||
@extend_schema(responses=UserListSerializer(many=True))
|
@extend_schema(responses=UserListSerializer(many=True))
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
return super().get(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
|
||||||
|
|||||||
Reference in New Issue
Block a user