Esempio n. 1
0
    def post(self, request, format=None):
        v_ser = self.add_voucher_serializer_class(data=request.data,
                                                  context={'request': request})
        if v_ser.is_valid():
            basket = operations.get_basket(request)

            voucher = v_ser.instance
            basket.vouchers.add(voucher)

            signals.voucher_addition.send(sender=None,
                                          basket=basket,
                                          voucher=voucher)

            # Recalculate discounts to see if the voucher gives any
            operations.apply_offers(request, basket)
            discounts_after = basket.offer_applications

            # Look for discounts from this new voucher
            for discount in discounts_after:
                if discount['voucher'] and discount['voucher'] == voucher:
                    break
            else:
                basket.vouchers.remove(voucher)
                return Response(
                    {
                        'reason':
                        _("Your basket does not qualify for a voucher discount"
                          )
                    },  # noqa
                    status=status.HTTP_406_NOT_ACCEPTABLE)

            ser = self.serializer_class(voucher, context={'request': request})
            return Response(ser.data)

        return Response(v_ser.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 2
0
    def post(self, request, format=None):
        v_ser = self.add_voucher_serializer_class(
            data=request.data, context={'request': request})
        if v_ser.is_valid():
            basket = operations.get_basket(request)

            voucher = v_ser.instance
            basket.vouchers.add(voucher)

            signals.voucher_addition.send(
                sender=None, basket=basket, voucher=voucher)

            # Recalculate discounts to see if the voucher gives any
            operations.apply_offers(request, basket)
            discounts_after = basket.offer_applications

            # Look for discounts from this new voucher
            for discount in discounts_after:
                if discount['voucher'] and discount['voucher'] == voucher:
                    break
            else:
                basket.vouchers.remove(voucher)
                return Response(
                    {'reason': _(
                        "Your basket does not qualify for a voucher discount")},  # noqa
                    status=status.HTTP_406_NOT_ACCEPTABLE)

            ser = self.serializer_class(
                voucher, context={'request': request})
            return Response(ser.data)

        return Response(v_ser.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 3
0
    def post(self, request, *args, **kwargs):  # pylint: disable=redefined-builtin
        p_ser = self.add_product_serializer_class(
            data=request.data, context={"request": request}
        )
        if p_ser.is_valid():
            basket = operations.get_basket(request)
            product = p_ser.validated_data["url"]
            quantity = p_ser.validated_data["quantity"]
            options = p_ser.validated_data.get("options", [])

            basket_valid, message = self.validate(basket, product, quantity, options)
            if not basket_valid:
                return Response(
                    {"reason": message}, status=status.HTTP_406_NOT_ACCEPTABLE
                )

            basket.add_product(product, quantity=quantity, options=options)

            signals.basket_addition.send(
                sender=self, product=product, user=request.user, request=request
            )

            operations.apply_offers(request, basket)
            ser = self.serializer_class(basket, context={"request": request})
            return Response(ser.data)

        return Response({"reason": p_ser.errors}, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 4
0
    def post(self, request, format=None):
        '''
            return a basket info refer to oscar api with a little modification
            {
                "id": 1,
                "owner": user_id,
                "status": "Open",
                "lines": [
                    {...}, 
                    {...}, ...
                ],
                "total_excl_tax": "59.96",
                "total_excl_tax_excl_discounts": "66.95",
                "total_incl_tax": "59.96",
                "total_incl_tax_excl_discounts": "66.95",
                "total_tax": "0.00",
                "currency": "CNY",
                "voucher_discounts": [],
                "offer_discounts": [
                    {
                        "description": null,
                        "name": "Normal site offer",
                        "amount": "6.99"
                    }
                ],
                "is_tax_known": true
            } 
        '''
        ser = AssetLocationReserveSerializer(data=request.data)
        ser.is_valid(raise_exception=True)
        #     3. empty(flush) the basket and add a stockrecord basketline to the basket, apply all the offers
        #     4. return the basket info and display it
        options = []
        product = None
        quantity = 1
        sku_code_key = 'sku_code'

        for k, v in ser.validated_data.items():
            if k == sku_code_key:
                product = get_object_or_404(Product,
                                            stockrecords__partner_sku=v)
                product.sku_code = v
            option = get_object_or_404(ProductOption, code=k)
            options.append({
                'option': option,
                'value': v,
            })

        basket = operations.get_basket(request)
        basket.flush()
        basket_valid, message = self.validate(basket, product, quantity,
                                              options)
        if not basket_valid:
            return Response({'detail': message},
                            status=status.HTTP_406_NOT_ACCEPTABLE)
        basket.add_product(product, quantity=1, options=options)
        operations.apply_offers(request, basket)
        ser = self.serializer_class(basket, context={'request': request})
        return Response(ser.data)
Esempio n. 5
0
def add_product(request, format=None):
    """
    Add a certain quantity of a product to the basket.

    POST(url, quantity)
    {
        "url": "http://testserver.org/oscarapi/products/209/",
        "quantity": 6
    }

    NOT IMPLEMENTED: LineAttributes, which are references to catalogue.Option.
    To Implement make the serializer accept lists of option object, which look
    like this:
    {
        option: "http://testserver.org/oscarapi/options/1/,
        value: "some value"
    },
    These should be passed to basket.add_product as a list of dictionaries.
    """
    p_ser = serializers.AddProductSerializer(data=request.DATA,
                                             context={'request': request})
    if p_ser.is_valid():
        basket = get_basket(request)
        product = p_ser.object
        quantity = p_ser.init_data.get('quantity')
        availability = basket.strategy.fetch_for_product(product).availability

        # check if product is available at all
        if not availability.is_available_to_buy:
            return Response({'reason': availability.message},
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        # check if we can buy this quantity
        allowed, message = availability.is_purchase_permitted(quantity)
        if not allowed:
            return Response({'reason': message},
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        # check if there is a limit on amount
        allowed, message = basket.is_quantity_allowed(quantity)
        if not allowed:
            return Response({'reason': message},
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        basket.add_product(p_ser.object, quantity=quantity)
        apply_offers(request, basket)
        ser = serializers.BasketSerializer(basket,
                                           context={'request': request})
        return Response(ser.data)

    return Response({'reason': p_ser.errors},
                    status=status.HTTP_406_NOT_ACCEPTABLE)
def add_product(request, format=None):
    """
    Add a certain quantity of a product to the basket.

    POST(url, quantity)
    {
        "url": "http://testserver.org/oscarapi/products/209/",
        "quantity": 6
    }

    NOT IMPLEMENTED: LineAttributes, which are references to catalogue.Option.
    To Implement make the serializer accept lists of option object, which look
    like this:
    {
        option: "http://testserver.org/oscarapi/options/1/,
        value: "some value"
    },
    These should be passed to basket.add_product as a list of dictionaries.
    """
    p_ser = serializers.AddProductSerializer(data=request.DATA,
                                             context={'request': request})
    if p_ser.is_valid():
        basket = get_basket(request)
        product = p_ser.object
        quantity = p_ser.init_data.get('quantity')
        availability = basket.strategy.fetch_for_product(product).availability

        # check if product is available at all
        if not availability.is_available_to_buy:
            return Response(
                {'reason': availability.message}, status=status.HTTP_406_NOT_ACCEPTABLE)

        # check if we can buy this quantity
        allowed, message = availability.is_purchase_permitted(quantity)
        if not allowed:
            return Response({'reason': message}, status=status.HTTP_406_NOT_ACCEPTABLE)

        # check if there is a limit on amount
        allowed, message = basket.is_quantity_allowed(quantity)
        if not allowed:
            return Response({'reason': message}, status=status.HTTP_406_NOT_ACCEPTABLE)

        basket.add_product(p_ser.object, quantity=quantity)
        apply_offers(request, basket)
        ser = serializers.BasketSerializer(
            basket,  context={'request': request})
        return Response(ser.data)

    return Response({'reason': p_ser.errors}, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 7
0
def add_voucher(request, format=None):
    """
    Add a voucher to the basket.

    POST(vouchercode)
    {
        "vouchercode": "kjadjhgadjgh7667"
    }

    Will return 200 and the voucher as json if succesful.
    If unsuccessful, will return 406 with the error.
    """
    v_ser = serializers.VoucherAddSerializer(data=request.data,
                                             context={'request': request})
    if v_ser.is_valid():
        basket = get_basket(request)

        voucher = v_ser.instance
        basket.vouchers.add(voucher)

        signals.voucher_addition.send(sender=None,
                                      basket=basket,
                                      voucher=voucher)

        # Recalculate discounts to see if the voucher gives any
        apply_offers(request, basket)
        discounts_after = basket.offer_applications

        # Look for discounts from this new voucher
        for discount in discounts_after:
            if discount['voucher'] and discount['voucher'] == voucher:
                break
        else:
            basket.vouchers.remove(voucher)
            return Response(
                {
                    'reason':
                    _("Your basket does not qualify for a voucher discount")
                },
                status=status.HTTP_406_NOT_ACCEPTABLE)

        ser = serializers.VoucherSerializer(voucher,
                                            context={'request': request})
        return Response(ser.data)

    return Response(v_ser.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 8
0
def add_voucher(request, format=None):
    """
    Add a voucher to the basket.

    POST(vouchercode)
    {
        "vouchercode": "kjadjhgadjgh7667"
    }

    Will return 200 and the voucher as json if succesful.
    If unsuccessful, will return 406 with the error.
    """
    v_ser = serializers.VoucherAddSerializer(data=request.data,
                                             context={'request': request})
    if v_ser.is_valid():
        basket = get_basket(request)

        voucher = v_ser.instance
        basket.vouchers.add(voucher)

        signals.voucher_addition.send(
            sender=None, basket=basket, voucher=voucher)

        # Recalculate discounts to see if the voucher gives any
        apply_offers(request, basket)
        discounts_after = basket.offer_applications

        # Look for discounts from this new voucher
        for discount in discounts_after:
            if discount['voucher'] and discount['voucher'] == voucher:
                break
        else:
            basket.vouchers.remove(voucher)
            return Response(
                {'reason': _(
                    "Your basket does not qualify for a voucher discount")},
                status=status.HTTP_406_NOT_ACCEPTABLE)

        ser = serializers.VoucherSerializer(
            voucher, context={'request': request})
        return Response(ser.data)

    return Response(v_ser.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 9
0
    def post(self, request, format=None):
        p_ser = self.add_product_serializer_class(data=request.data,
                                                  context={'request': request})
        if p_ser.is_valid():
            basket = operations.get_basket(request)
            product = p_ser.validated_data['url']
            quantity = p_ser.validated_data['quantity']
            options = p_ser.validated_data.get('options', [])

            basket_valid, message = self.validate(basket, product, quantity,
                                                  options)
            if not basket_valid:
                return Response({'reason': message},
                                status=status.HTTP_406_NOT_ACCEPTABLE)

            basket.add_product(product, quantity=quantity, options=options)
            operations.apply_offers(request, basket)
            ser = self.serializer_class(basket, context={'request': request})
            return Response(ser.data)

        return Response({'reason': p_ser.errors},
                        status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 10
0
    def post(self, request, format=None):
        p_ser = serializers.AddProductSerializer(
            data=request.DATA, context={'request': request})
        if p_ser.is_valid():
            basket = get_basket(request)
            product = p_ser.validated_data['url']
            quantity = p_ser.validated_data['quantity']
            options = p_ser.validated_data.get('options', [])

            basket_valid, message = self.validate(basket, product, quantity, options)
            if not basket_valid:
                return Response(
                    {'reason': message},
                    status=status.HTTP_406_NOT_ACCEPTABLE)

            basket.add_product(product, quantity=quantity, options=options)
            apply_offers(request, basket)
            ser = self.serializer_class(
                basket, context={'request': request})
            return Response(ser.data)

        return Response(
            {'reason': p_ser.errors}, status=status.HTTP_406_NOT_ACCEPTABLE)