예제 #1
0
def checkout(request):
    shopping_cart = ShoppingCart(request.session.get('shopping_cart', {}))
    if request.method == 'POST':
        form = DeliveryForm(request.POST)
        if form.is_valid() and not shopping_cart.is_empty():
            # Add new purchase order and all items
            purchase_order = PurchaseOrder.objects.create(**form.cleaned_data)
            purchase_order_items = []
            for shopping_cart_item in shopping_cart.all():
                purchase_order_item = PurchaseOrderItem(
                    product_name=shopping_cart_item['product_name'],
                    product_price=shopping_cart_item['product_price'],
                    product_discount=shopping_cart_item['product_discount'],
                    product_qty=shopping_cart_item['product_qty']
                )
                purchase_order_items.append(purchase_order_item)
            purchase_order.purchaseorderitem_set.add(*purchase_order_items)
            # Remove shopping cart from user's session
            del request.session['shopping_cart']
            request.session['last_purchase_order'] = purchase_order.id
            # Email customer and managers
            send_mail(
                subject=u'Заказ #%s' % purchase_order.id,
                message=render_to_string(
                    'email-purchase_order_confirmation.html',
                    dict(purchase_order=purchase_order)),
                from_email=settings.EMAIL_HOST_USER,
                recipient_list=[purchase_order.customer_email],
                fail_silently=True)
            mail_managers(
                subject=u'Новый заказ на сумму %s руб.' % purchase_order.total_vals()['price'],
                message=u'Куй железо пока горячо!',
                fail_silently=True)
            return redirect('thank_you')
    else:
        form = DeliveryForm()
    return render_to_response(
        'checkout.html',
        dict(form=form, shopping_cart_is_empty=shopping_cart.is_empty()),
        context_instance=RequestContext(request))
예제 #2
0
def shopping_cart(request, id=None):
    id = int(id) if id else None
    shopping_cart = ShoppingCart(request.session.get('shopping_cart', {}))
    if request.method in ('POST', 'PUT'):
        form = ProductActionsForm(simplejson.loads(request.raw_post_data))
        if form.is_valid():
            if id is None:
                response = shopping_cart.create(form.cleaned_data)
            else:
                response = shopping_cart.update(id, form.cleaned_data)
        else:
            response = None
    elif request.method == 'DELETE':
        response = shopping_cart.delete(id)
    elif request.method == 'GET' and id:
        response = shopping_cart.get(id)
    else:
        response = shopping_cart.all()
    request.session['shopping_cart'] = shopping_cart.shopping_cart
    return response