def render(self, context, instance, placeholder): try: cart = CartModel.objects.get_from_request(context['request']) context['is_cart_filled'] = cart.items.exists() render_type = instance.glossary['render_type'] if render_type == 'static': # update context for static cart with items to be endered as HTML cart_serializer = CartSerializer(cart, context=context, label='cart', with_items=True) context['cart'] = cart_serializer.data elif render_type == 'summary': # update context for cart summary to be endered as HTML cart_serializer = CartSerializer(cart, context=context, label='cart') context['cart'] = cart_serializer.data except (KeyError, CartModel.DoesNotExist): pass return self.super(ShopCartPlugin, self).render(context, instance, placeholder)
def update(self, request, *args, **kwargs): """ Handle changing the amount of the cart item referred by its primary key. """ cart_item = self.get_object() context = self.get_serializer_context() item_serializer = self.item_serializer_class( cart_item, context=context, data=request.data, label=self.serializer_label) item_serializer.is_valid(raise_exception=True) self.perform_update(item_serializer) cart_serializer = CartSerializer(cart_item.cart, context=context, label='cart') response_data = { 'cart_item': item_serializer.data, 'cart': cart_serializer.data, } return Response(data=response_data)