Django rest framework serializer.save() not saving to database and shows ConnectionAbortedError:

195 Views Asked by At

I'm trying to PUT some data into UserAccount model in order to reset the password without using web template(for a mobile app). Here the model

class UserAccount(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_password_reset_confirmed = models.BooleanField(default=False)
    password_reset_token = models.CharField(max_length=255, default="")

    objects = UserAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def get_full_name(self):
        return self.name
    
    def get_short_name(self):
        return self.name
    
    def __str__(self):
        return self.email

The data I send won't save. But returns the Success response 204. Heres my serializer.py

class UserAccountPasswordResetConfirmationSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserAccount
        fields = ['is_password_reset_confirmed', 'password_reset_token']

views.py

class UserPasswordResetConfirmView(APIView):

    def get(self, request, uid, token, format=None):
        payload = {
            'is_confirmed': True,
            'token': token
        }

        url = 'http://localhost:8000/password/reset/confirmed/{}/'.format(smart_str(urlsafe_base64_decode(uid)))
        response = requests.put(url, data=payload)

        if response.status_code == 204:
            return Response(response.content)
        else:
            return Response({"Error": "Failed to Confirm"})


class UserPasswordResetView(APIView):

    def get_object(self, pk):
        try:
            return UserAccount.objects.get(pk=pk)
        except UserAccount.DoesNotExist:
            raise Response(status=status.HTTP_404_NOT_FOUND)

    def put(self, request, pk):
        user = self.get_object(pk)
        # print("Data", request.data)
        serializer = UserAccountPasswordResetConfirmationSerializer(user, data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I checked whether the data are passing to UserPasswordResetView from UserPasswordResetConfirmView using print statements(^.^') and there's no problem in that.

 Data <QueryDict: {'is_confirmed': ['True'], 'token': ['5m0-60f758fc23628d143158']}>

But the response.content returns "" and the data won't update.

Addition to that console indicates,

[30/Nov/2020 14:56:05] "PUT /password/reset/confirmed/1/ HTTP/1.1" 204 63
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 64019)
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Program Files\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\Ozunu\AppData\Roaming\Python\Python37\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\Ozunu\AppData\Roaming\Python\Python37\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Program Files\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
[30/Nov/2020 14:56:05] "GET /password/reset/confirm/MQ/5m0-60f758fc23628d143158/ HTTP/1.1" 200 5540

Btw I'm using MongoDB and djoser. Why is this happening and any help will be greatly appreciated.


Edit:

I've tested the endpoint of the UserPasswordResetView(APIView) and it does the updates to the relevant record.


Edit

I was able to solve the not saving issue by converting

<QueryDict: {'is_confirmed': ['True'], 'token': ['5m0-60f758fc23628d143158']}>

to a dictionary

{'is_confirmed': 'True', 'token': '5m0-60f758fc23628d143158'}

but unable to solve the ConnectionAbortedError: error.

0

There are 0 best solutions below