Generateurv2/backend/api/users/views.py

32 lines
1.3 KiB
Python
Raw Normal View History

2022-06-11 23:39:03 +02:00
from django.shortcuts import render
2022-06-24 13:42:16 +02:00
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
2022-06-11 23:39:03 +02:00
2022-06-24 13:42:16 +02:00
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from .serializers import UserEditSerializer, UserSerializer
2022-06-11 23:39:03 +02:00
# Create your views here.
2022-06-24 13:42:16 +02:00
class UsersAPI(APIView):
@method_decorator(login_required)
def get(self, request, format = None):
print(UserSerializer(request.user).data)
return Response({**UserSerializer(request.user).data}, status= status.HTTP_200_OK)
@method_decorator(login_required)
def put(self, request, format = None):
first_name = request.data.get('first_name')
email = request.data.get('email')
username = request.data.get('username')
last_name = request.data.get('last_name')
serial = UserEditSerializer(data={"first_name": first_name, 'email':email, 'username': username, 'last_name': last_name}, instance=request.user)
if serial.is_valid():
request.user.email = email
request.user.last_name = last_name
request.user.first_name = first_name
request.user.username = username
request.user.save()
return Response({**UserSerializer(request.user).data}, status= status.HTTP_200_OK)
return Response({'errors': serial.errors}, status = status.HTTP_400_BAD_REQUEST)