Exemple #1
0
def orders_list(request, tmpl, action=None):
    msg_ok = msg_err = None
    if action == 'validate':
        cart = Cart(request)
        if not cart.has_gcs_ckecked():
            return HttpResponseRedirect('/resa/cart/uncheckedgcs/')
        if not cart.is_valid():
            return HttpResponseRedirect('/resa/cart/invalid/')
        elif not cart.empty():
            order = Order(user=request.user, creation_date=date.now(), donation = cart.donation)
            order.save_confirm(cart)
            cart.clear()
            cart.save(request)
            msg_ok = _(u"Order successfully confirmed")

    pending_orders = request.user.order_set.filter(payment_date__isnull=True)
    validated_orders = request.user.order_set.filter(payment_date__isnull=False)
    return tmpl, {
        'pending_orders': pending_orders,
        'validated_orders': validated_orders,
        'msg_err': msg_err, 'msg_ok': msg_ok,
        'user_obj': request.user,
        'currency': settings.CURRENCY, 'currency_alt': settings.CURRENCY_ALT,
    }
Exemple #2
0
def manage_cart(request, tmpl, user_id=None, action=None, product_id=None):
    user = None
    if user_id:
        try:
            user = User.objects.get(id=user_id)
        except:
            user = None
    msg_ok = msg_err = products = cart = None
    products = None
    if user:
        cart = Cart(request, user.id)
        if request.user.is_staff:
            products = Article.objects.all().order_by('order')
        else:
            products = Article.objects.filter(enabled=True).order_by('order')
        if action == 'add':
            product_id = int(request.POST.get('cart_add'))
            if cart.add(product_id, 1):
                msg_ok = _(u"Product successfully added to cart")
            else:
                msg_err = _(u"Error while adding product to cart")
        elif action == 'del':
            if cart.delete(int(product_id)):
                msg_ok = _(u"Product successfully removed from cart")
            else:
                msg_err = _(u"Error while removing product from cart")
        elif action == 'update':
            update = True
            for k,v  in request.POST.iteritems():
                product_id = 0
                try:
                    quantity = int(v)
                except:
                    quantity = 0
                if k.startswith('product_'):
                    product_id = int(k[8:])
                    update = update and cart.update(product_id, quantity)
            if update:
                msg_ok = _(u"Product(s) successfully updated")
            else:
                msg_err = _(u"Error while updating product(s)")
        elif action == 'validate':
            valid = cart.is_valid()
            if not valid and request.user.is_staff:
                valid = request.POST.get('force') == '1'
            if not valid:
                msg_err = _(u"Unable to confirm this order, one (or more) product(s) in the cart exceed the available quantity")
            else:
                order = Order(user=user, creation_date=date.now())
                order.save_confirm(cart)
                cart.clear()
                msg_ok = _(u"Order successfully confirmed")
        cart.save(request)
    return tmpl, {
        'user_obj': user,
        'products': products,
        'cart': cart,
        'msg_err': msg_err, 'msg_ok': msg_ok,
        'is_admin': request.user.is_staff,
        'currency': settings.CURRENCY, 'currency_alt': settings.CURRENCY_ALT,
    }