Mohammed Shamma
Mohammed Shamma

Reputation: 35

Can I use the existing oscar oscar-api object model to implement a single-item only basket?

I'm working on a modified checkout view that accepts a SINGLE product/price/quantity, customer info (name, address, email) and shipping charges.

QUESTION: Can I use the existing django-oscar-api methods for this? In other words, I'd like to enforce a basket model that contains only a single product and uses (LIFO) last in first out when making updates.

Here is my first attempt at it:

# This method is a hybrid of two oscarapi methods
# 1. oscarapi/basket/add-product
# 2. oscarapi/checkout
# It only allows one product to be added to the
# basket and immediately freezes it so that the
# customer can checkout
def post(self, request, format=None):
    # deserialize the product from json
    p_ser = self.add_product_serializer_class(
        data=request.data['addproduct'], context={'request': request})
    if p_ser.is_valid():
        # create a basket
        basket = operations.get_basket(request)
        # load the validate the product
        product = p_ser.validated_data['url']
        # load the validated quantity
        quantity = p_ser.validated_data['quantity']
        # load any options
        options = p_ser.validated_data.get('options', [])
        #validate the basket
        basket_valid, message = self.validate(
            basket, product, quantity, options)
        if not basket_valid:
            return Response(
                {'reason': message},
                status=status.HTTP_406_NOT_ACCEPTABLE)

        # add the product to the validated basket
        basket.add_product(product, quantity=quantity, options=options)

        # apply offers
        operations.apply_offers(request, basket)
        ###### from oscarapi.views.checkout
        # deserialize the checkout object and complete the order
        co_data = request.data['checkout']
        co_data['basket'] = "http://127.0.0.1:8000/oscar-api/baskets/"+str(basket.pk)+"/"
        c_ser = self.checkout_serializer(
            data=co_data, context={'request': request})
        if c_ser.is_valid():
            order = c_ser.save()
            basket.freeze()
            o_ser = self.order_serializer_class(
                order, context={'request': request})
            oscarapi_post_checkout.send(
                sender=self, order=order, user=request.user,
                request=request, response=response)
            return response.Response(o_ser.data)

        return response.Response(c_ser.errors, status.HTTP_406_NOT_ACCEPTABLE)

Upvotes: 1

Views: 582

Answers (1)

Raydel Miranda
Raydel Miranda

Reputation: 14360

The raw answer is yes.

Now ...

As you surely have noticed, implementing this using the oscar-api is not a trivial task. oscar-api it just not implemented thinking in such features. It is a REST API, nothing else.

The simplest way ...

I would recommend to go deeper and fork the basket app and overwrite the Basket.add_product method to ensure what you want, simple.

0f course this is only valid if you own the backend implementation or have some decision power over it.

Look this:

class Basket(AbstractBasket):
    # ...
    def add_product(self, product, quantity=1, options=None):
        if quantity > 1:
            # Throw an error, set it to 1, is up to you
        # Remove all lines.
        self.flush()
        # Do as usual.
        super(Basket, self).add_product(product, quantity, options)

very very simple!

Upvotes: 1

Related Questions