Example #1
0
    def partial_update(self, request, username=None):
        """Partially Updates the driver profile.

        Arguments:
            request: the request data sent by the user, it is used
                     to check the user's permissions and get the data
            username: the username of the driver profile that will be updated

        Returns:
            HTTP 404 Response if driver profile is not found,
            HTTP 400 Response if the data is
            not valid with the errors,
            HTTP 403 Response if the user is not
            authorized to update that profile,
            if not returns HTTP 200 Response with the update JSON data.
        """

        driver_profile = get_object_or_404(DriverProfileModel,
                                           account__username=username)
        self.check_object_permissions(request, driver_profile)
        serializer = DriverProfileSerializer(driver_profile,
                                             data=request.data,
                                             partial=True)
        if serializer.is_valid():
            serializer.save()
            update_session_auth_hash(request, driver_profile.account)
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
    def create(self, request):
        """Creates A new driver profile and Logs it In.

        Checks if user is authenticated if true, return HTTP 401 Response,
        then it Validates the post data if not valid,
        return HTTP 400 Response, then creates the driver and logs him in,
        and returns 201 Response.

        Arguments:
            request: the request data sent by the user, it is used
                     to get the post data from it to get validated and created,
                     and to log the driver in.

        Returns:
            HTTP 400 Response if data is not valid,
            HTTP 401 Response if user is already logged in,
            HTTP 201 Response with the JSON data of the created profile.
        """

        if not request.user.is_authenticated:
            serializer = DriverProfileSerializer(data=request.data)
            if serializer.is_valid():
                driver_profile = serializer.save()
                login(request, driver_profile.account)
                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        return Response(status=status.HTTP_401_UNAUTHORIZED)