def put(self, request, pk, format=None): user = self.get_object(pk=pk) serializer = UserUpdateSerializer(user, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, pk): """PATCH method for editing user's name and password""" instance = User.objects.get(pk=pk) serializer = UserUpdateSerializer(instance, data=request.data, partial=True) if not serializer.is_valid(): return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: new_user = serializer.save() except: return Response(data={'error': sys.exc_info()[0]}, status=status.HTTP_400_BAD_REQUEST) serializer = UserSerializer(new_user) return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED)
def put(request): serializer = UserUpdateSerializer(request.user, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(status=status.HTTP_200_OK)
def put(self, request, pk, format=None): """ API endpoint that update the users. --- Body example: ``` { "username": "******", "email": "*****@*****.**", "password": "******" } ``` Response example: ``` { "id": 1, "username": "******", "email": "*****@*****.**", "profile": { "id": 1, "hours": 5, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 10, "photo": null, "coins": 0 } } ``` """ user = self.get_object(pk=pk) serializer = UserUpdateSerializer(user, data=request.data) if serializer.is_valid(): serializer.save() # profile_data = request.data['profile'] # abilities_data = profile_data.pop('abilities') # profile = Profile.objects.filter(user=user).update( # **profile_data # ) # profile = Profile.objects.get(user=user) # profile.abilities.clear() # for abilitiest in abilities_data: # abilitiest = Abilities.objects.filter(id=abilitiest['id']) # profile.abilities.add(*abilitiest) # profile.save() # profile_serializer = ProfileUpdateSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)