Ejemplo n.º 1
0
def create_basket_session(request, parameters):
    serializer = serializers.BasketSerializer(data=parameters)
    serializer.is_valid(raise_exception=True)

    custom = serializer.validated_data.get('custom_basket')
    # validate product list
    if custom:
        product_serializer = serializers.CheckoutCustomProductSerializer(data=serializer.initial_data.get('products'), many=True)
    else:
        product_serializer = serializers.CheckoutProductSerializer(data=serializer.initial_data.get('products'), many=True)
    product_serializer.is_valid(raise_exception=True)
        
    # validate basket
    if serializer.validated_data.get('vouchers'):
        if custom:
            basket = createCustomBasket(serializer.validated_data['products'],
                                        request.user, serializer.validated_data['system'],
                                        vouchers=serializer.validated_data['vouchers'])
        else:
            basket = createBasket(serializer.validated_data['products'], request.user,
                                    serializer.validated_data['system'],
                                    vouchers=serializer.validated_data['vouchers'])
    else:
        if custom:
            basket = createCustomBasket(serializer.validated_data['products'],
                                        request.user, serializer.validated_data['system'])
        else:
            basket = createBasket(serializer.validated_data['products'],
                                    request.user, serializer.validated_data['system'])

    return basket, BasketMiddleware().get_basket_hash(basket.id)
Ejemplo n.º 2
0
def create_basket_session(request, parameters):
    serializer = serializers.BasketSerializer(data=parameters)
    serializer.is_valid(raise_exception=True)
    custom = serializer.validated_data.get('custom_basket')
    booking_reference = None
    if 'booking_reference' in parameters:
        booking_reference = serializer.validated_data['booking_reference']
    # validate product list
    if custom:
        product_serializer = serializers.CheckoutCustomProductSerializer(
            data=serializer.initial_data.get('products'), many=True)
    else:
        product_serializer = serializers.CheckoutProductSerializer(
            data=serializer.initial_data.get('products'), many=True)
    product_serializer.is_valid(raise_exception=True)
    # Cleaning up stale Baskets
    if request.user:
        if request.user.__class__.__name__ == 'EmailUser':
            ba = Basket.objects.filter(owner=request.user).exclude(
                status='Submitted')
            for b in ba:
                b.status = 'Frozen'
                b.save()
    # validate basket
    if serializer.validated_data.get('vouchers'):
        if custom:
            basket = createCustomBasket(serializer.validated_data['products'],
                                        request.user,
                                        serializer.validated_data['system'],
                                        serializer.validated_data['vouchers'],
                                        True, booking_reference)
        else:
            basket = createBasket(serializer.validated_data['products'],
                                  request.user,
                                  serializer.validated_data['system'],
                                  serializer.validated_data['vouchers'], True,
                                  booking_reference)
    else:
        if custom:
            basket = createCustomBasket(serializer.validated_data['products'],
                                        request.user,
                                        serializer.validated_data['system'],
                                        None, True, booking_reference)
        else:
            basket = createBasket(serializer.validated_data['products'],
                                  request.user,
                                  serializer.validated_data['system'], None,
                                  True, booking_reference)

    return basket, BasketMiddleware().get_basket_hash(basket.id)
Ejemplo n.º 3
0
def use_existing_basket_from_invoice(invoice):

    inv = Invoice.objects.get(reference=invoice)
    order = Order.objects.get(number=inv.order_number)
    basket = order.basket
    return basket, BasketMiddleware().get_basket_hash(order.basket.id)
Ejemplo n.º 4
0
def use_existing_basket(basket):

    return basket, BasketMiddleware().get_basket_hash(basket.id)
Ejemplo n.º 5
0
    def create(self, request):
        try:
            http_status = status.HTTP_200_OK
            #parse and validate data
            serializer = self.serializer_class(data=request.data)
            serializer.is_valid(raise_exception=True)
            #Check if it is a custom basket
            custom = serializer.validated_data.get('custom_basket')
            # Validate Products
            if custom:
                product_serializer = CheckoutCustomProductSerializer(
                    data=serializer.initial_data.get('products'), many=True)
            else:
                product_serializer = CheckoutProductSerializer(
                    data=serializer.initial_data.get('products'), many=True)
            product_serializer.is_valid(raise_exception=True)
            #create basket
            if serializer.validated_data.get('vouchers'):
                if custom:
                    basket = createCustomBasket(
                        serializer.validated_data['products'],
                        request.user,
                        serializer.validated_data['system'],
                        vouchers=serializer.validated_data['vouchers'])
                else:
                    basket = createBasket(
                        serializer.validated_data['products'],
                        request.user,
                        serializer.validated_data['system'],
                        vouchers=serializer.validated_data['vouchers'])
            else:
                if custom:
                    basket = createCustomBasket(
                        serializer.validated_data['products'], request.user,
                        serializer.validated_data['system'])
                else:
                    basket = createBasket(
                        serializer.validated_data['products'], request.user,
                        serializer.validated_data['system'])

            redirect = HttpResponseRedirect(
                reverse('checkout:index') +
                '?{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}'.format(
                    self.get_redirect_value(serializer, 'card_method'),
                    self.get_redirect_value(serializer, 'basket_owner'),
                    self.get_redirect_value(serializer, 'template'),
                    self.get_redirect_value(serializer, 'fallback_url'),
                    self.get_redirect_value(serializer, 'return_url'),
                    self.get_redirect_value(serializer,
                                            'associateInvoiceWithToken'),
                    self.get_redirect_value(serializer, 'forceRedirect'),
                    self.get_redirect_value(serializer, 'sendEmail'),
                    self.get_redirect_value(serializer, 'proxy'),
                    self.get_redirect_value(serializer, 'checkoutWithToken'),
                    self.get_redirect_value(serializer, 'bpay_format'),
                    self.get_redirect_value(serializer, 'icrn_format'),
                    self.get_redirect_value(serializer, 'invoice_text'),
                    self.get_redirect_value(serializer, 'check_url')))
            # inject the current basket into the redirect response cookies
            # or else, anonymous users will be directionless
            redirect.set_cookie(settings.OSCAR_BASKET_COOKIE_OPEN,
                                BasketMiddleware().get_basket_hash(basket.id),
                                max_age=settings.OSCAR_BASKET_COOKIE_LIFETIME,
                                secure=settings.OSCAR_BASKET_COOKIE_SECURE,
                                httponly=True)
            return redirect
        except serializers.ValidationError:
            raise
        except Exception as e:
            traceback.print_exc()
            raise serializers.ValidationError(str(e))