from django.conf import settings
from django.shortcuts import get_object_or_404
from products.models import Product


def cart_contents(request):
    # Use get or initialize into empty dictionary.
    cart_items = []
    total = 0
    product_count = 0
    cart = request.session.get('cart', {})

# iterate through items to add up total cost and product count
    for item_id, quantity in cart.items():

        #  Bug chase - pk=item_id  and carries through to views.py?   
        product = get_object_or_404(Product, pk=item_id)
        total += quantity * product.price
        product_count += quantity
        cart_items.append({
            'item_id': item_id,
            'quantity': quantity,
            'product': product,
        })      
   
    if total == 0:
        delivery = 0
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total

    elif total < settings.FREE_DELIVERY_THRESHOLD:        
        delivery = 7
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    else:
        delivery = 0
        free_delivery_delta = 0

    grand_total = delivery + total
 
    context = {
        'cart_items': cart_items,
        'total': total,
        'product_count': product_count,
        'delivery': delivery,
        'free_delivery_delta': free_delivery_delta,        
        'free_delivery_threshold': settings.FREE_DELIVERY_THRESHOLD,
        'grand_total': grand_total,
    }
    return context
    
31e54f4b2e900733eebd2a140487f2053f244294
<mask token>

<mask token>


def cart_contents(request):
    cart_items = []
    total = 0
    product_count = 0
    cart = request.session.get('cart', {})
    for item_id, quantity in cart.items():
        product = get_object_or_404(Product, pk=item_id)
        total += quantity * product.price
        product_count += quantity
        cart_items.append({'item_id': item_id, 'quantity': quantity,
            'product': product})
    if total == 0:
        delivery = 0
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    elif total < settings.FREE_DELIVERY_THRESHOLD:
        delivery = 7
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    else:
        delivery = 0
        free_delivery_delta = 0
    grand_total = delivery + total
    context = {'cart_items': cart_items, 'total': total, 'product_count':
        product_count, 'delivery': delivery, 'free_delivery_delta':
        free_delivery_delta, 'free_delivery_threshold': settings.
        FREE_DELIVERY_THRESHOLD, 'grand_total': grand_total}
    return context

from django.conf import settings
from django.shortcuts import get_object_or_404
from products.models import Product


def cart_contents(request):
    cart_items = []
    total = 0
    product_count = 0
    cart = request.session.get('cart', {})
    for item_id, quantity in cart.items():
        product = get_object_or_404(Product, pk=item_id)
        total += quantity * product.price
        product_count += quantity
        cart_items.append({'item_id': item_id, 'quantity': quantity,
            'product': product})
    if total == 0:
        delivery = 0
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    elif total < settings.FREE_DELIVERY_THRESHOLD:
        delivery = 7
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    else:
        delivery = 0
        free_delivery_delta = 0
    grand_total = delivery + total
    context = {'cart_items': cart_items, 'total': total, 'product_count':
        product_count, 'delivery': delivery, 'free_delivery_delta':
        free_delivery_delta, 'free_delivery_threshold': settings.
        FREE_DELIVERY_THRESHOLD, 'grand_total': grand_total}
    return context

from django.conf import settings
from django.shortcuts import get_object_or_404
from products.models import Product


def cart_contents(request):
    # Use get or initialize into empty dictionary.
    cart_items = []
    total = 0
    product_count = 0
    cart = request.session.get('cart', {})

# iterate through items to add up total cost and product count
    for item_id, quantity in cart.items():

        #  Bug chase - pk=item_id  and carries through to views.py?   
        product = get_object_or_404(Product, pk=item_id)
        total += quantity * product.price
        product_count += quantity
        cart_items.append({
            'item_id': item_id,
            'quantity': quantity,
            'product': product,
        })      
   
    if total == 0:
        delivery = 0
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total

    elif total < settings.FREE_DELIVERY_THRESHOLD:        
        delivery = 7
        free_delivery_delta = settings.FREE_DELIVERY_THRESHOLD - total
    else:
        delivery = 0
        free_delivery_delta = 0

    grand_total = delivery + total
 
    context = {
        'cart_items': cart_items,
        'total': total,
        'product_count': product_count,
        'delivery': delivery,
        'free_delivery_delta': free_delivery_delta,        
        'free_delivery_threshold': settings.FREE_DELIVERY_THRESHOLD,
        'grand_total': grand_total,
    }
    return context
    