Esempio n. 1
0
    def create(self, request):
        """Handle POST operations

        Returns:
            Response -- JSON serialized product category instance
        """
        new_favorite = Favorite()
        customer = Customer.objects.get(user=request.auth.user)
        new_favorite.customer = customer

        seller = Customer.objects.get(pk=request.data["seller_id"])
        new_favorite.seller = seller
        new_favorite.save()

        serializer = FavoriteSerializer(new_favorite,
                                        context={'request': request})

        return Response(serializer.data)
Esempio n. 2
0
    def favoritesellers(self, request):
        """
        @api {GET} /profile/favoritesellers GET favorite sellers
        @apiName GetFavoriteSellers
        @apiGroup UserProfile

        @apiHeader {String} Authorization Auth token
        @apiHeaderExample {String} Authorization
            Token 9ba45f09651c5b0c404f37a2d2572c026c146611

        @apiSuccess (200) {id} id Favorite id
        @apiSuccess (200) {Object} seller Favorited seller
        @apiSuccess (200) {String} seller.url Seller URI
        @apiSuccess (200) {String} seller.phone_number Seller phone number
        @apiSuccess (200) {String} seller.address Seller address
        @apiSuccess (200) {String} seller.user Seller user profile URI
        @apiSuccessExample {json} Success
            [
                {
                    "id": 1,
                    "seller": {
                        "url": "http://localhost:8000/customers/5",
                        "phone_number": "555-1212",
                        "address": "100 Endless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 2,
                    "seller": {
                        "url": "http://localhost:8000/customers/6",
                        "phone_number": "555-1212",
                        "address": "100 Dauntless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 3,
                    "seller": {
                        "url": "http://localhost:8000/customers/7",
                        "phone_number": "555-1212",
                        "address": "100 Indefatiguable Way",
                        "user": "******"
                    }
                }
            ]
        """
        customer = Customer.objects.get(user=request.auth.user)
        if request.method == "GET":
            favorites = Favorite.objects.filter(customer=customer)
            serializer = FavoriteSerializer(
            favorites, many=True, context={'request': request})
            return Response(serializer.data)

        if request.method == "POST":
            try:
                get_favorite_seller_id = Customer.objects.get(pk=request.data["seller"])
                favorite_seller = Favorite()
                favorite_seller.customer = customer
                favorite_seller.seller = get_favorite_seller_id
                favorite_seller.save()
                serializer = FavoriteSerializer(
                favorite_seller, many=False, context={'request': request})            
                return Response(serializer.data)
            except Customer.DoesNotExist as ex:
                return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND)
Esempio n. 3
0
    def favoritesellers(self, request):
        """
        @api {GET} /profile/favoritesellers GET favorite sellers
        @apiName GetFavoriteSellers
        @apiGroup UserProfile

        @apiHeader {String} Authorization Auth token
        @apiHeaderExample {String} Authorization
            Token 9ba45f09651c5b0c404f37a2d2572c026c146611

        @apiSuccess (200) {id} id Favorite id
        @apiSuccess (200) {Object} seller Favorited seller
        @apiSuccess (200) {String} seller.url Seller URI
        @apiSuccess (200) {String} seller.phone_number Seller phone number
        @apiSuccess (200) {String} seller.address Seller address
        @apiSuccess (200) {String} seller.user Seller user profile URI
        @apiSuccessExample {json} Success
            [
                {
                    "id": 1,
                    "seller": {
                        "url": "http://localhost:8000/customers/5",
                        "phone_number": "555-1212",
                        "address": "100 Endless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 2,
                    "seller": {
                        "url": "http://localhost:8000/customers/6",
                        "phone_number": "555-1212",
                        "address": "100 Dauntless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 3,
                    "seller": {
                        "url": "http://localhost:8000/customers/7",
                        "phone_number": "555-1212",
                        "address": "100 Indefatiguable Way",
                        "user": "******"
                    }
                }
            ]
        """
        customer = Customer.objects.get(user=request.auth.user)

        if request.method == "GET":

            favorites = Favorite.objects.filter(customer=customer)

            serializer = FavoriteSerializer(favorites,
                                            many=True,
                                            context={'request': request})
            return Response(serializer.data)

        if request.method == "POST":
            seller_id = self.request.data["seller"]
            if seller_id is not None:
                try:
                    seller = Customer.objects.get(pk=seller_id)
                except Customer.DoesNotExist as ex:
                    return Response({}, status=status.HTTP_204_NO_CONTENT)

                try:
                    favorite = Favorite.objects.get(customer=customer,
                                                    seller=seller)
                except Favorite.DoesNotExist as ex:
                    new_favorite = Favorite()
                    new_favorite.customer = customer
                    new_favorite.seller = seller
                    new_favorite.save()

                    serializer = FavoriteSerializer(
                        new_favorite, context={'request': request})
                    return Response(serializer.data,
                                    status=status.HTTP_201_CREATED)

                serializer = FavoriteSerializer(favorite,
                                                context={'request': request})
                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)

        return Response(status=status.HTTP_400_BAD_REQUEST)
    def favoritesellers(self, request):
        """
        @api {GET} /profile/favoritesellers GET favorite sellers
        @apiName GetFavoriteSellers
        @apiGroup UserProfile

        @apiHeader {String} Authorization Auth token
        @apiHeaderExample {String} Authorization
            Token 9ba45f09651c5b0c404f37a2d2572c026c146611

        @apiSuccess (200) {id} id Favorite id
        @apiSuccess (200) {Object} seller Favorited seller
        @apiSuccess (200) {String} seller.url Seller URI
        @apiSuccess (200) {String} seller.phone_number Seller phone number
        @apiSuccess (200) {String} seller.address Seller address
        @apiSuccess (200) {String} seller.user Seller user profile URI
        @apiSuccessExample {json} Success
            [
                {
                    "id": 1,
                    "seller": {
                        "url": "http://localhost:8000/customers/5",
                        "phone_number": "555-1212",
                        "address": "100 Endless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 2,
                    "seller": {
                        "url": "http://localhost:8000/customers/6",
                        "phone_number": "555-1212",
                        "address": "100 Dauntless Way",
                        "user": "******"
                    }
                },
                {
                    "id": 3,
                    "seller": {
                        "url": "http://localhost:8000/customers/7",
                        "phone_number": "555-1212",
                        "address": "100 Indefatiguable Way",
                        "user": "******"
                    }
                }
            ]
        """
        if request.method == 'GET':

            customer = Customer.objects.get(user=request.auth.user)
            favorites = Favorite.objects.filter(customer=customer)

            serializer = FavoriteSerializer(favorites,
                                            many=True,
                                            context={'request': request})
            return Response(serializer.data)

        if request.method == 'POST':
            customer = Customer.objects.get(user=request.auth.user)
            seller = Customer.objects.get(pk=request.data["seller"])

            fave = Favorite()
            fave.customer = customer
            fave.seller = seller

            if customer != seller:
                try:
                    Favorite.objects.get(customer=customer, seller=seller)
                    return Response({'message': "Already favorited"},
                                    status=status.HTTP_400_BAD_REQUEST)
                except Favorite.DoesNotExist:
                    fave.save()
                    serializer = FavoriteSerializer(
                        fave, many=False, context={'request': request})
                    return Response(serializer.data,
                                    status=status.HTTP_201_CREATED)
            else:
                return Response({},
                                status=status.HTTP_422_UNPROCESSABLE_ENTITY)
Esempio n. 5
0
    def favoritesellers(self, request):

        if request.method == "GET":
            """
            @api {GET} /profile/favoritesellers GET favorite sellers
            @apiName GetFavoriteSellers
            @apiGroup UserProfile

            @apiHeader {String} Authorization Auth token
            @apiHeaderExample {String} Authorization
                Token 9ba45f09651c5b0c404f37a2d2572c026c146611

            @apiSuccess (200) {id} id Favorite id
            @apiSuccess (200) {Object} seller Favorited seller
            @apiSuccess (200) {String} seller.url Seller URI
            @apiSuccess (200) {String} seller.phone_number Seller phone number
            @apiSuccess (200) {String} seller.address Seller address
            @apiSuccess (200) {String} seller.user Seller user profile URI
            @apiSuccessExample {json} Success
                [
                    {
                        "id": 1,
                        "seller": {
                            "url": "http://localhost:8000/customers/5",
                            "phone_number": "555-1212",
                            "address": "100 Endless Way",
                            "user": "******"
                        }
                    },
                    {
                        "id": 2,
                        "seller": {
                            "url": "http://localhost:8000/customers/6",
                            "phone_number": "555-1212",
                            "address": "100 Dauntless Way",
                            "user": "******"
                        }
                    },
                    {
                        "id": 3,
                        "seller": {
                            "url": "http://localhost:8000/customers/7",
                            "phone_number": "555-1212",
                            "address": "100 Indefatiguable Way",
                            "user": "******"
                        }
                    }
                ]
            """
            customer = Customer.objects.get(user=request.auth.user)
            favorites = Favorite.objects.filter(customer=customer)

            serializer = FavoriteSerializer(favorites,
                                            many=True,
                                            context={'request': request})
            return Response(serializer.data)

        if request.method == "POST":
            """
            @api {POST} /profile/favoritesellers POST new favorite seller
            @apiName AddToFavoriteSellers
            @apiGroup UserProfile

            @apiHeader {String} Authorization Auth token
            @apiHeaderExample {String} Authorization
                Token 9ba45f09651c5b0c404f37a2d2572c026c146611

            @apiSuccessExample {json} Success
                HTTP/1.1 204 No Content
            """
            try:
                customer = Customer.objects.get(user=request.auth.user)
                seller = Customer.objects.get(pk=request.data["seller"])

            except Customer.DoesNotExist as ex:
                return Response({'message': ex.args[0]},
                                status=status.HTTP_404_NOT_FOUND)

            new_favorite = Favorite()
            new_favorite.customer = customer
            new_favorite.seller = seller

            try:
                new_favorite.save()
            except IntegrityError:
                return Response(
                    {'message': "You have already favorited that seller."},
                    status=status.HTTP_400_BAD_REQUEST)

            return Response({}, status=status.HTTP_204_NO_CONTENT)