Adds missing_categories tag
This commit is contained in:
		
							parent
							
								
									4ad96286c3
								
							
						
					
					
						commit
						faa25c9b3a
					
				
					 2 changed files with 34 additions and 3 deletions
				
			
		| 
						 | 
					@ -1,8 +1,11 @@
 | 
				
			||||||
''' NEEDS TESTS '''
 | 
					''' NEEDS TESTS '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import operator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from registrasion.models import commerce
 | 
					from registrasion.models import commerce
 | 
				
			||||||
from registrasion.models import inventory
 | 
					from registrasion.models import inventory
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from collections import Iterable
 | 
				
			||||||
from collections import namedtuple
 | 
					from collections import namedtuple
 | 
				
			||||||
from django.db.models import Case
 | 
					from django.db.models import Case
 | 
				
			||||||
from django.db.models import Q
 | 
					from django.db.models import Q
 | 
				
			||||||
| 
						 | 
					@ -34,6 +37,7 @@ class ItemController(object):
 | 
				
			||||||
        ''' Aggregates the items that this user has purchased.
 | 
					        ''' Aggregates the items that this user has purchased.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Arguments:
 | 
					        Arguments:
 | 
				
			||||||
 | 
					            cart_status (int or Iterable(int)): etc
 | 
				
			||||||
            category (Optional[models.inventory.Category]): the category
 | 
					            category (Optional[models.inventory.Category]): the category
 | 
				
			||||||
                of items to restrict to.
 | 
					                of items to restrict to.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,11 +47,18 @@ class ItemController(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        '''
 | 
					        '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        in_cart = (
 | 
					        if not isinstance(cart_status, Iterable):
 | 
				
			||||||
            Q(productitem__cart__user=self.user) &
 | 
					            cart_status = [cart_status]
 | 
				
			||||||
            Q(productitem__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(
 | 
					        quantities_in_cart = When(
 | 
				
			||||||
            in_cart,
 | 
					            in_cart,
 | 
				
			||||||
            then="productitem__quantity",
 | 
					            then="productitem__quantity",
 | 
				
			||||||
| 
						 | 
					@ -72,6 +83,11 @@ class ItemController(object):
 | 
				
			||||||
            out.append(ProductAndQuantity(prod, prod.quantity))
 | 
					            out.append(ProductAndQuantity(prod, prod.quantity))
 | 
				
			||||||
        return out
 | 
					        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):
 | 
					    def items_purchased(self, category=None):
 | 
				
			||||||
        ''' Aggregates the items that this user has purchased.
 | 
					        ''' Aggregates the items that this user has purchased.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,21 @@ def available_categories(context):
 | 
				
			||||||
    return CategoryController.available_categories(context.request.user)
 | 
					    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)
 | 
					@register.assignment_tag(takes_context=True)
 | 
				
			||||||
def available_credit(context):
 | 
					def available_credit(context):
 | 
				
			||||||
    ''' Calculates the sum of unclaimed credit from this user's credit notes.
 | 
					    ''' Calculates the sum of unclaimed credit from this user's credit notes.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue