Exemplo n.º 1
0
def edit_purchase(request, purchase_id, **kwargs):

    try:
        purchase = Purchase.objects.polymorphic().get(pk=purchase_id)
    except Purchase.DoesNotExist:
        raise Http404

    cart = cart_from_request(request)
    messages = FlashMessages()
    form = EditPurchaseForm.factory(
        purchase,
        data=request.POST
    )(request.POST)

    if 'remove' in request.POST:
        cart.remove(purchase)
        messages.success(
            request, _(
                "{0} has been removed from your "
                "cart."
            ).format(purchase)
        )
    else:
        # We require a valid form
        if form.is_valid():
            try:
                purchase = Purchase.make(**form.get_edit_purchase_args())
                cart.update(purchase)
                messages.success(request, _("Your cart has been updated."))
            except PurchaseInvalid as error:
                messages.error(
                    request, _(
                        "Your cart could not be "
                        "updated."
                    )
                )
                messages.error(request, error)
        else:
            messages.error(request, _("Your cart could not be updated."))
            messages.error(request, get_error_message(form))

    # Provide default response
    yield redirect(
        reverse(
            request.POST.get(
                'next',
                request.GET.get('next', 'cart:cart')),
            current_app=resolve(request.path).namespace
        )
    )

    yield chaining.update(
        purchase=purchase,
        cart=cart,
        messages=messages)

    if (yield chaining.forward).result is None:
        raise ViewNotImplemented

    if cart.has_changed():
        cart.calculate()
        cart.save()

    messages.transmit()
Exemplo n.º 2
0
def add_to_cart(request, **kwargs):

    cart = cart_from_request(request)
    messages = FlashMessages()
    form = AddToCartForm.factory(data=request.POST)(request.POST)

    # Create purchase
    purchase = None
    if form.is_valid():
        try:
            purchase = Purchase.make(**form.get_purchase_args())
        except PurchaseInvalid as error:
            messages.error(request, _("Could not be added to your cart."))
            messages.error(request, error)
    else:
        messages.error(request, _("Your cart could not be updated."))
        messages.error(request, get_error_message(form))

    # Add purchase to cart
    if purchase is not None:
        try:
            cart.add(purchase)
            messages.success(
                request, _(
                    "{0} has been added to your "
                    "cart."
                ).format(purchase)
            )

        except PurchaseInvalid as error:
            messages.error(
                request, _(
                    "{0} could not be added to "
                    "your cart."
                ).format(purchase)
            )
            messages.error(request, error)

    # Provide default response
    yield redirect(
        reverse(
            request.POST.get(
                'next',
                request.GET.get('next', 'cart:cart')),
            current_app=resolve(request.path).namespace
        )
    )

    yield chaining.update(
        purchase=purchase,
        cart=cart,
        messages=messages)

    if (yield chaining.forward).result is None:
        raise ViewNotImplemented

    if cart.has_changed():
        cart.calculate()
        cart.save()
        cart.track(request)

    messages.transmit()