from .models import Category
from cart.models import CartItem, Cart
from cart.views import _cart_id


def counter(request):
    item_count = 0
    grand_total = 0

    if 'admin' in request.path:
        return{}
    else:
        try:
            cart = Cart.objects.filter(cart_id=_cart_id(request))
            cart_items = CartItem.objects.all().filter(cart=cart[:1])
            for cart_item in cart_items:
                item_count += cart_item.quantity
                grand_total += cart_item.quantity * cart_item.product.price
        except Cart.DoesNotExist:
            item_count = 0
    return dict(item_count=item_count, grand_total=grand_total)


def menu_links(request):
    links = Category.objects.all()
    return dict(links=links)
