Adds missing_categories tag

This commit is contained in:
Christopher Neugebauer 2016-09-14 13:28:15 +10:00
parent 4ad96286c3
commit faa25c9b3a
2 changed files with 34 additions and 3 deletions

View file

@ -1,8 +1,11 @@
''' NEEDS TESTS '''
import operator
from registrasion.models import commerce
from registrasion.models import inventory
from collections import Iterable
from collections import namedtuple
from django.db.models import Case
from django.db.models import Q
@ -34,6 +37,7 @@ class ItemController(object):
''' Aggregates the items that this user has purchased.
Arguments:
cart_status (int or Iterable(int)): etc
category (Optional[models.inventory.Category]): the category
of items to restrict to.
@ -43,11 +47,18 @@ class ItemController(object):
'''
in_cart = (
Q(productitem__cart__user=self.user) &
Q(productitem__cart__status=cart_status)
if not isinstance(cart_status, Iterable):
cart_status = [cart_status]
status_query = (
Q(productitem__cart__status=status) for status in cart_status
)
in_cart = Q(productitem__cart__user=self.user)
in_cart = in_cart & reduce(operator.__or__, status_query)
print in_cart
quantities_in_cart = When(
in_cart,
then="productitem__quantity",
@ -72,6 +83,11 @@ class ItemController(object):
out.append(ProductAndQuantity(prod, prod.quantity))
return out
def items_pending_or_purchased(self):
''' Returns the items that this user has purchased or has pending. '''
status = [commerce.Cart.STATUS_PAID, commerce.Cart.STATUS_ACTIVE]
return self._items(status)
def items_purchased(self, category=None):
''' Aggregates the items that this user has purchased.

View file

@ -20,6 +20,21 @@ def available_categories(context):
return CategoryController.available_categories(context.request.user)
@register.assignment_tag(takes_context=True)
def missing_categories(context):
''' Adds the categories that the user does not currently have. '''
user = context.request.user
categories_available = set(CategoryController.available_categories(user))
items = ItemController(user).items_pending_or_purchased()
categories_held = set()
for product, quantity in items:
categories_held.add(product.category)
return categories_available - categories_held
@register.assignment_tag(takes_context=True)
def available_credit(context):
''' Calculates the sum of unclaimed credit from this user's credit notes.