def post(self, request, *args, **kwargs): """ Function to handle POST requests. Creates a new user. """ logger.info(f"Request: {request}. args: {args}, kwargs: {kwargs}") serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() username = serializer.data.get("username") logger.info(f"User with username: {username} has been created.") return Response(serializer.data, status=status.HTTP_201_CREATED) logger.info(f"Bad request with errors: {serializer.errors}") return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, user_id): user = User.objects.get(pk=user_id) profile = UserProfile.objects.get(user_id=user_id) profileSerializer = UserSerializer( profile, data={ 'first_name': request.data.get('profile').get('first_name'), 'last_name': request.data.get('profile').get('last_name'), 'phone_number': request.data.get('profile').get('phone_number'), 'age': request.data.get('profile').get('age'), 'gender': request.data.get('profile').get('gender') }, partial=True) profileSerializer.is_valid(raise_exception=True) profileSerializer.save() if request.data.get('password'): serializer = self.serializer_class(user, data={ 'email': request.data.get('email'), 'password': request.data.get('password') }, partial=True) else: serializer = self.serializer_class( user, data={'email': request.data.get('email')}, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response( { 'success': 'Updated Successfully!', 'data': serializer.data }, status=status.HTTP_200_OK)