accounts: add user profile and user update api
This commit is contained in:
28
core/apps/accounts/serializers/user.py
Normal file
28
core/apps/accounts/serializers/user.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.accounts.models import User
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
'id', 'phone', 'indentification_num', 'profile_image', 'first_name', 'last_name', 'email'
|
||||
]
|
||||
|
||||
|
||||
class UserUpdateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
'indentification_num', 'profile_image', 'first_name', 'last_name', 'email'
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
instance.indentification_num = validated_data.get('indentification_num', instance.indentification_num)
|
||||
instance.profile_image = validated_data.get('profile_image', instance.profile_image)
|
||||
instance.first_name = validated_data.get('first_name', instance.first_name)
|
||||
instance.last_name = validated_data.get('last_name', instance.last_name)
|
||||
instance.email = validated_data.get('email', instance.email)
|
||||
instance.save()
|
||||
return instance
|
||||
@@ -2,6 +2,7 @@ from django.urls import path, include
|
||||
|
||||
from core.apps.accounts.views.auth import LoginApiView, RegisterApiView, ConfirUserApiView, ChoiceUserRoleApiView, SearchUserPhoneApiView
|
||||
from core.apps.accounts.views.forgot_password import ConfirmCodeApiView, SendCodeApiView, ResetPasswordApiView
|
||||
from core.apps.accounts.views.user import UserProfileApiView, UserProfileUpdateApiView
|
||||
|
||||
urlpatterns = [
|
||||
path('auth/', include(
|
||||
@@ -15,6 +16,8 @@ urlpatterns = [
|
||||
path('user/', include(
|
||||
[
|
||||
path('<str:number>/search/', SearchUserPhoneApiView.as_view()),
|
||||
path('profile/', UserProfileApiView.as_view()),
|
||||
path('profile/update/', UserProfileUpdateApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
path('forgot_password/', include(
|
||||
|
||||
29
core/apps/accounts/views/user.py
Normal file
29
core/apps/accounts/views/user.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from rest_framework import generics, views
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.accounts.serializers import user as serializers
|
||||
from core.apps.accounts.models import User
|
||||
|
||||
|
||||
class UserProfileApiView(views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
user = request.user
|
||||
serializer = serializers.UserSerializer(user)
|
||||
return Response(serializer.data, status=200)
|
||||
|
||||
|
||||
class UserProfileUpdateApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.UserUpdateSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
queryset = User.objects.all()
|
||||
|
||||
def patch(self, request):
|
||||
user = request.user
|
||||
serializer = self.serializer_class(data=request.data, instance=user)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
data = serializer.save()
|
||||
return Response(serializers.UserSerializer(data).data, status=200)
|
||||
return Response(serializer.errors, status=400)
|
||||
Reference in New Issue
Block a user