From faa25c9b3a7db39e148fa0c0dddb61893d46903d Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 14 Sep 2016 13:28:15 +1000 Subject: [PATCH] Adds missing_categories tag --- registrasion/controllers/item.py | 22 ++++++++++++++++--- .../templatetags/registrasion_tags.py | 15 +++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/registrasion/controllers/item.py b/registrasion/controllers/item.py index 9a1a9eb2..48456f76 100644 --- a/registrasion/controllers/item.py +++ b/registrasion/controllers/item.py @@ -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. diff --git a/registrasion/templatetags/registrasion_tags.py b/registrasion/templatetags/registrasion_tags.py index 2d6f51a4..89db7e25 100644 --- a/registrasion/templatetags/registrasion_tags.py +++ b/registrasion/templatetags/registrasion_tags.py @@ -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.