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 process_response(self, request, response): if self.is_api_request(request) and hasattr(request, 'user') and request.session: # at this point we are sure a basket can be found in the session # (if the session hasn't been destroyed by logging out), # because it is enforced in process_request. # We just have to make sure it is stored as a cookie, because it # could have been created by oscarapi. cookie_key = self.get_cookie_key(request) basket = get_basket(request) cookie = self.get_basket_hash(basket.id) # Delete any surplus cookies cookies_to_delete = getattr(request, 'cookies_to_delete', []) for cookie_key in cookies_to_delete: response.delete_cookie(cookie_key) if not request.user.is_authenticated: response.set_cookie( cookie_key, cookie, max_age=settings.OSCAR_BASKET_COOKIE_LIFETIME, secure=settings.OSCAR_BASKET_COOKIE_SECURE, httponly=True) return response else: return super( ApiBasketMiddleWare, self).process_response(request, response)
def _get(self, request, shipping_address=None, format=None): basket = operations.get_basket(request) shiping_methods = Repository().get_shipping_methods( basket=basket, user=request.user, shipping_addr=shipping_address, request=request) ser = self.shipping_method_serializer_class( shiping_methods, many=True, context={'basket': basket}) return Response(ser.data)
def process_response(self, request, response): if self.is_api_request(request) and hasattr(request, 'user'): # at this point we are sure a basket can be found in the session, # because it is enforced in process_request. # We just have to make sure it is stored as a cookie, because it # could have been created by oscarapi. cookie_key = self.get_cookie_key(request) basket = get_basket(request) request.COOKIES[cookie_key] = self.get_basket_hash(basket.id) return super(ApiBasketMiddleWare, self).process_response(request, response)
def get(self, request, pk=None, format=None): line = self.get_object() basket = 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: 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 shipping_methods(request, format=None): """ Get the available shipping methods and their cost for this order. GET: A list of shipping method details and the prices. """ basket = get_basket(request) shiping_methods = Repository().get_shipping_methods( basket=request.basket, user=request.user, request=request) ser = serializers.ShippingMethodSerializer( shiping_methods, many=True, context={'basket': basket}) return Response(ser.data)
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 __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # We require a request because we need to know what plans are valid for the # user to be drafting from. This is tied to the current basket. request = self.context.get('request', None) assert request is not None, ( "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " "the serializer." % self.__class__.__name__ ) # Limit plans by the user's basket (plan availability is driven by offer/voucher conditions) basket = operations.get_basket(self.context['request']) plans = list_plans_for_basket(basket) self.fields['financing_plan'].queryset = FinancingPlan.objects.filter(id__in=[p.id for p in plans])
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)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # We require a request because we need to know what accounts are valid for the # user to be drafting from. This is derived from the user when authenticated or # the session store when anonymous request = self.context.get('request', None) assert request is not None, ( "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " "the serializer." % self.__class__.__name__ ) # Limit baskets to only the one that is active and owned by the user. basket = get_basket(request) self.fields['basket'].queryset = Basket.objects.filter(pk=basket.pk) # Limit account to only those owned by the user valid_account_ids = IsAccountOwner.list_valid_account_ids(request) valid_accounts = self._base_account_queryset.filter(pk__in=valid_account_ids) self.fields['wfrs_source_account'].queryset = valid_accounts
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 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)
def post(self, request, *args, **kwargs): # pylint: disable=redefined-builtin 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 get(self, request): basket = operations.get_basket(request) plans = list_plans_for_basket(basket) ser = FinancingPlanSerializer(plans, many=True) return Response(ser.data)
def get(self, request, format=None): basket = get_basket(request) ser = self.serializer_class(basket, context={'request': request}) return Response(ser.data)
def post(self, request, format=None): basket = operations.get_basket(request) return Response(basket.id)
def get(self, request, format=None): basket = get_basket(request) ser = serializers.BasketSerializer(basket, context={'request': request}) return Response(ser.data)
def get(self, request, format=None): basket = operations.get_basket(request) ser = self.serializer_class(basket, context={'request': request}) return Response(ser.data)
def get(self, request, *args, **kwargs): # pylint: disable=redefined-builtin basket = operations.get_basket(request) ser = self.serializer_class(basket, context={"request": request}) return Response(ser.data)