Esempio n. 1
0
def update_cart(request):
    """This method is called when user changes selection quantities on the view cart page, when user clicks 'update cart' or before final checkout.
       key value pair is selection id, selection quantity """
    if request.method == "POST":
        order = shopping_utils.get_order(request)
        xml = '<?xml version="1.0"?><response>'
        #get the selections
        xml += '<selections>'
        for selection_update in request.POST.items():
            #get the params
            id = selection_update[0]
            quantity = selection_update[1]
            #update the django Selection object
            selection = Selection.objects.get(id=id)
            selection.quantity = quantity
            selection.save()
            
            #add an xml row to the response
            xml += '<selection><id>%s</id><quantity>%s</quantity></selection>' % (selection.id, selection.quantity)
        xml += '</selections>'
        #also return the new item count and new subtotal
        new_total_count = shopping_utils.get_order(request).get_item_count()
        xml += '<itemcount>' + str(new_total_count) + '</itemcount>'
        new_subtotal = order.get_subtotal()
        xml += '<subtotal>' + str(new_subtotal) + '</subtotal>'
        xml += '</response>'
        return HttpResponse(xml)
Esempio n. 2
0
def get_paypal_form(request):
    '''this gets called anytime the viewcart page gets updated, to keep the paypal form in sync'''
    context = {}
    context['order'] = shopping_utils.get_order(request)
    context['business'] = settings.PAYPAL_ADDRESS
    context['paypal_url'] = get_paypal_url()
    return render_to_response('shopping/paypal_form.html', context, context_instance=RequestContext(request))  
    
Esempio n. 3
0
def empty_cart(request):
    #get the order
    order = shopping_utils.get_order(request)
    #delete it
    order.delete()
    #return success message
    response = {'success':True}
    responseJSON = simplejson.dumps(response)
    return HttpResponse(responseJSON)
Esempio n. 4
0
def view_cart(request):
    context = {}
    business = settings.PAYPAL_ADDRESS
    context['business'] = business
    context['paypal_url'] = get_paypal_url()
    
    #get or create current order
    order = shopping_utils.get_order(request)
    context['order'] = order
    
    return render_to_response('shopping/viewcart.html', context, context_instance=RequestContext(request))
Esempio n. 5
0
def add_to_cart(request):
    '''Add an item to the shopping cart'''
    if request.method == "POST":
        item_id = request.POST.__getitem__('item_id')
        quantity = request.POST.__getitem__('quantity')
        
        #get the current order and item
        order = shopping_utils.get_order(request)
        #Orders are only saved once an item gets added to it
        order.save()
        #save order back to the session
        request.session['shopping_order'] = order
        
        item = Item.objects.get(id=item_id)
        
        #get the item variations if any
        item_variations = []
        for param in request.POST.items():
            #if the http param is an item variation
            key = param[0]
            if key.startswith("variation_"):
                #get the variation_category
                variation_category = str(key).split("_")[1]
                #print "category: " + variation_category
                #and the value the user selected
                variation_value = param[1]
                #print "value: " + variation_value
                #get the matching ItemVariation object
                item_variation = ItemVariation.objects.get(item = item, variation_value__variation_category__name = variation_category, variation_value__value = variation_value)
                #print "----item variation----"
                #print item_variation
                #add it to the collection of item_variations for this selection
                item_variations.append(item_variation)
        
        
        #if user's order already has a selection of this item - including the same variations - add to the quantity
        #otherwise, create a new selection
        selection = None
        selections = Selection.objects.filter(order=order, item=item)
        if selections.count() == 0:
            #create a new Selection object
            selection = Selection(order=order, item=item, quantity=quantity)
            selection.save()
            for item_variation in item_variations:
                selection.item_variations.add(item_variation)
        else:
            #loop through the selections to find the exact one that matches the variations passed in
            #a match is found if variation count is the same and all variations are in list passed in
            match = False
            for possible_selection in selections:
                if possible_selection.item_variations.count() == len(item_variations):
                    #assume its a match unless the variations don't match
                    match = True
                    for item_variation in possible_selection.item_variations.all():
                        if item_variation not in item_variations:
                            match = False
                            
                    if match == True:
                        #found the match. Add to the quantity
                        selection = possible_selection
                        selection.quantity += int(quantity)
                        selection.save()
                        break
                    
            if match == False:
                #no match ever found. This is for the same item but different variations. creating new selection"
                selection = Selection(order=order, item=item, quantity=quantity)
                selection.save()
                for item_variation in item_variations:
                    selection.item_variations.add(item_variation)
        
        response = {}
        response['num_in_cart'] = item.get_num_in_cart(order)
        response['item_count'] = order.get_item_count()
        responseJSON = simplejson.dumps(response)
        return HttpResponse(responseJSON)
def get_order(request):
    order = shopping_utils.get_order(request)
    return {'order': order}