Replaces CategoryController.attach_user_remainders with user_remainders

This commit is contained in:
Christopher Neugebauer 2016-04-30 20:30:44 +10:00
parent 941caa30d9
commit c6fdfa496e
3 changed files with 10 additions and 23 deletions

View file

@ -240,12 +240,11 @@ class CartController(object):
by_cat[product.category].append((product, quantity))
# Pre-annotate categories
r = CategoryController.attach_user_remainders(self.cart.user, by_cat)
with_remainders = dict((cat, cat) for cat in r)
remainders = CategoryController.user_remainders(self.cart.user)
# Test each category limit here
for category in by_cat:
limit = with_remainders[category].remainder
limit = remainders[category.id]
# Get the amount so far in the cart
to_add = sum(i[1] for i in by_cat[category])

View file

@ -39,17 +39,16 @@ class CategoryController(object):
return set(i.category for i in available)
@classmethod
def attach_user_remainders(cls, user, categories):
def user_remainders(cls, user):
'''
Return:
queryset(inventory.Product): A queryset containing items from
``categories``, with an extra attribute -- remainder = the amount
of items from this category that is remaining.
Mapping[int->int]: A dictionary that maps the category ID to the
user's remainder for that category.
'''
ids = [category.id for category in categories]
categories = inventory.Category.objects.filter(id__in=ids)
categories = inventory.Category.objects.all()
cart_filter = (
Q(product__productitem__cart__user=user) &
@ -73,12 +72,4 @@ class CategoryController(object):
categories = categories.annotate(remainder=remainder)
return categories
def user_quantity_remaining(self, user):
''' Returns the quantity of this product that the user add in the
current cart. '''
with_remainders = self.attach_user_remainders(user, [self.category])
return with_remainders[0].remainder
return dict((cat.id, cat.remainder) for cat in categories)

View file

@ -34,16 +34,13 @@ class ProductController(object):
if products is not None:
all_products = set(itertools.chain(all_products, products))
categories = set(product.category for product in all_products)
r = CategoryController.attach_user_remainders(user, categories)
cat_quants = dict((c, c) for c in r)
category_remainders = CategoryController.user_remainders(user)
product_remainders = ProductController.user_remainders(user)
passed_limits = set(
product
for product in all_products
if cat_quants[product.category].remainder > 0
if category_remainders[product.category.id] > 0
if product_remainders[product.id] > 0
)