Makes items_purchased do more database work

This commit is contained in:
Christopher Neugebauer 2016-04-28 19:52:39 +10:00
parent a79ad3520e
commit fd5cf50fab

View file

@ -4,7 +4,11 @@ from registrasion.controllers.category import CategoryController
from collections import namedtuple from collections import namedtuple
from django import template from django import template
from django.db.models import Case
from django.db.models import Q
from django.db.models import Sum from django.db.models import Sum
from django.db.models import When
from django.db.models import Value
register = template.Library() register = template.Library()
@ -99,20 +103,33 @@ def items_purchased(context, category=None):
''' '''
all_items = commerce.ProductItem.objects.filter( in_cart=(
cart__user=context.request.user, Q(productitem__cart__user=context.request.user) &
cart__status=commerce.Cart.STATUS_PAID, Q(productitem__cart__status=commerce.Cart.STATUS_PAID)
).select_related("product", "product__category") )
quantities_in_cart = When(
in_cart,
then="productitem__quantity",
)
quantities_or_zero = Case(
quantities_in_cart,
default=Value(0),
)
products = inventory.Product.objects
if category: if category:
all_items = all_items.filter(product__category=category) products = products.filter(category=category)
products = products.select_related("category")
products = products.annotate(quantity=Sum(quantities_or_zero))
products = products.filter(quantity__gt=0)
pq = all_items.values("product").annotate(quantity=Sum("quantity")).all()
products = inventory.Product.objects.all()
out = [] out = []
for item in pq: for prod in products:
prod = products.get(pk=item["product"]) out.append(ProductAndQuantity(prod, prod.quantity))
out.append(ProductAndQuantity(prod, item["quantity"]))
return out return out