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)
def shipping_methods(request, format=None): basket = operations.get_basket(request) shiping_methods = Repository().get_shipping_methods(basket=basket, user=request.user, request=request) ser = serializers.ShippingMethodSerializer(shiping_methods, many=True, context={'basket': basket}) return Response(ser.data)
def get(self, request, pk=None, format=None): line = self.get_object() basket = operations.get_basket(request) # if the line is from the current basket, use the serializer that # computes the prices by using the strategy. if line.basket == basket: operations.assign_basket_strategy(line.basket, request) ser = serializers.BasketLineSerializer( instance=line, context={'request': request}) return Response(ser.data) return super(LineDetail, self).get(request, pk, format)
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)
def get(self, request, format=None): basket = operations.get_basket(request) ser = self.serializer_class(basket, context={'request': request}) return Response(ser.data)