from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from .serializers import UserEditSerializer, UserSerializer # Create your views here. 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)