Example #1
0
def cart_delivery(request):
    if not request.is_ajax():
        raise Http404
    price = 0
    result = None
    html = ''
    cart = ExtCart(request)
    if request.method == 'POST':
        location = request.session.get('cdek_location', None)
        if location and cart.count():
            result = delivery_to_location(location.id, cart)
            if result:
                price = result.get('price', 0)
                location_name = ', '.join(
                    [name for name in (location.city, location.region) if name]
                )
                html = loader.render_to_string('cart/cart_delivery.html', {
                    'location_name': location_name,
                    'delivery': result
                }, context_instance=RequestContext(request))
    return HttpResponse(simplejson.dumps({
        'summary': str(cart.summary()),
        'price': str(price),
        'html': html
    }), mimetype='application/javascript')
Example #2
0
def create_order(request):
    result = None
    cart = ExtCart(request)
    location = request.session.get('cdek_location', None)
    if not cart.count():
        return redirect('checkout:show_cart')
    order = Order.objects.create(
        status='new',
        owner=request.user,
        date_added=datetime.now(),
    )
    try:
        currency = Currency.objects.get(code__exact=settings.DEFAULT_CURRENCY)
    except Currency.DoesNotExist:
        currency = Currency.objects.all()
        if currency:
            currency = currency[0]
    for cart_item in cart:
        OrderProduct.objects.create(
            order = order,
            quantity = cart_item.quantity,
            product = cart_item.get_product(),
            current_price = cart_item.unit_price,
            currency = currency
        )
    TrackOrder.objects.create(
        order = order,
        status = 'new',
        description = _('Order create'),
        date_added = datetime.now(),
        performer = request.user
    )
    if location:
        try:
            currency = Currency.objects.get(code__exact=settings.DELIVERY_CURRENCY)
        except Currency.DoesNotExist:
            currency = Currency.objects.all()
            if currency:
                currency = currency[0]
        args = {
            'order': order,
            'location_to': '%s, %s' % (location.city, location.region),
            'location_to_id': location.id,
            'date_added': datetime.now(),
            'currency': currency
        }
        result = delivery_to_location(location.id, cart)
        if result:
            args['tariff'] = get_or_none(Tariff, cdek_id=result['tariffId'])
            args['price'] = result.get('price', 0)
            args['min_days'] = result.get('deliveryPeriodMin', None)
            args['max_days'] = result.get('deliveryPeriodMax', None)
            Delivery.objects.create(**args)
    cart.clear()
    return redirect('checkout:show_order', order.id)