Ejemplo n.º 1
0
 def create_order(self, request, form=None):
     if request.session.get(settings.ORDER_KEY):
         uid = request.session[settings.ORDER_KEY]
         try:
             order = Order.objects.get(uid=uid)
             order.delete()
         except:
             pass
     
     #try to search if contact already exists in database
     try:
         contact = get_object_or_404(Client, email=self.cleaned_data['email'])
         self.instance = contact
         #print "he trobat instancia %s" % self.instance
     except:
         contact = None;
 
     #contact = save_instance(self, contact)
     if self.instance:
         contact = save_instance(self, self.instance, construct=True)
     else:
         self.save()
             
     c = cart_from_session(request)
     pay_type = self.cleaned_data['pago']
     
     order = order_from_cart(c, contact, pay_type, form)
     return order    
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 add_to_cart(request):
    ''' add a product to cart, with product variations if have... '''
    f = CartForm(request.POST)
    if f.is_valid():
        product = f.get_product()
        c = cart_from_session(request)
        variation = f.cleaned_data['sub'] if product.has_variations() else None
        c.add((product.pk, variation, f.cleaned_data['qty']))
        c.save(request)
    else:
        raise Http404

    if request.is_ajax():
        return HttpResponse('ok')

    return HttpResponseRedirect(reverse('view_cart'))
Ejemplo n.º 5
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.º 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 process_request(self, request):

        from shop.cart import cart_from_session
        c = cart_from_session(request)
        if c.total() == 0:
            request.cart = None
            return None

        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
        request.cart = val
        return None
Ejemplo n.º 8
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.º 9
0
    def process_request(self, request):
        
        from shop.cart import cart_from_session
        c = cart_from_session(request)
        if c.total() == 0:
            request.cart = None
            return None
            
        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
        request.cart = val
        return None
Ejemplo n.º 10
0
def remove_from_cart(request, id):
    ''' delete an item from cart '''
    c = cart_from_session(request)
    c.delete(int(id))
    c.save(request)
    return HttpResponseRedirect(reverse('view_cart'))