Ejemplo n.º 1
0
def order_from_cart(cart, client, payment_type, form):
    '''Creates an order from a given session cart and a client..'''
    o = Order()
    o.client = client
    o.status = Order.PENDING
    o.pay_type = payment_type
    o.save()
    #uid = generate_order_id()
    o.uid = "0000%s-%s" % (o.pk, o.client.email[0:2])
    o.save()
    l = cart_list(cart)
    for item in l:
        print item
        en = Line.objects.create(
            order           =o, 
            types           =Line.PRODUCT, 
            product         =item[0],
            product_option  =item[1],
            quantity        =item[2],
            amount          =item[3],
            amount_discount =item[4],
            total           =item[5])
    #afegim linia de calcul dels shippings...
    amount = cart_total( l )
    cp = client.ship_pc
    signals.order_created.send(Order, order=o, client=client, amount=amount, cart=cart, form=form)
    return o
Ejemplo n.º 2
0
def shipping_cost(request):
    ''' calc shipping cost for ajax request...'''
    #if not request.is_ajax():
    #    raise Http404

    cart = cart_from_session(request)
    cl = cart_list(cart)
    amount = cart_total(cl)

    cp = request.GET.get('cp', None)
    # aquest parxe es per tenir resultats en la vista de checkout,
    # on preguntem els costos d'enviament a un determinat pais...
    if not cp:
        cp = request.GET.get('country')

    if cp != "":
        from shipping import get_shipping_method
        shipping_method = get_shipping_method()
        #print shipping_method
        result = shipping_method(cart, postal=cp, amount=amount)
    else:
        raise Http404

    return direct_to_template(request,
                              "shop/shipping_costs.html",
                              extra_context={
                                  'arrival_date': cart_arrival_day(),
                                  'ship': result
                              })
Ejemplo n.º 3
0
def view_cart(request):
    ''' show cart content, pass 1 from checkout '''
    c = cart_from_session(request)
    context = {}
    context['cart'] = cart_list(c)
    context['total'] = cart_total(context['cart'])
    return direct_to_template(request,
                              template="shop/cart.html",
                              extra_context=context)
Ejemplo n.º 4
0
def checkout(request):
    f = CheckoutForm()
    c = {}
    cart = cart_from_session(request)
    #c['cart'] = cart
    #print cart.total()
    if cart.total() == 0:
        return HttpResponseRedirect(reverse('view_cart'))

    c['cart'] = cart_list(cart)
    c['total'] = cart_total(c['cart'])

    if request.method == "GET":
        if request.session.get(settings.ORDER_KEY):
            uid = request.session.get(settings.ORDER_KEY)
            try:
                order = get_object_or_404(Order, uid=uid)
                f = CheckoutForm(instance=order.client)
            except:
                pass
        c['form'] = f
        return direct_to_template(request,
                                  template="shop/checkout.html",
                                  extra_context=c)
    if request.method == "POST":
        f = CheckoutForm(data=request.POST)
        if f.is_valid():
            order = f.create_order(request, f)
            order.client.lang = get_language()
            order.client.save()
            request.session[settings.ORDER_KEY] = order.uid
            return HttpResponseRedirect(reverse('checkout_confirm'))
        else:
            print f.errors
            c['form'] = f
            return direct_to_template(request,
                                      template="shop/checkout.html",
                                      extra_context=c)
    else:
        c['form'] = f
        return direct_to_template(request,
                                  template="shop/checkout.html",
                                  extra_context=c)
Ejemplo n.º 5
0
def get_cart(context, asvar):

    c = cart_from_session(context['request'])
    val = None
    if c.total() == 0:
        context[asvar] = None
        return ''

    val = cache.get('cart')
    if not val:
        val = {}
        val['total'] = c.total_items()
        l = cart_list(c)
        val['price'] = cart_total(l)
        cache.set('cart', val)

    val['c'] = c
    context[asvar] = val
    return ''
Ejemplo n.º 6
0
def get_cart(context, asvar):
    
    c = cart_from_session(context['request'])
    val = None
    if c.total() == 0:
        context[asvar] = None
        return ''
        
    val = cache.get('cart')
    if not val:
        val = {}
        val['total'] = c.total_items()
        l = cart_list(c)
        val['price'] = cart_total(l)
        cache.set('cart', val)

    val['c'] = c
    context[asvar] = val
    return ''
Ejemplo n.º 7
0
 def test_order_from_cart(self):
     """
     Tests that 1 + 1 always equals 2.
     """
     self.assertEqual(1 + 1, 2)
     client = Client.objects.get(pk=1)
     self.assertEqual(client.email, '*****@*****.**')
     
     c = Cart()
     c.add((12, 37, 2))
     c.add((1, None, 1))
     l = cart_list(c)
     
     order = order_from_cart(c, client, 'paypal')
     self.assertEqual(len(order.line_set.all()), 3)
     # this test fails because not shipping costs
     ship_cost = Decimal(calc_shipping_costs(c, '08695', cart_total(l)))
     self.assertEqual(order.total(), cart_total(l) + ship_cost )
     
     self.assertEqual(order.line_set.all()[2].types, 'ship' )
     self.assertEqual(order.pay_type, 'paypal' )
Ejemplo n.º 8
0
    def test_order_from_cart(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)
        client = Client.objects.get(pk=1)
        self.assertEqual(client.email, '*****@*****.**')

        c = Cart()
        c.add((12, 37, 2))
        c.add((1, None, 1))
        l = cart_list(c)

        order = order_from_cart(c, client, 'paypal')
        self.assertEqual(len(order.line_set.all()), 3)
        # this test fails because not shipping costs
        ship_cost = Decimal(calc_shipping_costs(c, '08695', cart_total(l)))
        self.assertEqual(order.total(), cart_total(l) + ship_cost)

        self.assertEqual(order.line_set.all()[2].types, 'ship')
        self.assertEqual(order.pay_type, 'paypal')