ConfirmController::sanity_check() returns hard value after signal manipulation

Issue #1488 new
Dag Königrör
created an issue

See file payment/view/confirm.py, ConfirmController::sanity_check()

    def sanity_check(self):
        """Ensure we have a valid cart and order."""
        try:
            self.order = Order.objects.from_request(self.request)

        except Order.DoesNotExist:
            url = urlresolvers.reverse('satchmo_checkout-step1')
            self.invalidate(HttpResponseRedirect(url))
            return False

        try:
            self.cart = Cart.objects.from_request(self.request)
            if self.cart.numItems == 0 and not self.order.is_partially_paid:
                template = self.lookup_template('EMPTY_CART')
                self.invalidate(render_to_response(template,
                                                   context_instance=RequestContext(self.request)))
                return False

        except Cart.DoesNotExist:
            template = self.lookup_template('EMPTY_CART')
            self.invalidate(render_to_response(template,
                                               context_instance=RequestContext(self.request)))
            return False

        # Check if the order is still valid
        if not self.order.validate(self.request):
            context = RequestContext(self.request, 
                {'message': _('Your order is no longer valid.')})
            self.invalidate(render_to_response(self.templates['404'],
                                               context_instance=context))
        #Do a check to make sure we don't have products that are no longer valid
        #or have sold out since the user started the process
        not_enough_qty = False
        invalid_prod = False
        error_products = []
        for cartitem in self.cart:
            stock = cartitem.product.items_in_stock
            if not self.no_stock_checkout:      # If we want to enforce inventory, check again
                if stock < cartitem.quantity:
                    not_enough_qty = True
                    error_products.append(cartitem.product.name)
            if not cartitem.product.active:
                invalid_prod = True
                error_products.append(cartitem.product.name)
        if not_enough_qty or invalid_prod:
            prod_list = ",".join(error_products)
            if not_enough_qty:
                error_message = _('There are not enough %(prod)s in stock to complete your order. Please modify your order.') % {'prod':prod_list}
            else:
                error_message = _('The following products %(prod)s are no longer available. Please modify your order.') % {'prod':prod_list}
            messages.error(self.request, error_message)
            url = urlresolvers.reverse('satchmo_cart')
            self.invalidate(HttpResponseRedirect(url))
            return False
        self.valid = True
        signals.confirm_sanity_check.send(self, controller=self)
        return True

I am talking about the last three lines.

        self.valid = True
        signals.confirm_sanity_check.send(self, controller=self)
        return True

After i did some checks in my listener, eg:

@receiver(confirm_sanity_check)
def my_order_validation(sender, **kwargs):
    controller = sender
    is_valid = False # do your checks
    if not is_valid:
        messages.error(controller.request, _('Not a valid cart, please check again.'))
        url = urlresolvers.reverse('satchmo_cart')
        controller.invalidate(HttpResponseRedirect(url))

... i invalidated the order and controller.isvalid is set to False. But sanity_check returns True anyway.

Am i right with this?

                error_message = _('There are not enough %(prod)s in stock to complete your order. Please modify your order.') % {'prod':prod_list}
            else:
                error_message = _('The following products %(prod)s are no longer available. Please modify your order.') % {'prod':prod_list}
            messages.error(self.request, error_message)
            url = urlresolvers.reverse('satchmo_cart')
            self.invalidate(HttpResponseRedirect(url))
            return self.valid # CHANGE HERE
        self.valid = True
        signals.confirm_sanity_check.send(self, controller=self)
        return self.valid # CHANGE HERE

Comments (0)

  1. Log in to comment